Skip to content

Commit

Permalink
[FEATURE] #38 : 운영진 코드 인증 디알로그 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongjaino committed Oct 23, 2023
1 parent 44c3884 commit 75dba23
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
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()
}
}
*/
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()
}
}

0 comments on commit 75dba23

Please sign in to comment.