-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] #141 : SignUpValidationScreen + ViewModel
- Loading branch information
Showing
3 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
.../auth/src/main/java/com/wap/wapp/feature/auth/signup/validation/SignUpValidationScreen.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,102 @@ | ||
package com.wap.wapp.feature.auth.signup.validation | ||
|
||
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.padding | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.SnackbarHost | ||
import androidx.compose.material3.SnackbarHostState | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalFocusManager | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.unit.dp | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import com.wap.designsystem.WappTheme | ||
import com.wap.designsystem.component.WappButton | ||
import com.wap.designsystem.component.WappTextField | ||
import com.wap.designsystem.modifier.addFocusCleaner | ||
import com.wap.wapp.core.commmon.extensions.toSupportingText | ||
import com.wap.wapp.feature.auth.R | ||
import kotlinx.coroutines.flow.collectLatest | ||
|
||
@Composable | ||
fun SignUpValidationScreen( | ||
viewModel: SignUpValidationViewModel = hiltViewModel(), | ||
onValidationSuccess: () -> Unit, | ||
) { | ||
val code by viewModel.signUpCode.collectAsStateWithLifecycle() | ||
val isError by viewModel.isError.collectAsStateWithLifecycle() | ||
val errorSupportingText by viewModel.errorSupportingText.collectAsStateWithLifecycle() | ||
val snackBarHostState = remember { SnackbarHostState() } | ||
val focusManager = LocalFocusManager.current | ||
|
||
LaunchedEffect(true) { | ||
viewModel.signUpCodeUiState.collectLatest { | ||
when (it) { | ||
is SignUpValidationViewModel.SignUpCodeUiState.Init -> {} | ||
|
||
is SignUpValidationViewModel.SignUpCodeUiState.Success -> | ||
onValidationSuccess() | ||
|
||
is SignUpValidationViewModel.SignUpCodeUiState.Failure -> | ||
snackBarHostState.showSnackbar(it.throwable.toSupportingText()) | ||
} | ||
} | ||
} | ||
|
||
Scaffold( | ||
containerColor = WappTheme.colors.backgroundBlack, | ||
snackbarHost = { SnackbarHost(snackBarHostState) }, | ||
) { paddingValues -> | ||
Column( | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
verticalArrangement = Arrangement.Center, | ||
modifier = Modifier | ||
.padding(paddingValues) | ||
.fillMaxSize() | ||
.addFocusCleaner(focusManager), | ||
) { | ||
Text( | ||
text = stringResource(R.string.sign_up_dialog_title), | ||
style = WappTheme.typography.titleBold, | ||
color = WappTheme.colors.white, | ||
textAlign = TextAlign.Center, | ||
) | ||
|
||
Text( | ||
text = stringResource(R.string.sign_up_dialog_content), | ||
style = WappTheme.typography.captionMedium, | ||
color = WappTheme.colors.white, | ||
textAlign = TextAlign.Center, | ||
) | ||
|
||
Spacer(modifier = Modifier.padding(vertical = 8.dp)) | ||
|
||
WappTextField( | ||
value = code, | ||
onValueChanged = viewModel::setManagementCode, | ||
label = R.string.code, | ||
isError = isError, | ||
supportingText = stringResource(id = errorSupportingText), | ||
) | ||
|
||
Spacer(modifier = Modifier.padding(vertical = 8.dp)) | ||
|
||
WappButton( | ||
onClick = { viewModel.validateManagementCode() }, | ||
isEnabled = code.isNotBlank(), | ||
modifier = Modifier.padding(horizontal = 32.dp), | ||
) | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...th/src/main/java/com/wap/wapp/feature/auth/signup/validation/SignUpValidationViewModel.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,63 @@ | ||
package com.wap.wapp.feature.auth.signup.validation | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.wap.wapp.core.domain.model.CodeValidation | ||
import com.wap.wapp.core.domain.usecase.management.ValidateManagementCodeUseCase | ||
import com.wap.wapp.feature.auth.R | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class SignUpValidationViewModel @Inject constructor( | ||
private val validateManagementCodeUseCase: ValidateManagementCodeUseCase, | ||
) : ViewModel() { | ||
private val _signUpCodeUiState: MutableStateFlow<SignUpCodeUiState> = | ||
MutableStateFlow(SignUpCodeUiState.Init) | ||
val signUpCodeUiState: StateFlow<SignUpCodeUiState> = _signUpCodeUiState.asStateFlow() | ||
|
||
private val _signUpCode: MutableStateFlow<String> = MutableStateFlow("") | ||
val signUpCode: StateFlow<String> = _signUpCode.asStateFlow() | ||
|
||
private val _isError: MutableStateFlow<Boolean> = MutableStateFlow(false) | ||
val isError: StateFlow<Boolean> = _isError.asStateFlow() | ||
|
||
private val _errorSupportingText: MutableStateFlow<Int> = | ||
MutableStateFlow(R.string.sign_up_dialog_hint) | ||
val errorSupportingText: StateFlow<Int> = _errorSupportingText.asStateFlow() | ||
|
||
fun validateManagementCode() { | ||
viewModelScope.launch { | ||
validateManagementCodeUseCase(_signUpCode.value) | ||
.onSuccess { | ||
when (it) { | ||
CodeValidation.VALID -> { | ||
_signUpCodeUiState.value = SignUpCodeUiState.Success | ||
} | ||
|
||
CodeValidation.INVALID -> { | ||
_isError.value = true | ||
_errorSupportingText.value = R.string.sign_up_incorrect_code | ||
} | ||
} | ||
} | ||
.onFailure { throwable -> | ||
_signUpCodeUiState.value = SignUpCodeUiState.Failure(throwable) | ||
} | ||
} | ||
} | ||
|
||
fun setManagementCode(code: String) { | ||
_signUpCode.value = code | ||
} | ||
|
||
sealed class SignUpCodeUiState { | ||
data object Init : SignUpCodeUiState() | ||
data object Success : SignUpCodeUiState() | ||
data class Failure(val throwable: Throwable) : SignUpCodeUiState() | ||
} | ||
} |
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