diff --git a/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/SurveyFormContent.kt b/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/SurveyFormContent.kt index a45e54ca7..c65be37c1 100644 --- a/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/SurveyFormContent.kt +++ b/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/SurveyFormContent.kt @@ -35,7 +35,7 @@ internal fun SurveyFormContent( onQuestionChanged: (String) -> Unit, onQuestionTypeChanged: (QuestionType) -> Unit, onTimeChanged: (LocalTime) -> Unit, - onNextButtonClicked: (SurveyFormState) -> Unit, + onNextButtonClicked: (SurveyFormState, SurveyFormState) -> Unit, // (currentState, nextState) onAddQuestionButtonClicked: () -> Unit, onRegisterButtonClicked: () -> Unit, ) { @@ -45,9 +45,13 @@ internal fun SurveyFormContent( SurveyEventContent( eventList = eventList, selectedEvent = eventSelection, - // default prefix -> 함수 parameter <-> 콜백 함수 parameter conflict onEventSelected = onEventSelected, - onNextButtonClicked = { onNextButtonClicked(SurveyFormState.INFORMATION) }, + onNextButtonClicked = { + onNextButtonClicked( + SurveyFormState.EVENT_SELECTION, + SurveyFormState.INFORMATION, + ) + }, ) } @@ -57,7 +61,12 @@ internal fun SurveyFormContent( onTitleChanged = onTitleChanged, content = content, onContentChanged = onContentChanged, - onNextButtonClicked = { onNextButtonClicked(SurveyFormState.QUESTION) }, + onNextButtonClicked = { + onNextButtonClicked( + SurveyFormState.INFORMATION, + SurveyFormState.QUESTION, + ) + }, ) } @@ -72,7 +81,12 @@ internal fun SurveyFormContent( onAddSurveyQuestionButtonClicked = onAddQuestionButtonClicked, currentQuestionIndex = currentQuestionIndex, totalQuestionIndex = totalQuestionSize, - onNextButtonClicked = { onNextButtonClicked(SurveyFormState.DEADLINE) }, + onNextButtonClicked = { + onNextButtonClicked( + SurveyFormState.QUESTION, + SurveyFormState.DEADLINE, + ) + }, ) } diff --git a/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/edit/SurveyFormEditScreen.kt b/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/edit/SurveyFormEditScreen.kt index fb098e61d..59c51fb10 100644 --- a/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/edit/SurveyFormEditScreen.kt +++ b/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/edit/SurveyFormEditScreen.kt @@ -25,6 +25,7 @@ import com.wap.designsystem.component.WappSubTopBar import com.wap.wapp.core.commmon.extensions.toSupportingText import com.wap.wapp.feature.management.survey.R import com.wap.wapp.feature.management.survey.SurveyFormContent +import com.wap.wapp.feature.management.survey.SurveyFormState import com.wap.wapp.feature.management.survey.SurveyFormStateIndicator import kotlinx.coroutines.flow.collectLatest @@ -35,7 +36,7 @@ internal fun SurveyFormEditScreen( surveyFormId: String, navigateToManagement: () -> Unit, ) { - val currentRegistrationState = viewModel.currentRegistrationState.collectAsState().value + val currentRegistrationState = viewModel.currentSurveyFormState.collectAsState().value val eventList = viewModel.eventList.collectAsState().value val eventSelection = viewModel.surveyEventSelection.collectAsState().value val title = viewModel.surveyTitle.collectAsState().value @@ -124,11 +125,27 @@ internal fun SurveyFormEditScreen( }, onDateChanged = viewModel::setSurveyDateDeadline, onTimeChanged = { localTime -> viewModel.setSurveyTimeDeadline(localTime) }, - onNextButtonClicked = { surveyRegistrationState -> - viewModel.setSurveyRegistrationState(surveyRegistrationState) + onNextButtonClicked = { currentState, nextState -> + if (viewModel.validateSurveyForm(currentState).not()) { // 유효성 검증 + return@SurveyFormContent + } + + if (currentState == SurveyFormState.QUESTION) { + viewModel.addSurveyQuestion() // 마지막으로 작성한 질문, 질문 목록에 추가 + } + + viewModel.setSurveyFormState(nextState) // 다음 단계 + }, + onAddQuestionButtonClicked = { + if (viewModel.validateSurveyForm(SurveyFormState.QUESTION)) { + viewModel.addSurveyQuestion() + } + }, + onRegisterButtonClicked = { + if (viewModel.validateSurveyForm(SurveyFormState.DEADLINE)) { + viewModel.updateSurveyForm() + } }, - onAddQuestionButtonClicked = { viewModel.addSurveyQuestion() }, - onRegisterButtonClicked = viewModel::updateSurveyForm, ) } } diff --git a/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/edit/SurveyFormEditViewModel.kt b/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/edit/SurveyFormEditViewModel.kt index f04f9a76d..d54566770 100644 --- a/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/edit/SurveyFormEditViewModel.kt +++ b/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/edit/SurveyFormEditViewModel.kt @@ -11,7 +11,6 @@ import com.wap.wapp.core.model.survey.QuestionType import com.wap.wapp.core.model.survey.SurveyForm import com.wap.wapp.core.model.survey.SurveyQuestion import com.wap.wapp.feature.management.survey.SurveyFormState -import com.wap.wapp.feature.management.survey.registration.SurveyFormRegistrationViewModel import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow @@ -33,9 +32,9 @@ class SurveyFormEditViewModel @Inject constructor( MutableSharedFlow() val surveyFormEditUiEvent = _surveyFormEditUiEvent.asSharedFlow() - private val _currentRegistrationState: MutableStateFlow = + private val _currentSurveyFormState: MutableStateFlow = MutableStateFlow(SurveyFormState.EVENT_SELECTION) - val currentRegistrationState = _currentRegistrationState.asStateFlow() + val currentSurveyFormState = _currentSurveyFormState.asStateFlow() private val _eventList: MutableStateFlow> = MutableStateFlow(emptyList()) val eventList = _eventList.asStateFlow() @@ -43,7 +42,7 @@ class SurveyFormEditViewModel @Inject constructor( private val surveyFormId: MutableStateFlow = MutableStateFlow("") private val _surveyEventSelection: MutableStateFlow = - MutableStateFlow(SurveyFormRegistrationViewModel.EVENT_SELECTION_INIT) + MutableStateFlow(EVENT_SELECTION_INIT) val surveyEventSelection = _surveyEventSelection.asStateFlow() private val _surveyTitle: MutableStateFlow = MutableStateFlow("") @@ -94,64 +93,68 @@ class SurveyFormEditViewModel @Inject constructor( } fun updateSurveyForm() = viewModelScope.launch { - if (isValidDeadline()) { - val surveyForm = SurveyForm( - surveyFormId = surveyFormId.value, - eventId = _surveyEventSelection.value.eventId, - title = _surveyTitle.value, - content = _surveyContent.value, - surveyQuestionList = _surveyQuestionList.value, - deadline = LocalDateTime.of(_surveyDateDeadline.value, _surveyTimeDeadline.value), - ) + val surveyForm = SurveyForm( + surveyFormId = surveyFormId.value, + eventId = _surveyEventSelection.value.eventId, + title = _surveyTitle.value, + content = _surveyContent.value, + surveyQuestionList = _surveyQuestionList.value, + deadline = LocalDateTime.of(_surveyDateDeadline.value, _surveyTimeDeadline.value), + ) - updateSurveyFormUseCase(surveyForm = surveyForm) - .onSuccess { - _surveyFormEditUiEvent.emit(SurveyFormEditUiEvent.Success) - }.onFailure { throwable -> - _surveyFormEditUiEvent.emit(SurveyFormEditUiEvent.Failure(throwable)) - } - } else { - emitValidationErrorMessage("최소 하루 이상 설문 날짜를 지정하세요.") - } + updateSurveyFormUseCase(surveyForm = surveyForm) + .onSuccess { + _surveyFormEditUiEvent.emit(SurveyFormEditUiEvent.Success) + }.onFailure { throwable -> + _surveyFormEditUiEvent.emit(SurveyFormEditUiEvent.Failure(throwable)) + } } - fun setSurveyRegistrationState(surveyRegistrationState: SurveyFormState) { - when (surveyRegistrationState) { - SurveyFormState.EVENT_SELECTION -> { /* initial value */ } + fun getEventList() = viewModelScope.launch { + getEventListUseCase(DateUtil.generateNowDate()) + .onSuccess { eventList -> + _eventList.value = eventList + }.onFailure { throwable -> + _surveyFormEditUiEvent.emit(SurveyFormEditUiEvent.Failure(throwable)) + } + } - SurveyFormState.INFORMATION -> { + fun validateSurveyForm(currentState: SurveyFormState): Boolean { + when (currentState) { + SurveyFormState.EVENT_SELECTION -> { if (isNotValidEventSelection()) { emitValidationErrorMessage("행사를 선택해 주세요.") - return + return false } } - SurveyFormState.QUESTION -> { + SurveyFormState.INFORMATION -> { if (isNotValidInformation()) { emitValidationErrorMessage("제목과 내용을 확인해 주세요.") - return + return false } } - SurveyFormState.DEADLINE -> { - if (isValidSurveyQuestion()) { - addSurveyQuestion() // 현재 작성한 질문, 질문 리스트에 삽입 - } else { + SurveyFormState.QUESTION -> { + if (isNotValidSurveyQuestion()) { emitValidationErrorMessage("질문 내용을 확인해 주세요.") - return + return false + } + } + + SurveyFormState.DEADLINE -> { + if (isNotValidDeadline()) { + emitValidationErrorMessage("최소 하루 이상 설문 날짜를 지정하세요.") + return false } } } - _currentRegistrationState.value = surveyRegistrationState + + return true } - fun getEventList() = viewModelScope.launch { - getEventListUseCase(DateUtil.generateNowDate()) - .onSuccess { eventList -> - _eventList.value = eventList - }.onFailure { throwable -> - _surveyFormEditUiEvent.emit(SurveyFormEditUiEvent.Failure(throwable)) - } + fun setSurveyFormState(nextState: SurveyFormState) { + _currentSurveyFormState.value = nextState } fun addSurveyQuestion() { @@ -193,7 +196,7 @@ class SurveyFormEditViewModel @Inject constructor( private fun clearSurveyQuestionState() { _surveyQuestion.value = EMPTY } - private fun isValidSurveyQuestion() = _surveyQuestion.value.isNotBlank() + private fun isNotValidSurveyQuestion() = _surveyQuestion.value.isBlank() private fun isNotValidEventSelection() = _surveyEventSelection.value.eventId == EVENT_SELECTION_INIT.eventId @@ -201,7 +204,7 @@ class SurveyFormEditViewModel @Inject constructor( private fun isNotValidInformation() = _surveyTitle.value.isBlank() || _surveyContent.value.isBlank() - private fun isValidDeadline() = _surveyDateDeadline.value > DateUtil.generateNowDate() + private fun isNotValidDeadline() = _surveyDateDeadline.value <= DateUtil.generateNowDate() private fun emitValidationErrorMessage(message: String) { viewModelScope.launch { diff --git a/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/registration/SurveyFormRegistrationScreen.kt b/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/registration/SurveyFormRegistrationScreen.kt index 75b34d9bc..7def59033 100644 --- a/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/registration/SurveyFormRegistrationScreen.kt +++ b/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/registration/SurveyFormRegistrationScreen.kt @@ -25,6 +25,7 @@ import com.wap.designsystem.component.WappSubTopBar import com.wap.wapp.core.commmon.extensions.toSupportingText import com.wap.wapp.feature.management.survey.R import com.wap.wapp.feature.management.survey.SurveyFormContent +import com.wap.wapp.feature.management.survey.SurveyFormState import com.wap.wapp.feature.management.survey.SurveyFormStateIndicator import kotlinx.coroutines.flow.collectLatest @@ -34,7 +35,7 @@ internal fun SurveyRegistrationScreen( viewModel: SurveyFormRegistrationViewModel = hiltViewModel(), navigateToManagement: () -> Unit, ) { - val currentRegistrationState = viewModel.currentRegistrationState.collectAsState().value + val currentRegistrationState = viewModel.currentSurveyFormState.collectAsState().value val eventList = viewModel.eventList.collectAsState().value val eventSelection = viewModel.surveyEventSelection.collectAsState().value val title = viewModel.surveyTitle.collectAsState().value @@ -119,11 +120,27 @@ internal fun SurveyRegistrationScreen( }, onDateChanged = viewModel::setSurveyDateDeadline, onTimeChanged = { localTime -> viewModel.setSurveyTimeDeadline(localTime) }, - onNextButtonClicked = { surveyRegistrationState -> - viewModel.setSurveyRegistrationState(surveyRegistrationState) + onNextButtonClicked = { currentState, nextState -> + if (viewModel.validateSurveyForm(currentState).not()) { + return@SurveyFormContent + } + + if (currentState == SurveyFormState.QUESTION) { + viewModel.addSurveyQuestion() + } + + viewModel.setSurveyFormState(nextState) + }, + onAddQuestionButtonClicked = { + if (viewModel.validateSurveyForm(SurveyFormState.QUESTION)) { + viewModel.addSurveyQuestion() + } + }, + onRegisterButtonClicked = { + if (viewModel.validateSurveyForm(SurveyFormState.DEADLINE)) { + viewModel.registerSurvey() + } }, - onAddQuestionButtonClicked = { viewModel.addSurveyQuestion() }, - onRegisterButtonClicked = { viewModel.registerSurvey() }, ) } } diff --git a/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/registration/SurveyFormRegistrationViewModel.kt b/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/registration/SurveyFormRegistrationViewModel.kt index 999a4dcd4..84306e8c3 100644 --- a/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/registration/SurveyFormRegistrationViewModel.kt +++ b/feature/management-survey/src/main/java/com/wap/wapp/feature/management/survey/registration/SurveyFormRegistrationViewModel.kt @@ -28,9 +28,9 @@ class SurveyFormRegistrationViewModel @Inject constructor( MutableSharedFlow() val surveyRegistrationEvent = _surveyRegistrationEvent.asSharedFlow() - private val _currentRegistrationState: MutableStateFlow = + private val _currentSurveyFormState: MutableStateFlow = MutableStateFlow(SurveyFormState.EVENT_SELECTION) - val currentRegistrationState = _currentRegistrationState.asStateFlow() + val currentSurveyFormState = _currentSurveyFormState.asStateFlow() private val _eventList: MutableStateFlow> = MutableStateFlow(emptyList()) val eventList = _eventList.asStateFlow() @@ -64,64 +64,66 @@ class SurveyFormRegistrationViewModel @Inject constructor( MutableStateFlow(DateUtil.generateNowDate()) val surveyDateDeadline = _surveyDateDeadline.asStateFlow() - // Content 전환 함수, 전환 전 내용 검증 - fun setSurveyRegistrationState(surveyRegistrationState: SurveyFormState) { - when (surveyRegistrationState) { - SurveyFormState.EVENT_SELECTION -> { /* initial value */ + fun getEventList() = viewModelScope.launch { + getEventListUseCase(DateUtil.generateNowDate()) + .onSuccess { eventList -> + _eventList.value = eventList + }.onFailure { throwable -> + _surveyRegistrationEvent.emit(SurveyRegistrationEvent.Failure(throwable)) } + } - SurveyFormState.INFORMATION -> { + fun registerSurvey() = viewModelScope.launch { + registerSurveyUseCase( + event = surveyEventSelection.value, + title = _surveyTitle.value, + content = _surveyContent.value, + surveyQuestionList = _surveyQuestionList.value, + deadlineDate = _surveyDateDeadline.value, + deadlineTime = _surveyTimeDeadline.value, + ).onSuccess { + _surveyRegistrationEvent.emit(SurveyRegistrationEvent.Success) + }.onFailure { throwable -> + _surveyRegistrationEvent.emit(SurveyRegistrationEvent.Failure(throwable)) + } + } + + fun validateSurveyForm(currentState: SurveyFormState): Boolean { + when (currentState) { + SurveyFormState.EVENT_SELECTION -> { if (isNotValidEventSelection()) { emitValidationErrorMessage("행사를 선택해 주세요.") - return + return false } } - SurveyFormState.QUESTION -> { + SurveyFormState.INFORMATION -> { if (isNotValidInformation()) { emitValidationErrorMessage("제목과 내용을 확인해 주세요.") - return + return false } } - SurveyFormState.DEADLINE -> { - if (isValidSurveyQuestion()) { - addSurveyQuestion() // 현재 작성한 질문, 질문 리스트에 삽입 - } else { + SurveyFormState.QUESTION -> { + if (isNotValidSurveyQuestion()) { emitValidationErrorMessage("질문 내용을 확인해 주세요.") - return + return false } } - } - _currentRegistrationState.value = surveyRegistrationState - } - fun getEventList() = viewModelScope.launch { - getEventListUseCase(DateUtil.generateNowDate()) - .onSuccess { eventList -> - _eventList.value = eventList - }.onFailure { throwable -> - _surveyRegistrationEvent.emit(SurveyRegistrationEvent.Failure(throwable)) + SurveyFormState.DEADLINE -> { + if (isNotValidDeadline()) { + emitValidationErrorMessage("최소 하루 이상 설문 날짜를 지정하세요.") + return false + } } + } + + return true } - fun registerSurvey() = viewModelScope.launch { - if (isValidDeadline()) { - registerSurveyUseCase( - event = surveyEventSelection.value, - title = _surveyTitle.value, - content = _surveyContent.value, - surveyQuestionList = _surveyQuestionList.value, - deadlineDate = _surveyDateDeadline.value, - deadlineTime = _surveyTimeDeadline.value, - ).onSuccess { - _surveyRegistrationEvent.emit(SurveyRegistrationEvent.Success) - }.onFailure { throwable -> - _surveyRegistrationEvent.emit(SurveyRegistrationEvent.Failure(throwable)) - } - } else { - emitValidationErrorMessage("최소 하루 이상 설문 날짜를 지정하세요.") - } + fun setSurveyFormState(nextState: SurveyFormState) { + _currentSurveyFormState.value = nextState } fun setSurveyEventSelection(event: Event) { _surveyEventSelection.value = event } @@ -132,9 +134,7 @@ class SurveyFormRegistrationViewModel @Inject constructor( fun setSurveyQuestion(question: String) { _surveyQuestion.value = question } - fun setSurveyQuestionType(questionType: QuestionType) { - _surveyQuestionType.value = questionType - } + fun setSurveyQuestionType(type: QuestionType) { _surveyQuestionType.value = type } fun setSurveyTimeDeadline(time: LocalTime) { _surveyTimeDeadline.value = time } @@ -150,11 +150,9 @@ class SurveyFormRegistrationViewModel @Inject constructor( clearSurveyQuestionState() } - private fun clearSurveyQuestionState() { - _surveyQuestion.value = EMPTY - } + private fun clearSurveyQuestionState() { _surveyQuestion.value = EMPTY } - private fun isValidSurveyQuestion() = _surveyQuestion.value.isNotBlank() + private fun isNotValidSurveyQuestion() = _surveyQuestion.value.isBlank() private fun isNotValidEventSelection() = _surveyEventSelection.value.eventId == EVENT_SELECTION_INIT.eventId @@ -162,7 +160,7 @@ class SurveyFormRegistrationViewModel @Inject constructor( private fun isNotValidInformation() = _surveyTitle.value.isBlank() || _surveyContent.value.isBlank() - private fun isValidDeadline() = _surveyDateDeadline.value > DateUtil.generateNowDate() + private fun isNotValidDeadline() = _surveyDateDeadline.value <= DateUtil.generateNowDate() private fun emitValidationErrorMessage(message: String) { viewModelScope.launch {