Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Device revoked on connect screen #5604

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import net.mullvad.mullvadvpn.compose.component.ScaffoldWithTopBarAndDeviceName
import net.mullvad.mullvadvpn.compose.component.drawVerticalScrollbar
import net.mullvad.mullvadvpn.compose.component.notificationbanner.NotificationBanner
import net.mullvad.mullvadvpn.compose.destinations.AccountDestination
import net.mullvad.mullvadvpn.compose.destinations.DeviceRevokedDestination
import net.mullvad.mullvadvpn.compose.destinations.OutOfTimeDestination
import net.mullvad.mullvadvpn.compose.destinations.SelectLocationDestination
import net.mullvad.mullvadvpn.compose.destinations.SettingsDestination
Expand Down Expand Up @@ -98,6 +99,12 @@ fun Connect(navigator: DestinationsNavigator) {
popUpTo(NavGraphs.root) { inclusive = true }
}
}
ConnectViewModel.UiSideEffect.RevokedDevice -> {
navigator.navigate(DeviceRevokedDestination) {
launchSingleTop = true
popUpTo(NavGraphs.root) { inclusive = true }
}
}
}
}
}
Expand Down
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
Expand Up @@ -23,6 +23,7 @@ import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.compose.state.ConnectUiState
import net.mullvad.mullvadvpn.model.DeviceState
import net.mullvad.mullvadvpn.model.GeoIpLocation
import net.mullvad.mullvadvpn.model.TunnelState
import net.mullvad.mullvadvpn.repository.AccountRepository
Expand Down Expand Up @@ -137,11 +138,17 @@ class ConnectViewModel(

init {
viewModelScope.launch {
// This once we get isOutOfTime true we will navigate to OutOfTime view.
// When we get isOutOfTime true we will navigate to OutOfTime view.
outOfTimeUseCase.isOutOfTime().first { it == true }
_uiSideEffect.send(UiSideEffect.OutOfTime)
}

viewModelScope.launch {
// When we get a revoked DeviceState we navigate to the RevokedDevice screen.
deviceRepository.deviceState.filterIsInstance<DeviceState.Revoked>().first()
_uiSideEffect.send(UiSideEffect.RevokedDevice)
}

viewModelScope.launch {
paymentUseCase.verifyPurchases { accountRepository.fetchAccountExpiry() }
}
Expand Down Expand Up @@ -194,6 +201,8 @@ class ConnectViewModel(
data class OpenAccountManagementPageInBrowser(val token: String) : UiSideEffect

data object OutOfTime : UiSideEffect

data object RevokedDevice : UiSideEffect
}

companion object {
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