Skip to content

Commit

Permalink
Logout user when "Go to login" is clicked.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa authored and Pururun committed Jan 4, 2024
1 parent 85df7eb commit 3406902
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
Expand All @@ -34,6 +34,7 @@ import net.mullvad.mullvadvpn.compose.destinations.SettingsDestination
import net.mullvad.mullvadvpn.compose.state.DeviceRevokedUiState
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens
import net.mullvad.mullvadvpn.viewmodel.DeviceRevokedSideEffect
import net.mullvad.mullvadvpn.viewmodel.DeviceRevokedViewModel
import org.koin.androidx.compose.koinViewModel

Expand All @@ -49,15 +50,24 @@ fun DeviceRevoked(navigator: DestinationsNavigator) {
val viewModel = koinViewModel<DeviceRevokedViewModel>()

val state by viewModel.uiState.collectAsState()

LaunchedEffect(Unit) {
viewModel.uiSideEffect.collect { sideEffect ->
when (sideEffect) {
DeviceRevokedSideEffect.NavigateToLogin -> {
navigator.navigate(LoginDestination()) {
launchSingleTop = true
popUpTo(NavGraphs.root) { inclusive = true }
}
}
}
}
}

DeviceRevokedScreen(
state = state,
onSettingsClicked = { navigator.navigate(SettingsDestination) { launchSingleTop = true } },
onGoToLoginClicked = {
navigator.navigate(LoginDestination(null)) {
launchSingleTop = true
popUpTo(NavGraphs.root) { inclusive = true }
}
}
onGoToLoginClicked = viewModel::onGoToLoginClicked
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package net.mullvad.mullvadvpn.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.compose.state.DeviceRevokedUiState
import net.mullvad.mullvadvpn.repository.AccountRepository
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager
Expand Down Expand Up @@ -42,12 +47,21 @@ class DeviceRevokedViewModel(
initialValue = DeviceRevokedUiState.UNKNOWN
)

private val _uiSideEffect = Channel<DeviceRevokedSideEffect>(1, BufferOverflow.DROP_OLDEST)
val uiSideEffect = _uiSideEffect.receiveAsFlow()

fun onGoToLoginClicked() {
serviceConnectionManager.connectionProxy()?.let { proxy ->
if (proxy.state.isSecured()) {
proxy.disconnect()
}
accountRepository.logout()
}
accountRepository.logout()

viewModelScope.launch { _uiSideEffect.send(DeviceRevokedSideEffect.NavigateToLogin) }
}
}

sealed interface DeviceRevokedSideEffect {
data object NavigateToLogin : DeviceRevokedSideEffect
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import net.mullvad.mullvadvpn.compose.state.DeviceRevokedUiState
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
import net.mullvad.mullvadvpn.model.TunnelState
import net.mullvad.mullvadvpn.repository.AccountRepository
import net.mullvad.mullvadvpn.ui.serviceconnection.ConnectionProxy
Expand All @@ -26,9 +27,11 @@ import net.mullvad.talpid.util.EventNotifier
import net.mullvad.talpid.util.callbackFlowFromSubscription
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class DeviceRevokedViewModelTest {
@get:Rule val testCoroutineRule = TestCoroutineRule()

@MockK private lateinit var mockedAccountRepository: AccountRepository

Expand Down

0 comments on commit 3406902

Please sign in to comment.