Commit e04350fc authored by Mygod's avatar Mygod Committed by Mygod

Only show progress when connecting is taking too long

parent eb827a45
......@@ -29,6 +29,8 @@ import android.view.View
import androidx.annotation.DrawableRes
import androidx.appcompat.widget.TooltipCompat
import androidx.dynamicanimation.animation.DynamicAnimation
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.vectordrawable.graphics.drawable.Animatable2Compat
import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat
import com.github.shadowsocks.R
......@@ -36,6 +38,8 @@ import com.github.shadowsocks.bg.BaseService
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.progressindicator.BaseProgressIndicator
import com.google.android.material.progressindicator.DeterminateDrawable
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import java.util.*
class ServiceButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
......@@ -59,7 +63,7 @@ class ServiceButton @JvmOverloads constructor(context: Context, attrs: Attribute
}
private inner class AnimatedState(@DrawableRes resId: Int,
private val onStart: BaseProgressIndicator<*>.() -> Unit = { hide() }) {
private val onStart: BaseProgressIndicator<*>.() -> Unit = { hideProgress() }) {
val icon: AnimatedVectorDrawableCompat = AnimatedVectorDrawableCompat.create(context, resId)!!.apply {
registerAnimationCallback(this@ServiceButton.callback)
}
......@@ -74,16 +78,25 @@ class ServiceButton @JvmOverloads constructor(context: Context, attrs: Attribute
private val iconStopped by lazy { AnimatedState(R.drawable.ic_service_stopped) }
private val iconConnecting by lazy {
AnimatedState(R.drawable.ic_service_connecting) {
hide()
isIndeterminate = true
show()
hideProgress()
delayedAnimation = (context as LifecycleOwner).lifecycleScope.launchWhenStarted {
delay(context.resources.getInteger(android.R.integer.config_mediumAnimTime) + 1000L)
isIndeterminate = true
show()
}
}
}
private val iconConnected by lazy {
AnimatedState(R.drawable.ic_service_connected) {
delayedAnimation?.cancel()
setProgressCompat(1, true)
}
}
private val iconConnected by lazy { AnimatedState(R.drawable.ic_service_connected) { setProgressCompat(1, true) } }
private val iconStopping by lazy { AnimatedState(R.drawable.ic_service_stopping) }
private val animationQueue = ArrayDeque<AnimatedState>()
private var checked = false
private var delayedAnimation: Job? = null
private lateinit var progress: BaseProgressIndicator<*>
fun initProgress(progress: BaseProgressIndicator<*>) {
this.progress = progress
......@@ -94,6 +107,11 @@ class ServiceButton @JvmOverloads constructor(context: Context, attrs: Attribute
if (!canceled) progress.hide()
}
private fun hideProgress() {
delayedAnimation?.cancel()
progress.hide()
}
override fun onCreateDrawableState(extraSpace: Int): IntArray {
val drawableState = super.onCreateDrawableState(extraSpace + 1)
if (checked) View.mergeDrawableStates(drawableState, intArrayOf(android.R.attr.state_checked))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment