-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea24fa3
commit cf938f9
Showing
2 changed files
with
219 additions
and
244 deletions.
There are no files selected for viewing
196 changes: 196 additions & 0 deletions
196
android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/AccountScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
package net.mullvad.mullvadvpn.compose.screen | ||
|
||
import androidx.compose.animation.animateContentSize | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.wrapContentHeight | ||
import androidx.compose.material3.ButtonDefaults | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
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.platform.LocalContext | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import me.onebone.toolbar.ScrollStrategy | ||
import me.onebone.toolbar.rememberCollapsingToolbarScaffoldState | ||
import net.mullvad.mullvadvpn.R | ||
import net.mullvad.mullvadvpn.compose.button.ActionButton | ||
import net.mullvad.mullvadvpn.compose.component.CollapsableAwareToolbarScaffold | ||
import net.mullvad.mullvadvpn.compose.component.CollapsingTopBar | ||
import net.mullvad.mullvadvpn.compose.component.CopyableInformationView | ||
import net.mullvad.mullvadvpn.compose.component.InformationView | ||
import net.mullvad.mullvadvpn.compose.component.MissingPolicy | ||
import net.mullvad.mullvadvpn.compose.state.AccountUiState | ||
import net.mullvad.mullvadvpn.compose.theme.Dimens | ||
import net.mullvad.mullvadvpn.ui.extension.openAccountPageInBrowser | ||
import net.mullvad.mullvadvpn.viewmodel.AccountViewModel | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Preview | ||
@Composable | ||
private fun PreviewAccountScreen() { | ||
AccountScreen( | ||
uiState = | ||
AccountUiState( | ||
_deviceName = "Test Name", | ||
accountNumber = "1234123412341234", | ||
accountExpiry = null | ||
), | ||
viewActionSharedFlow = MutableSharedFlow<AccountViewModel.ViewAction>().asSharedFlow(), | ||
) | ||
} | ||
|
||
@ExperimentalMaterial3Api | ||
@Composable | ||
fun AccountScreen( | ||
uiState: AccountUiState, | ||
viewActionSharedFlow: SharedFlow<AccountViewModel.ViewAction>, | ||
onRedeemVoucherClick: () -> Unit = {}, | ||
onManageAccountClick: () -> Unit = {}, | ||
onLogoutClick: () -> Unit = {}, | ||
onBackClick: () -> Unit = {} | ||
) { | ||
val context = LocalContext.current | ||
val state = rememberCollapsingToolbarScaffoldState() | ||
val progress = state.toolbarState.progress | ||
|
||
CollapsableAwareToolbarScaffold( | ||
backgroundColor = MaterialTheme.colorScheme.background, | ||
modifier = Modifier.fillMaxSize(), | ||
state = state, | ||
scrollStrategy = ScrollStrategy.ExitUntilCollapsed, | ||
isEnabledWhenCollapsable = true, | ||
toolbar = { | ||
val scaffoldModifier = | ||
Modifier.road( | ||
whenCollapsed = Alignment.TopCenter, | ||
whenExpanded = Alignment.BottomStart | ||
) | ||
CollapsingTopBar( | ||
backgroundColor = MaterialTheme.colorScheme.secondary, | ||
onBackClicked = { onBackClick() }, | ||
title = stringResource(id = R.string.settings_account), | ||
progress = progress, | ||
modifier = scaffoldModifier, | ||
backTitle = String(), | ||
shouldRotateBackButtonDown = true | ||
) | ||
}, | ||
) { | ||
LaunchedEffect(Unit) { | ||
viewActionSharedFlow.distinctUntilChanged().collect { viewAction -> | ||
if (viewAction is AccountViewModel.ViewAction.OpenAccountView) { | ||
context.openAccountPageInBrowser(viewAction.token) | ||
} | ||
} | ||
} | ||
Column( | ||
verticalArrangement = Arrangement.Bottom, | ||
horizontalAlignment = Alignment.Start, | ||
modifier = | ||
Modifier.background(MaterialTheme.colorScheme.background) | ||
.fillMaxWidth() | ||
.wrapContentHeight() | ||
.animateContentSize() | ||
) { | ||
Text( | ||
style = MaterialTheme.typography.labelMedium, | ||
text = stringResource(id = R.string.device_name), | ||
modifier = Modifier.padding(start = Dimens.sideMargin, end = Dimens.sideMargin) | ||
) | ||
|
||
InformationView(content = uiState.deviceName, whenMissing = MissingPolicy.SHOW_SPINNER) | ||
|
||
Text( | ||
style = MaterialTheme.typography.labelMedium, | ||
text = stringResource(id = R.string.account_number), | ||
modifier = | ||
Modifier.padding( | ||
start = Dimens.sideMargin, | ||
end = Dimens.sideMargin, | ||
top = Dimens.smallPadding | ||
) | ||
) | ||
|
||
CopyableInformationView(content = uiState.accountNumber) | ||
|
||
Text( | ||
style = MaterialTheme.typography.labelMedium, | ||
text = stringResource(id = R.string.paid_until), | ||
modifier = Modifier.padding(start = Dimens.sideMargin, end = Dimens.sideMargin) | ||
) | ||
|
||
InformationView( | ||
content = uiState.expiryString, | ||
whenMissing = MissingPolicy.SHOW_SPINNER | ||
) | ||
|
||
Spacer(modifier = Modifier.weight(1.0f)) | ||
|
||
ActionButton( | ||
text = stringResource(id = R.string.manage_account), | ||
onClick = { onManageAccountClick() }, | ||
modifier = | ||
Modifier.padding( | ||
start = Dimens.sideMargin, | ||
end = Dimens.sideMargin, | ||
bottom = Dimens.screenVerticalMargin | ||
), | ||
colors = | ||
ButtonDefaults.buttonColors( | ||
contentColor = MaterialTheme.colorScheme.onPrimary, | ||
containerColor = MaterialTheme.colorScheme.surface | ||
) | ||
) | ||
|
||
ActionButton( | ||
text = stringResource(id = R.string.redeem_voucher), | ||
onClick = onRedeemVoucherClick, | ||
modifier = | ||
Modifier.padding( | ||
start = Dimens.sideMargin, | ||
end = Dimens.sideMargin, | ||
bottom = Dimens.screenVerticalMargin | ||
), | ||
colors = | ||
ButtonDefaults.buttonColors( | ||
contentColor = MaterialTheme.colorScheme.onPrimary, | ||
containerColor = MaterialTheme.colorScheme.surface | ||
) | ||
) | ||
|
||
ActionButton( | ||
text = stringResource(id = R.string.log_out), | ||
onClick = onLogoutClick, | ||
modifier = | ||
Modifier.padding( | ||
start = Dimens.sideMargin, | ||
end = Dimens.sideMargin, | ||
bottom = Dimens.screenVerticalMargin | ||
), | ||
colors = | ||
ButtonDefaults.buttonColors( | ||
contentColor = MaterialTheme.colorScheme.onPrimary, | ||
containerColor = MaterialTheme.colorScheme.error | ||
) | ||
) | ||
|
||
Spacer(modifier = Modifier.height(50.dp)) | ||
} | ||
} | ||
} |
Oops, something went wrong.