Skip to content

Commit

Permalink
fix: tunnel control tile sync
Browse files Browse the repository at this point in the history
closes #442
  • Loading branch information
zaneschepke committed Nov 23, 2024
1 parent 63d46c1 commit b15cdbc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ constructor(

override suspend fun startTunnel(tunnelConfig: TunnelConfig, background: Boolean): Result<TunnelState> {
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)
}
Expand All @@ -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()
Expand All @@ -115,7 +115,7 @@ constructor(
return withContext(ioDispatcher) {
onBeforeStop(tunnelConfig)
setState(tunnelConfig, TunnelState.DOWN).onSuccess {
emitTunnelState(it)
updateTunnelState(it)
}.onFailure {
Timber.e(it)
onStopFailed()
Expand All @@ -136,7 +136,7 @@ constructor(
private suspend fun toggleTunnel(tunnelConfig: TunnelConfig): Result<TunnelState> {
return withContext(ioDispatcher) {
setState(tunnelConfig, TunnelState.TOGGLE).onSuccess {
emitTunnelState(it)
updateTunnelState(it)
resetBackendStatistics()
}.onFailure {
Timber.e(it)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ constructor(
val configurationChange = _configurationChange.asStateFlow()

init {
viewModelScope.launch(ioDispatcher) {
viewModelScope.launch {
initPin()
initAutoTunnel()
initTunnel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b15cdbc

Please sign in to comment.