@@ -20,6 +20,7 @@ import kotlinx.coroutines.Dispatchers
2020import kotlinx.coroutines.delay
2121import kotlinx.coroutines.launch
2222import com.neko.marquee.text.AutoMarqueeTextView
23+ import androidx.lifecycle.LifecycleOwner
2324
2425class StatsBar @JvmOverloads constructor(
2526 context : Context , attrs : AttributeSet ? = null ,
@@ -82,30 +83,33 @@ class StatsBar @JvmOverloads constructor(
8283 }
8384
8485 fun changeState (state : BaseService .State ) {
85- val activity = context as MainActivity
86- fun postWhenStarted ( what : () -> Unit ) = activity.lifecycleScope.launch(Dispatchers .Main ) {
86+ val activity = context as ? LifecycleOwner ? : return
87+ activity.lifecycleScope.launch(Dispatchers .Main ) {
8788 delay(100L )
88- activity.whenStarted { what() }
89- }
90- if ((state == BaseService .State .Connected ).also { hideOnScroll = it }) {
91- postWhenStarted {
92- if (allowShow) performShow()
93- setStatus(app.getText(R .string.vpn_connected))
94- }
95- } else {
96- postWhenStarted {
97- performHide()
89+ when (state) {
90+ BaseService .State .Connected -> {
91+ hideOnScroll = true
92+ allowShow = true
93+ performShow()
94+ setStatus(context.getText(R .string.vpn_connected))
95+ }
96+
97+ else -> {
98+ hideOnScroll = false
99+ allowShow = false
100+ performHide()
101+ updateSpeed(0 , 0 )
102+ setStatus(
103+ context.getText(
104+ when (state) {
105+ BaseService .State .Connecting -> R .string.connecting
106+ BaseService .State .Stopping -> R .string.stopping
107+ else -> R .string.not_connected
108+ }
109+ )
110+ )
111+ }
98112 }
99- updateSpeed(0 , 0 )
100- setStatus(
101- context.getText(
102- when (state) {
103- BaseService .State .Connecting -> R .string.connecting
104- BaseService .State .Stopping -> R .string.stopping
105- else -> R .string.not_connected
106- }
107- )
108- )
109113 }
110114 }
111115
0 commit comments