-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Alexandre Ferris <[email protected]>
- Loading branch information
1 parent
6e6aabc
commit f194001
Showing
6 changed files
with
384 additions
and
6 deletions.
There are no files selected for viewing
164 changes: 164 additions & 0 deletions
164
app/src/main/kotlin/com/wire/android/ui/home/appLock/SetLockCodeScreen.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,164 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.android.ui.home.appLock | ||
|
||
import androidx.compose.foundation.ScrollState | ||
import androidx.compose.foundation.interaction.MutableInteractionSource | ||
import androidx.compose.foundation.layout.Box | ||
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.padding | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.verticalScroll | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.ExperimentalComposeUiApi | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.testTag | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.semantics.semantics | ||
import androidx.compose.ui.semantics.testTagsAsResourceId | ||
import androidx.compose.ui.text.input.ImeAction | ||
import androidx.compose.ui.text.input.TextFieldValue | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import com.ramcosta.composedestinations.annotation.Destination | ||
import com.ramcosta.composedestinations.annotation.RootNavGraph | ||
import com.wire.android.R | ||
import com.wire.android.navigation.Navigator | ||
import com.wire.android.ui.common.button.WireButtonState | ||
import com.wire.android.ui.common.button.WirePrimaryButton | ||
import com.wire.android.ui.common.dimensions | ||
import com.wire.android.ui.common.rememberBottomBarElevationState | ||
import com.wire.android.ui.common.scaffold.WireScaffold | ||
import com.wire.android.ui.common.textfield.WirePasswordTextField | ||
import com.wire.android.ui.common.textfield.WireTextFieldState | ||
import com.wire.android.ui.common.topappbar.WireCenterAlignedTopAppBar | ||
import com.wire.android.ui.theme.wireColorScheme | ||
import com.wire.android.ui.theme.wireDimensions | ||
|
||
@RootNavGraph | ||
@Destination | ||
@Composable | ||
fun SetLockCodeScreen( | ||
viewModel: SetLockScreenViewModel = hiltViewModel(), | ||
navigator: Navigator | ||
) { | ||
SetLockCodeScreenContent( | ||
navigator = navigator, | ||
state = viewModel.state, | ||
scrollState = rememberScrollState(), | ||
onPasswordChanged = viewModel::onPasswordChanged, | ||
onBackPress = navigator::navigateBack, | ||
onContinue = viewModel::onContinue | ||
) | ||
} | ||
|
||
@OptIn(ExperimentalComposeUiApi::class) | ||
@Composable | ||
fun SetLockCodeScreenContent( | ||
navigator: Navigator, | ||
state: SetLockCodeViewState, | ||
scrollState: ScrollState, | ||
onPasswordChanged: (TextFieldValue) -> Unit, | ||
onBackPress: () -> Unit = {}, | ||
onContinue: () -> Unit | ||
) { | ||
LaunchedEffect(state.done) { | ||
if (state.done) { | ||
navigator.navigateBack() | ||
} | ||
} | ||
|
||
WireScaffold(topBar = { | ||
WireCenterAlignedTopAppBar( | ||
onNavigationPressed = onBackPress, | ||
elevation = dimensions().spacing0x, | ||
title = stringResource(id = R.string.settings_privacy_settings_label) | ||
) | ||
}) { internalPadding -> | ||
Column( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.padding(internalPadding) | ||
) { | ||
Column( | ||
modifier = Modifier | ||
.weight(weight = 1f, fill = true) | ||
.verticalScroll(scrollState) | ||
.padding(MaterialTheme.wireDimensions.spacing16x) | ||
.semantics { | ||
testTagsAsResourceId = true | ||
} | ||
) { | ||
WirePasswordTextField( | ||
value = state.password, | ||
onValueChange = onPasswordChanged, | ||
labelMandatoryIcon = true, | ||
descriptionText = stringResource(R.string.create_account_details_password_description), | ||
imeAction = ImeAction.Done, | ||
modifier = Modifier | ||
.testTag("password"), | ||
state = WireTextFieldState.Default, | ||
autofill = false | ||
) | ||
Spacer(modifier = Modifier.weight(1f)) | ||
} | ||
|
||
Surface( | ||
shadowElevation = scrollState.rememberBottomBarElevationState().value, | ||
color = MaterialTheme.wireColorScheme.background, | ||
modifier = Modifier.semantics { | ||
testTagsAsResourceId = true | ||
} | ||
) { | ||
Box(modifier = Modifier.padding(MaterialTheme.wireDimensions.spacing16x)) { | ||
val enabled = state.password.text.isNotBlank() && state.isPasswordValid | ||
ContinueButton( | ||
enabled = enabled, | ||
onContinue = onContinue | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun ContinueButton( | ||
modifier: Modifier = Modifier.fillMaxWidth(), | ||
enabled: Boolean, | ||
onContinue: () -> Unit | ||
) { | ||
val interactionSource = remember { MutableInteractionSource() } | ||
Column(modifier = modifier) { | ||
WirePrimaryButton( | ||
text = stringResource(R.string.label_continue), | ||
onClick = onContinue, | ||
state = if (enabled) WireButtonState.Default else WireButtonState.Disabled, | ||
interactionSource = interactionSource, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.testTag("continue_button") | ||
) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/kotlin/com/wire/android/ui/home/appLock/SetLockCodeViewState.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,27 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.android.ui.home.appLock | ||
|
||
import androidx.compose.ui.text.input.TextFieldValue | ||
|
||
data class SetLockCodeViewState( | ||
val continueEnabled: Boolean = false, | ||
val password: TextFieldValue = TextFieldValue(), | ||
val isPasswordValid: Boolean = false, | ||
val done: Boolean = false | ||
) |
64 changes: 64 additions & 0 deletions
64
app/src/main/kotlin/com/wire/android/ui/home/appLock/SetLockScreenViewModel.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,64 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.android.ui.home.appLock | ||
|
||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.text.input.TextFieldValue | ||
import androidx.lifecycle.ViewModel | ||
import com.wire.kalium.logic.feature.auth.ValidatePasswordUseCase | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class SetLockScreenViewModel @Inject constructor( | ||
private val validatePassword: ValidatePasswordUseCase | ||
) : ViewModel() { | ||
|
||
var state: SetLockCodeViewState by mutableStateOf(SetLockCodeViewState()) | ||
private set | ||
|
||
fun onPasswordChanged(password: TextFieldValue) { | ||
state = state.copy( | ||
password = password | ||
) | ||
state = if (validatePassword(password.text)) { | ||
state.copy( | ||
continueEnabled = true, | ||
isPasswordValid = true | ||
) | ||
} else { | ||
state.copy( | ||
isPasswordValid = false | ||
) | ||
} | ||
} | ||
|
||
fun onContinue() { | ||
state = state.copy(continueEnabled = false) | ||
// the continue button is enabled iff the password is valid | ||
// this check is for safety only | ||
state = if (!validatePassword(state.password.text)) { | ||
state.copy(isPasswordValid = false) | ||
} else { | ||
// TODO: store password in secure storage | ||
state.copy(done = true) | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.