mirror of
https://github.com/wgtunnel/android.git
synced 2026-06-02 00:29:08 +02:00
fix: tile cleanup
This commit is contained in:
+12
-7
@@ -6,9 +6,9 @@ import com.zaneschepke.wireguardautotunnel.core.orchestration.AutoTunnelCoordina
|
||||
import com.zaneschepke.wireguardautotunnel.core.service.autotunnel.AutoTunnelStateHolder
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.cancelChildren
|
||||
import kotlinx.coroutines.launch
|
||||
import org.koin.android.ext.android.inject
|
||||
|
||||
@@ -19,6 +19,8 @@ class AutoTunnelControlTile : TileService() {
|
||||
|
||||
private val tileScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
|
||||
|
||||
private var observerJob: Job? = null
|
||||
|
||||
override fun onDestroy() {
|
||||
tileScope.cancel()
|
||||
super.onDestroy()
|
||||
@@ -30,17 +32,18 @@ class AutoTunnelControlTile : TileService() {
|
||||
startObserving()
|
||||
}
|
||||
|
||||
override fun onStopListening() {
|
||||
super.onStopListening()
|
||||
observerJob?.cancel()
|
||||
observerJob = null
|
||||
}
|
||||
|
||||
override fun onTileAdded() {
|
||||
super.onTileAdded()
|
||||
updateTileState()
|
||||
startObserving()
|
||||
}
|
||||
|
||||
override fun onStopListening() {
|
||||
super.onStopListening()
|
||||
tileScope.coroutineContext.cancelChildren()
|
||||
}
|
||||
|
||||
override fun onClick() {
|
||||
unlockAndRun { tileScope.launch { autoTunnelCoordinator.toggle() } }
|
||||
}
|
||||
@@ -51,7 +54,9 @@ class AutoTunnelControlTile : TileService() {
|
||||
}
|
||||
|
||||
private fun startObserving() {
|
||||
tileScope.launch {
|
||||
if (observerJob?.isActive == true) return
|
||||
|
||||
observerJob = tileScope.launch {
|
||||
autoTunnelStateHolder.active.collect { active ->
|
||||
if (active) setActive() else setInactive()
|
||||
}
|
||||
|
||||
+6
-1
@@ -7,6 +7,7 @@ import com.zaneschepke.wireguardautotunnel.core.orchestration.TunnelCoordinator
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.flow.distinctUntilChangedBy
|
||||
@@ -19,6 +20,8 @@ class TunnelControlTile : TileService() {
|
||||
private val tunnelsRepository: TunnelRepository by inject()
|
||||
private val tunnelCoordinator: TunnelCoordinator by inject()
|
||||
|
||||
private var collectionJob: Job? = null
|
||||
|
||||
private val tileScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
|
||||
|
||||
@Volatile private var observing = false
|
||||
@@ -44,7 +47,7 @@ class TunnelControlTile : TileService() {
|
||||
if (observing) return
|
||||
observing = true
|
||||
|
||||
tileScope.launch {
|
||||
collectionJob = tileScope.launch {
|
||||
val tunnels = withContext(Dispatchers.IO) { tunnelsRepository.getAll() }
|
||||
|
||||
tunnelCoordinator.backendStatus
|
||||
@@ -71,6 +74,8 @@ class TunnelControlTile : TileService() {
|
||||
override fun onStopListening() {
|
||||
super.onStopListening()
|
||||
observing = false
|
||||
collectionJob?.cancel()
|
||||
collectionJob = null
|
||||
}
|
||||
|
||||
override fun onClick() {
|
||||
|
||||
Reference in New Issue
Block a user