diff --git a/feature/manage/src/main/java/com/wap/wapp/feature/manage/code/ManageCodeDialog.kt b/feature/manage/src/main/java/com/wap/wapp/feature/manage/code/ManageCodeDialog.kt new file mode 100644 index 00000000..c7fa847d --- /dev/null +++ b/feature/manage/src/main/java/com/wap/wapp/feature/manage/code/ManageCodeDialog.kt @@ -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() + } +} +*/ diff --git a/feature/manage/src/main/java/com/wap/wapp/feature/manage/code/ManagementCodeViewModel.kt b/feature/manage/src/main/java/com/wap/wapp/feature/manage/code/ManagementCodeViewModel.kt new file mode 100644 index 00000000..705d7941 --- /dev/null +++ b/feature/manage/src/main/java/com/wap/wapp/feature/manage/code/ManagementCodeViewModel.kt @@ -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 = + MutableStateFlow(ManagementCodeUiState.Init) + val managementCodeUiState: StateFlow + get() = _managementCodeUiState + + private val _manageCode: MutableStateFlow = MutableStateFlow("") + val manageCode: StateFlow get() = _manageCode + + private val _isError: MutableStateFlow = MutableStateFlow(false) + val isError: StateFlow get() = _isError + + private val _errorSupportingText: MutableStateFlow = + MutableStateFlow(R.string.management_dialog_hint) + val errorSupportingText: StateFlow 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() + } +}