Skip to content

Commit

Permalink
[FEATURE] #93 : setSurveyRegistrationState 함수 세분화
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongjaino committed Jan 9, 2024
1 parent e7cfa84 commit a8072cb
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand All @@ -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,
)
},
)
}

Expand All @@ -57,7 +61,12 @@ internal fun SurveyFormContent(
onTitleChanged = onTitleChanged,
content = content,
onContentChanged = onContentChanged,
onNextButtonClicked = { onNextButtonClicked(SurveyFormState.QUESTION) },
onNextButtonClicked = {
onNextButtonClicked(
SurveyFormState.INFORMATION,
SurveyFormState.QUESTION,
)
},
)
}

Expand All @@ -72,7 +81,12 @@ internal fun SurveyFormContent(
onAddSurveyQuestionButtonClicked = onAddQuestionButtonClicked,
currentQuestionIndex = currentQuestionIndex,
totalQuestionIndex = totalQuestionSize,
onNextButtonClicked = { onNextButtonClicked(SurveyFormState.DEADLINE) },
onNextButtonClicked = {
onNextButtonClicked(
SurveyFormState.QUESTION,
SurveyFormState.DEADLINE,
)
},
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,17 +32,17 @@ class SurveyFormEditViewModel @Inject constructor(
MutableSharedFlow()
val surveyFormEditUiEvent = _surveyFormEditUiEvent.asSharedFlow()

private val _currentRegistrationState: MutableStateFlow<SurveyFormState> =
private val _currentSurveyFormState: MutableStateFlow<SurveyFormState> =
MutableStateFlow(SurveyFormState.EVENT_SELECTION)
val currentRegistrationState = _currentRegistrationState.asStateFlow()
val currentSurveyFormState = _currentSurveyFormState.asStateFlow()

private val _eventList: MutableStateFlow<List<Event>> = MutableStateFlow(emptyList())
val eventList = _eventList.asStateFlow()

private val surveyFormId: MutableStateFlow<String> = MutableStateFlow("")

private val _surveyEventSelection: MutableStateFlow<Event> =
MutableStateFlow(SurveyFormRegistrationViewModel.EVENT_SELECTION_INIT)
MutableStateFlow(EVENT_SELECTION_INIT)
val surveyEventSelection = _surveyEventSelection.asStateFlow()

private val _surveyTitle: MutableStateFlow<String> = MutableStateFlow("")
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -193,15 +196,15 @@ 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

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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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() },
)
}
}
Expand Down
Loading

0 comments on commit a8072cb

Please sign in to comment.