Clean up ServiceButton related code
This commit is contained in:
@@ -20,19 +20,38 @@
|
||||
|
||||
package com.github.shadowsocks.widget
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.support.annotation.DrawableRes
|
||||
import android.support.design.widget.FloatingActionButton
|
||||
import android.support.graphics.drawable.Animatable2Compat
|
||||
import android.support.graphics.drawable.AnimatedVectorDrawableCompat
|
||||
import android.support.v7.widget.TooltipCompat
|
||||
import android.util.AttributeSet
|
||||
import android.view.View
|
||||
import com.github.shadowsocks.App
|
||||
import com.github.shadowsocks.R
|
||||
import com.github.shadowsocks.bg.BaseService
|
||||
import java.util.*
|
||||
|
||||
class ServiceButton(private val fab: FloatingActionButton) : Animatable2Compat.AnimationCallback() {
|
||||
class ServiceButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
|
||||
FloatingActionButton(context, attrs, defStyleAttr) {
|
||||
private val callback = object : Animatable2Compat.AnimationCallback() {
|
||||
override fun onAnimationEnd(drawable: Drawable) {
|
||||
super.onAnimationEnd(drawable)
|
||||
var next = animationQueue.peek() ?: return
|
||||
if (next.current == drawable) {
|
||||
animationQueue.pop()
|
||||
next = animationQueue.peek() ?: return
|
||||
}
|
||||
setImageDrawable(next)
|
||||
next.start()
|
||||
}
|
||||
}
|
||||
|
||||
private fun createIcon(@DrawableRes resId: Int): AnimatedVectorDrawableCompat {
|
||||
val result = AnimatedVectorDrawableCompat.create(fab.context, resId)!!
|
||||
result.registerAnimationCallback(this)
|
||||
val result = AnimatedVectorDrawableCompat.create(context, resId)!!
|
||||
result.registerAnimationCallback(callback)
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -42,6 +61,14 @@ class ServiceButton(private val fab: FloatingActionButton) : Animatable2Compat.A
|
||||
private val iconStopping by lazy { createIcon(R.drawable.ic_service_stopping) }
|
||||
private val animationQueue = ArrayDeque<AnimatedVectorDrawableCompat>()
|
||||
|
||||
private var checked = false
|
||||
|
||||
override fun onCreateDrawableState(extraSpace: Int): IntArray {
|
||||
val drawableState = super.onCreateDrawableState(extraSpace + 1)
|
||||
if (checked) View.mergeDrawableStates(drawableState, intArrayOf(android.R.attr.state_checked))
|
||||
return drawableState
|
||||
}
|
||||
|
||||
fun changeState(state: Int, animate: Boolean) {
|
||||
when (state) {
|
||||
BaseService.CONNECTING -> changeState(iconConnecting, animate)
|
||||
@@ -49,6 +76,17 @@ class ServiceButton(private val fab: FloatingActionButton) : Animatable2Compat.A
|
||||
BaseService.STOPPING -> changeState(iconStopping, animate)
|
||||
else -> changeState(iconStopped, animate)
|
||||
}
|
||||
if (state == BaseService.CONNECTED) {
|
||||
checked = true
|
||||
TooltipCompat.setTooltipText(this, context.getString(R.string.stop))
|
||||
} else {
|
||||
checked = false
|
||||
TooltipCompat.setTooltipText(this, context.getString(R.string.connect))
|
||||
}
|
||||
refreshDrawableState()
|
||||
isEnabled = false
|
||||
if (state == BaseService.CONNECTED || state == BaseService.STOPPED) App.app.handler.postDelayed(
|
||||
{ isEnabled = state == BaseService.CONNECTED || state == BaseService.STOPPED }, 1000)
|
||||
}
|
||||
|
||||
private fun counters(a: AnimatedVectorDrawableCompat, b: AnimatedVectorDrawableCompat): Boolean =
|
||||
@@ -62,26 +100,15 @@ class ServiceButton(private val fab: FloatingActionButton) : Animatable2Compat.A
|
||||
if (animationQueue.size < 2 || !counters(animationQueue.last, icon)) {
|
||||
animationQueue.add(icon)
|
||||
if (animationQueue.size == 1) {
|
||||
fab.setImageDrawable(icon)
|
||||
setImageDrawable(icon)
|
||||
icon.start()
|
||||
}
|
||||
} else animationQueue.removeLast()
|
||||
} else {
|
||||
animationQueue.peekFirst()?.stop()
|
||||
animationQueue.clear()
|
||||
fab.setImageDrawable(icon)
|
||||
setImageDrawable(icon)
|
||||
icon.stop()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onAnimationEnd(drawable: Drawable) {
|
||||
super.onAnimationEnd(drawable)
|
||||
var next = animationQueue.peek() ?: return
|
||||
if (next.current == drawable) {
|
||||
animationQueue.pop()
|
||||
next = animationQueue.peek() ?: return
|
||||
}
|
||||
fab.setImageDrawable(next)
|
||||
next.start()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user