fix: tile cleanup

This commit is contained in:
zaneschepke
2026-05-31 12:07:48 -04:00
parent 53278243e8
commit c1760fda10
2 changed files with 18 additions and 8 deletions
@@ -6,9 +6,9 @@ import com.zaneschepke.wireguardautotunnel.core.orchestration.AutoTunnelCoordina
import com.zaneschepke.wireguardautotunnel.core.service.autotunnel.AutoTunnelStateHolder import com.zaneschepke.wireguardautotunnel.core.service.autotunnel.AutoTunnelStateHolder
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject import org.koin.android.ext.android.inject
@@ -19,6 +19,8 @@ class AutoTunnelControlTile : TileService() {
private val tileScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) private val tileScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
private var observerJob: Job? = null
override fun onDestroy() { override fun onDestroy() {
tileScope.cancel() tileScope.cancel()
super.onDestroy() super.onDestroy()
@@ -30,17 +32,18 @@ class AutoTunnelControlTile : TileService() {
startObserving() startObserving()
} }
override fun onStopListening() {
super.onStopListening()
observerJob?.cancel()
observerJob = null
}
override fun onTileAdded() { override fun onTileAdded() {
super.onTileAdded() super.onTileAdded()
updateTileState() updateTileState()
startObserving() startObserving()
} }
override fun onStopListening() {
super.onStopListening()
tileScope.coroutineContext.cancelChildren()
}
override fun onClick() { override fun onClick() {
unlockAndRun { tileScope.launch { autoTunnelCoordinator.toggle() } } unlockAndRun { tileScope.launch { autoTunnelCoordinator.toggle() } }
} }
@@ -51,7 +54,9 @@ class AutoTunnelControlTile : TileService() {
} }
private fun startObserving() { private fun startObserving() {
tileScope.launch { if (observerJob?.isActive == true) return
observerJob = tileScope.launch {
autoTunnelStateHolder.active.collect { active -> autoTunnelStateHolder.active.collect { active ->
if (active) setActive() else setInactive() if (active) setActive() else setInactive()
} }
@@ -7,6 +7,7 @@ import com.zaneschepke.wireguardautotunnel.core.orchestration.TunnelCoordinator
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.distinctUntilChangedBy import kotlinx.coroutines.flow.distinctUntilChangedBy
@@ -19,6 +20,8 @@ class TunnelControlTile : TileService() {
private val tunnelsRepository: TunnelRepository by inject() private val tunnelsRepository: TunnelRepository by inject()
private val tunnelCoordinator: TunnelCoordinator by inject() private val tunnelCoordinator: TunnelCoordinator by inject()
private var collectionJob: Job? = null
private val tileScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) private val tileScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
@Volatile private var observing = false @Volatile private var observing = false
@@ -44,7 +47,7 @@ class TunnelControlTile : TileService() {
if (observing) return if (observing) return
observing = true observing = true
tileScope.launch { collectionJob = tileScope.launch {
val tunnels = withContext(Dispatchers.IO) { tunnelsRepository.getAll() } val tunnels = withContext(Dispatchers.IO) { tunnelsRepository.getAll() }
tunnelCoordinator.backendStatus tunnelCoordinator.backendStatus
@@ -71,6 +74,8 @@ class TunnelControlTile : TileService() {
override fun onStopListening() { override fun onStopListening() {
super.onStopListening() super.onStopListening()
observing = false observing = false
collectionJob?.cancel()
collectionJob = null
} }
override fun onClick() { override fun onClick() {