fix: tunnel control tile sync

closes #442
This commit is contained in:
Zane Schepke 2024-11-22 22:07:41 -05:00
parent 63d46c1817
commit b15cdbce7c
3 changed files with 9 additions and 13 deletions

View File

@ -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)
}
@ -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<TunnelState> {
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 {

View File

@ -74,7 +74,7 @@ constructor(
val configurationChange = _configurationChange.asStateFlow()
init {
viewModelScope.launch(ioDispatcher) {
viewModelScope.launch {
initPin()
initAutoTunnel()
initTunnel()

View File

@ -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 {