-
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.
- Loading branch information
1 parent
44c3884
commit 75dba23
Showing
2 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
feature/manage/src/main/java/com/wap/wapp/feature/manage/code/ManageCodeDialog.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,104 @@ | ||
package com.wap.wapp.feature.manage.code | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.AlertDialog | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.window.DialogProperties | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import com.wap.designsystem.WappTheme | ||
import com.wap.designsystem.component.WappButton | ||
import com.wap.designsystem.component.WappTextField | ||
import com.wap.wapp.feature.manage.R | ||
import com.wap.wapp.feature.manage.code.ManagementCodeViewModel.ManagementCodeUiState | ||
import kotlinx.coroutines.flow.collectLatest | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun ManageCodeDialog( | ||
viewModel: ManagementCodeViewModel = hiltViewModel(), | ||
onDismissRequest: () -> Unit, | ||
showToast: (Throwable) -> Unit, | ||
) { | ||
LaunchedEffect(true) { | ||
viewModel.managementCodeUiState.collectLatest { | ||
when (it) { | ||
is ManagementCodeUiState.Success -> { | ||
onDismissRequest() | ||
} | ||
is ManagementCodeUiState.Failure -> { | ||
showToast(it.throwable) | ||
} | ||
is ManagementCodeUiState.Init -> { } | ||
} | ||
} | ||
} | ||
|
||
AlertDialog( | ||
onDismissRequest = { onDismissRequest() }, | ||
modifier = Modifier.padding(horizontal = 16.dp), | ||
properties = DialogProperties( | ||
dismissOnBackPress = false, | ||
dismissOnClickOutside = false, | ||
), | ||
) { | ||
Column( | ||
modifier = Modifier.padding(16.dp), | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
verticalArrangement = Arrangement.spacedBy(16.dp), | ||
) { | ||
Column { | ||
Text( | ||
text = stringResource(R.string.management_dialog_title), | ||
style = WappTheme.typography.titleBold, | ||
color = WappTheme.colors.white, | ||
textAlign = TextAlign.Center, | ||
) | ||
Text( | ||
text = stringResource(R.string.management_dialog_content), | ||
style = WappTheme.typography.captionMedium, | ||
color = WappTheme.colors.white, | ||
textAlign = TextAlign.Center, | ||
) | ||
} | ||
|
||
WappTextField( | ||
value = viewModel.manageCode.collectAsState().value, | ||
onValueChanged = { code -> viewModel.setManagementCode(code = code) }, | ||
label = R.string.code, | ||
isError = viewModel.isError.collectAsState().value, | ||
supportingText = stringResource( | ||
id = viewModel.errorSupportingText.collectAsState().value, | ||
), | ||
) | ||
|
||
WappButton( | ||
onClick = { viewModel.validateManageCode() }, | ||
isEnabled = viewModel.manageCode | ||
.collectAsState() | ||
.value | ||
.isNotBlank(), | ||
) | ||
} | ||
} | ||
} | ||
|
||
/* | ||
@Preview | ||
@Composable | ||
fun previewDialog() { | ||
WappTheme { | ||
ManageCodeDialog() | ||
} | ||
} | ||
*/ |
62 changes: 62 additions & 0 deletions
62
feature/manage/src/main/java/com/wap/wapp/feature/manage/code/ManagementCodeViewModel.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,62 @@ | ||
package com.wap.wapp.feature.manage.code | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.wap.wapp.core.domain.model.CodeValidation | ||
import com.wap.wapp.core.domain.usecase.manage.ValidateManageCodeUseCase | ||
import com.wap.wapp.feature.manage.R | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
|
||
@HiltViewModel | ||
class ManagementCodeViewModel @Inject constructor( | ||
private val validateManageCodeUseCase: ValidateManageCodeUseCase, | ||
) : ViewModel() { | ||
private val _managementCodeUiState: MutableStateFlow<ManagementCodeUiState> = | ||
MutableStateFlow(ManagementCodeUiState.Init) | ||
val managementCodeUiState: StateFlow<ManagementCodeUiState> | ||
get() = _managementCodeUiState | ||
|
||
private val _manageCode: MutableStateFlow<String> = MutableStateFlow("") | ||
val manageCode: StateFlow<String> get() = _manageCode | ||
|
||
private val _isError: MutableStateFlow<Boolean> = MutableStateFlow(false) | ||
val isError: StateFlow<Boolean> get() = _isError | ||
|
||
private val _errorSupportingText: MutableStateFlow<Int> = | ||
MutableStateFlow(R.string.management_dialog_hint) | ||
val errorSupportingText: StateFlow<Int> get() = _errorSupportingText | ||
|
||
fun validateManageCode() { | ||
viewModelScope.launch { | ||
validateManageCodeUseCase(_manageCode.value) | ||
.onSuccess { | ||
when (it) { | ||
CodeValidation.VALID -> { | ||
_managementCodeUiState.value = ManagementCodeUiState.Success | ||
} | ||
CodeValidation.INVALID -> { | ||
_isError.value = true | ||
_errorSupportingText.value = R.string.management_incorrect_code | ||
} | ||
} | ||
} | ||
.onFailure { throwable -> | ||
_managementCodeUiState.value = ManagementCodeUiState.Failure(throwable) | ||
} | ||
} | ||
} | ||
|
||
fun setManagementCode(code: String) { | ||
_manageCode.value = code | ||
} | ||
|
||
sealed class ManagementCodeUiState { | ||
data object Init : ManagementCodeUiState() | ||
data object Success : ManagementCodeUiState() | ||
data class Failure(val throwable: Throwable) : ManagementCodeUiState() | ||
} | ||
} |