summaryrefslogtreecommitdiffhomepage
path: root/android/app
diff options
context:
space:
mode:
authorDavid Göransson <david.goransson90@gmail.com>2024-02-28 11:22:57 +0100
committerDavid Göransson <david.goransson@mullvad.net>2024-03-11 15:56:57 +0100
commitca39b7e1e06f6d6318781e441bc3131514f31cfe (patch)
treec29bb1d30fca89fb4f5f1817ec0e6af2f04a598d /android/app
parent06f139fd53187cc972d9376233748f2869d6bca4 (diff)
downloadmullvadvpn-ca39b7e1e06f6d6318781e441bc3131514f31cfe.tar.xz
mullvadvpn-ca39b7e1e06f6d6318781e441bc3131514f31cfe.zip
Rework side effect subscription for SplashScreen
Diffstat (limited to 'android/app')
-rw-r--r--android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/SplashScreen.kt44
-rw-r--r--android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/SplashViewModel.kt28
2 files changed, 26 insertions, 46 deletions
diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/SplashScreen.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/SplashScreen.kt
index 496cf67232..2a024b7a0a 100644
--- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/SplashScreen.kt
+++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/SplashScreen.kt
@@ -1,6 +1,5 @@
package net.mullvad.mullvadvpn.compose.screen
-import android.window.SplashScreen
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
@@ -13,7 +12,6 @@ import androidx.compose.foundation.layout.size
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
-import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.compositeOver
@@ -54,37 +52,31 @@ private fun PreviewLoadingScreen() {
fun Splash(navigator: DestinationsNavigator) {
val viewModel: SplashViewModel = koinViewModel()
- LaunchedEffect(Unit) {
- viewModel.uiSideEffect.collect {
- when (it) {
- SplashUiSideEffect.NavigateToConnect -> {
- navigator.navigate(ConnectDestination) {
- popUpTo(NavGraphs.root) { inclusive = true }
- }
+ // We use CollectSideEffectWithLifecycle to re-evaluate the splash decision if the user
+ // navigates away from the app to the resume before we leave the splash screen
+ CollectSideEffectWithLifecycle(viewModel.uiSideEffect) {
+ when (it) {
+ SplashUiSideEffect.NavigateToConnect ->
+ navigator.navigate(ConnectDestination) {
+ popUpTo(NavGraphs.root) { inclusive = true }
}
- SplashUiSideEffect.NavigateToLogin -> {
- navigator.navigate(LoginDestination()) {
- popUpTo(NavGraphs.root) { inclusive = true }
- }
+ SplashUiSideEffect.NavigateToLogin ->
+ navigator.navigate(LoginDestination()) {
+ popUpTo(NavGraphs.root) { inclusive = true }
}
- SplashUiSideEffect.NavigateToPrivacyDisclaimer -> {
- navigator.navigate(PrivacyDisclaimerDestination) { popUpTo(NavGraphs.root) {} }
+ SplashUiSideEffect.NavigateToPrivacyDisclaimer ->
+ navigator.navigate(PrivacyDisclaimerDestination) { popUpTo(NavGraphs.root) {} }
+ SplashUiSideEffect.NavigateToRevoked ->
+ navigator.navigate(DeviceRevokedDestination) {
+ popUpTo(NavGraphs.root) { inclusive = true }
}
- SplashUiSideEffect.NavigateToRevoked -> {
- navigator.navigate(DeviceRevokedDestination) {
- popUpTo(NavGraphs.root) { inclusive = true }
- }
+ SplashUiSideEffect.NavigateToOutOfTime ->
+ navigator.navigate(OutOfTimeDestination) {
+ popUpTo(NavGraphs.root) { inclusive = true }
}
- SplashUiSideEffect.NavigateToOutOfTime ->
- navigator.navigate(OutOfTimeDestination) {
- popUpTo(NavGraphs.root) { inclusive = true }
- }
- }
}
}
- LaunchedEffect(Unit) { viewModel.start() }
-
SplashScreen()
}
diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/SplashViewModel.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/SplashViewModel.kt
index 1a7937e9bf..83442059da 100644
--- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/SplashViewModel.kt
+++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/SplashViewModel.kt
@@ -3,45 +3,33 @@ package net.mullvad.mullvadvpn.viewmodel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.async
-import kotlinx.coroutines.channels.BufferOverflow
-import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
+import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
-import kotlinx.coroutines.flow.receiveAsFlow
-import kotlinx.coroutines.launch
import kotlinx.coroutines.selects.onTimeout
import kotlinx.coroutines.selects.select
import net.mullvad.mullvadvpn.constant.ACCOUNT_EXPIRY_TIMEOUT_MS
-import net.mullvad.mullvadvpn.lib.ipc.Event
-import net.mullvad.mullvadvpn.lib.ipc.MessageHandler
-import net.mullvad.mullvadvpn.lib.ipc.events
import net.mullvad.mullvadvpn.model.AccountAndDevice
import net.mullvad.mullvadvpn.model.AccountExpiry
import net.mullvad.mullvadvpn.model.DeviceState
+import net.mullvad.mullvadvpn.repository.AccountRepository
import net.mullvad.mullvadvpn.repository.DeviceRepository
import net.mullvad.mullvadvpn.repository.PrivacyDisclaimerRepository
class SplashViewModel(
private val privacyDisclaimerRepository: PrivacyDisclaimerRepository,
private val deviceRepository: DeviceRepository,
- private val messageHandler: MessageHandler,
+ private val accountRepository: AccountRepository,
) : ViewModel() {
- private val _uiSideEffect = Channel<SplashUiSideEffect>(1, BufferOverflow.DROP_OLDEST)
- val uiSideEffect = _uiSideEffect.receiveAsFlow()
+ val uiSideEffect = flow { emit(getStartDestination()) }
- fun start() {
- viewModelScope.launch {
- if (privacyDisclaimerRepository.hasAcceptedPrivacyDisclosure()) {
- _uiSideEffect.send(getStartDestination())
- } else {
- _uiSideEffect.send(SplashUiSideEffect.NavigateToPrivacyDisclaimer)
- }
+ private suspend fun getStartDestination(): SplashUiSideEffect {
+ if (!privacyDisclaimerRepository.hasAcceptedPrivacyDisclosure()) {
+ return SplashUiSideEffect.NavigateToPrivacyDisclaimer
}
- }
- private suspend fun getStartDestination(): SplashUiSideEffect {
val deviceState =
deviceRepository.deviceState
.map {
@@ -68,7 +56,7 @@ class SplashViewModel(
private suspend fun getLoggedInStartDestination(): SplashUiSideEffect {
val expiry =
viewModelScope.async {
- messageHandler.events<Event.AccountExpiryEvent>().map { it.expiry }.first()
+ accountRepository.accountExpiryState.first { it !is AccountExpiry.Missing }
}
val accountExpiry = select {