Skip to content

Commit bfe5a54

Browse files
Update UI
1 parent ae538c4 commit bfe5a54

1 file changed

Lines changed: 26 additions & 22 deletions

File tree

  • app/src/main/java/io/nekohasekai/sagernet/widget

app/src/main/java/io/nekohasekai/sagernet/widget/StatsBar.kt

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import kotlinx.coroutines.Dispatchers
2020
import kotlinx.coroutines.delay
2121
import kotlinx.coroutines.launch
2222
import com.neko.marquee.text.AutoMarqueeTextView
23+
import androidx.lifecycle.LifecycleOwner
2324

2425
class 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

Comments
 (0)