Skip to content

Commit

Permalink
Propagate clear account history errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Jul 31, 2024
1 parent bb9481e commit a771eac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
Expand All @@ -38,6 +39,7 @@ import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusProperties
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
Expand Down Expand Up @@ -74,6 +76,7 @@ import net.mullvad.mullvadvpn.compose.textfield.mullvadWhiteTextFieldColors
import net.mullvad.mullvadvpn.compose.transitions.LoginTransition
import net.mullvad.mullvadvpn.compose.util.CollectSideEffectWithLifecycle
import net.mullvad.mullvadvpn.compose.util.accountNumberVisualTransformation
import net.mullvad.mullvadvpn.compose.util.showSnackbarImmediately
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens
import net.mullvad.mullvadvpn.lib.theme.color.AlphaTopBar
Expand Down Expand Up @@ -128,6 +131,8 @@ fun Login(
}
}

val context = LocalContext.current
val snackbarHostState = remember { SnackbarHostState() }
CollectSideEffectWithLifecycle(vm.uiSideEffect) {
when (it) {
LoginUiSideEffect.NavigateToWelcome ->
Expand All @@ -149,28 +154,35 @@ fun Login(
launchSingleTop = true
popUpTo(NavGraphs.root) { inclusive = true }
}
LoginUiSideEffect.GenericError ->
snackbarHostState.showSnackbarImmediately(
message = context.getString(R.string.error_occurred),
)
}
}
LoginScreen(
state,
vm::login,
vm::createAccount,
vm::clearAccountHistory,
vm::onAccountNumberChange,
dropUnlessResumed { navigator.navigate(SettingsDestination) }
state = state,
snackbarHostState = snackbarHostState,
onLoginClick = vm::login,
onCreateAccountClick = vm::createAccount,
onDeleteHistoryClick = vm::clearAccountHistory,
onAccountNumberChange = vm::onAccountNumberChange,
onSettingsClick = dropUnlessResumed { navigator.navigate(SettingsDestination) }
)
}

@Composable
private fun LoginScreen(
state: LoginUiState,
snackbarHostState: SnackbarHostState = SnackbarHostState(),
onLoginClick: (String) -> Unit = {},
onCreateAccountClick: () -> Unit = {},
onDeleteHistoryClick: () -> Unit = {},
onAccountNumberChange: (String) -> Unit = {},
onSettingsClick: () -> Unit = {},
) {
ScaffoldWithTopBar(
snackbarHostState = snackbarHostState,
topBarColor = MaterialTheme.colorScheme.primary,
iconTintColor = MaterialTheme.colorScheme.onPrimary.copy(alpha = AlphaTopBar),
onSettingsClicked = onSettingsClick,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ sealed interface LoginUiSideEffect {
data object NavigateToOutOfTime : LoginUiSideEffect

data class TooManyDevices(val accountNumber: AccountNumber) : LoginUiSideEffect

data object GenericError : LoginUiSideEffect
}

class LoginViewModel(
Expand Down Expand Up @@ -78,9 +80,15 @@ class LoginViewModel(

fun clearAccountHistory() =
viewModelScope.launch {
accountRepository.clearAccountHistory()
_mutableAccountHistory.update { null }
_mutableAccountHistory.update { accountRepository.fetchAccountHistory() }
accountRepository
.clearAccountHistory()
.fold(
{ _uiSideEffect.send(LoginUiSideEffect.GenericError) },
{
_mutableAccountHistory.update { null }
_mutableAccountHistory.update { accountRepository.fetchAccountHistory() }
}
)
}

fun createAccount() {
Expand Down

0 comments on commit a771eac

Please sign in to comment.