@@ -25,12 +25,9 @@ import io.matthewnelson.sampleapp.R
2525import io.matthewnelson.sampleapp.topl_android.MyEventBroadcaster
2626import io.matthewnelson.sampleapp.ui.MainActivity
2727import io.matthewnelson.sampleapp.ui.fragments.settings.library.LibraryPrefs
28- import io.matthewnelson.topl_core_base.BaseConsts
28+ import io.matthewnelson.topl_core_base.BaseConsts.TorState
2929import io.matthewnelson.topl_service.TorServiceController
30- import kotlinx.coroutines.CoroutineScope
31- import kotlinx.coroutines.Dispatchers
32- import kotlinx.coroutines.delay
33- import kotlinx.coroutines.launch
30+ import kotlinx.coroutines.*
3431import kotlin.system.exitProcess
3532
3633class DashboardFragment : Fragment () {
@@ -123,23 +120,36 @@ class DashboardFragment : Fragment() {
123120 }
124121 }
125122
123+ private var killAppJob: Job ? = null
124+
126125 private fun initButtons (context : Context , owner : LifecycleOwner ) {
127126 buttonAppRestart.setOnClickListener {
127+ buttonAppRestart.visibility = View .GONE
128+
128129 Toast .makeText(context, " Killing Application" , Toast .LENGTH_LONG ).show()
129130 val prefs = Prefs .createUnencrypted(App .PREFS_NAME , context)
130131
131132 TorServiceController .appEventBroadcaster?.let {
132133 (it as MyEventBroadcaster ).liveTorState.observe(owner, Observer { data ->
133134 if (data != null ) {
134- var delayLength = 500L + LibraryPrefs .getControllerStopDelaySetting(prefs)
135- if (data.state == BaseConsts .TorState .OFF )
136- delayLength = 200L
137-
138- TorServiceController .stopTor()
139- CoroutineScope (Dispatchers .Main ).launch {
140- delay(delayLength)
141- killApplication(context)
135+
136+ when (data.state) {
137+ TorState .ON ,
138+ TorState .STARTING -> {
139+ killAppJob?.cancel()
140+ TorServiceController .stopTor()
141+ }
142+ TorState .OFF -> {
143+ if (killAppJob?.isActive == true )
144+ killAppJob?.cancel()
145+
146+ killAppJob = CoroutineScope (Dispatchers .Main ).launch {
147+ delay(1000L + LibraryPrefs .getControllerStopDelaySetting(prefs))
148+ killApplication(context)
149+ }
150+ }
142151 }
152+
143153 }
144154 })
145155 }
0 commit comments