From b15cdbce7c1d2fa6a1af262e0b5b41d73c4530e8 Mon Sep 17 00:00:00 2001 From: Zane Schepke Date: Fri, 22 Nov 2024 22:07:41 -0500 Subject: [PATCH] fix: tunnel control tile sync closes #442 --- .../service/tunnel/WireGuardTunnel.kt | 15 ++++++++------- .../wireguardautotunnel/ui/AppViewModel.kt | 2 +- .../wireguardautotunnel/ui/MainActivity.kt | 5 ----- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/tunnel/WireGuardTunnel.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/tunnel/WireGuardTunnel.kt index 66d51b7..b1df39a 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/tunnel/WireGuardTunnel.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/tunnel/WireGuardTunnel.kt @@ -94,7 +94,7 @@ constructor( override suspend fun startTunnel(tunnelConfig: TunnelConfig, background: Boolean): Result { return withContext(ioDispatcher) { - if (runningHandle.get() == true && tunnelConfig == vpnState.value.tunnelConfig) { + if (runningHandle.get() && tunnelConfig == vpnState.value.tunnelConfig) { Timber.w("Tunnel already running") return@withContext Result.success(vpnState.value.status) } @@ -103,7 +103,7 @@ constructor( val settings = appDataRepository.settings.getSettings() if (background || settings.isKernelEnabled) startBackgroundService() setState(tunnelConfig, TunnelState.UP).onSuccess { - emitTunnelState(it) + updateTunnelState(it) }.onFailure { Timber.e(it) onStartFailed() @@ -115,7 +115,7 @@ constructor( return withContext(ioDispatcher) { onBeforeStop(tunnelConfig) setState(tunnelConfig, TunnelState.DOWN).onSuccess { - emitTunnelState(it) + updateTunnelState(it) }.onFailure { Timber.e(it) onStopFailed() @@ -136,7 +136,7 @@ constructor( private suspend fun toggleTunnel(tunnelConfig: TunnelConfig): Result { return withContext(ioDispatcher) { setState(tunnelConfig, TunnelState.TOGGLE).onSuccess { - emitTunnelState(it) + updateTunnelState(it) resetBackendStatistics() }.onFailure { Timber.e(it) @@ -191,12 +191,13 @@ constructor( resetBackendStatistics() } - private fun emitTunnelState(state: TunnelState) { + private fun updateTunnelState(state: TunnelState) { _vpnState.tryEmit( _vpnState.value.copy( status = state, ), ) + serviceManager.requestTunnelTileUpdate() } private fun emitBackendStatistics(statistics: TunnelStatistics) { @@ -264,11 +265,11 @@ constructor( } override fun onStateChange(newState: Tunnel.State) { - emitTunnelState(TunnelState.from(newState)) + updateTunnelState(TunnelState.from(newState)) } override fun onStateChange(state: State) { - emitTunnelState(TunnelState.from(state)) + updateTunnelState(TunnelState.from(state)) } companion object { diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/AppViewModel.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/AppViewModel.kt index ed458f4..3d343e2 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/AppViewModel.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/AppViewModel.kt @@ -74,7 +74,7 @@ constructor( val configurationChange = _configurationChange.asStateFlow() init { - viewModelScope.launch(ioDispatcher) { + viewModelScope.launch { initPin() initAutoTunnel() initTunnel() diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt index 599f48e..64ca353 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt @@ -61,7 +61,6 @@ import com.zaneschepke.wireguardautotunnel.ui.theme.WireguardAutoTunnelTheme import com.zaneschepke.wireguardautotunnel.util.Constants import com.zaneschepke.wireguardautotunnel.util.LocaleUtil import com.zaneschepke.wireguardautotunnel.util.extensions.requestAutoTunnelTileServiceUpdate -import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate import dagger.hilt.android.AndroidEntryPoint import javax.inject.Inject import kotlin.system.exitProcess @@ -97,10 +96,6 @@ class MainActivity : AppCompatActivity() { val configurationChange by viewModel.configurationChange.collectAsStateWithLifecycle() val navController = rememberNavController() - LaunchedEffect(appUiState.tunnels) { - requestTunnelTileServiceStateUpdate() - } - LaunchedEffect(configurationChange) { if (configurationChange) { Intent(this@MainActivity, MainActivity::class.java).also {