Skip to content

Commit

Permalink
[CHORE] #94 : SurveyAnswerForm 컴포저블 함수 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongjaino committed Jan 22, 2024
1 parent 228ea9a commit 6d9d914
Showing 1 changed file with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.wap.wapp.feature.survey.answer

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.wap.designsystem.component.WappButton
import com.wap.wapp.core.model.survey.QuestionType
import com.wap.wapp.core.model.survey.Rating
import com.wap.wapp.core.model.survey.SurveyForm
import com.wap.wapp.feature.survey.R

@Composable
internal fun SurveyAnswerForm(
surveyForm: SurveyForm,
modifier: Modifier,
questionNumber: Int,
subjectiveAnswer: String,
objectiveAnswer: Rating,
onSubjectiveAnswerChanged: (String) -> Unit,
onObjectiveAnswerSelected: (Rating) -> Unit,
onNextQuestionButtonClicked: () -> Unit,
onPreviousQuestionButtonClicked: () -> Unit,
) {
Column(
verticalArrangement = Arrangement.spacedBy(40.dp),
modifier = modifier,
) {
val surveyQuestion = surveyForm.surveyQuestionList[questionNumber]
val lastQuestionNumber = surveyForm.surveyQuestionList.lastIndex

SurveyAnswerStateIndicator(
index = questionNumber + 1,
size = lastQuestionNumber + 1,
)

when (surveyQuestion.questionType) {
QuestionType.SUBJECTIVE -> {
SubjectiveSurveyForm(
questionTitle = surveyQuestion.questionTitle,
answer = subjectiveAnswer,
onAnswerChanged = onSubjectiveAnswerChanged,
modifier = Modifier.weight(1f),
)
}

QuestionType.OBJECTIVE -> {
ObjectiveSurveyForm(
questionTitle = surveyQuestion.questionTitle,
answer = objectiveAnswer,
onAnswerSelected = onObjectiveAnswerSelected,
modifier = Modifier.weight(1f),
)
}
}

val isFirstQuestion = questionNumber > 0
val isLastQuestion = questionNumber == lastQuestionNumber // 마지막 응답일 경우, 완료로 변경
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
WappButton(
textRes = R.string.previous,
onClick = onPreviousQuestionButtonClicked,
isEnabled = isFirstQuestion,
modifier = Modifier.weight(1f),
)

SurveyAnswerButton(
isLastQuestion = isLastQuestion,
onButtonClicked = onNextQuestionButtonClicked,
isEnabled = isButtonEnabled(surveyQuestion.questionType, subjectiveAnswer),
modifier = Modifier.weight(1f),
)
}
}
}

@Composable
private fun SurveyAnswerButton(
isLastQuestion: Boolean,
onButtonClicked: () -> Unit,
isEnabled: Boolean,
modifier: Modifier,
) {
if (isLastQuestion) {
WappButton(
textRes = R.string.submit,
onClick = { onButtonClicked() },
isEnabled = isEnabled,
modifier = modifier,
)
} else {
WappButton(
textRes = R.string.next,
onClick = { onButtonClicked() },
isEnabled = isEnabled,
modifier = modifier,
)
}
}

private fun isButtonEnabled(
questionType: QuestionType,
subjectiveAnswer: String,
): Boolean {
if (questionType == QuestionType.SUBJECTIVE) return subjectiveAnswer.length >= 10

return true
}

0 comments on commit 6d9d914

Please sign in to comment.