Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#41] [Integrate] As a user, I can submit my form #130

Merged
merged 5 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ extension SurveyDetailView {
@Published var isLoading = false
@Published var isShowingTitle = true
@Published var isShowingTitleNavigationBar = true
@Published var questionIndex = 0
@Published var isShowingSuccessConfirmation = false
@Published var isShowingSubmit = false

private var cancellables = Set<AnyCancellable>()

Expand All @@ -33,29 +36,48 @@ extension SurveyDetailView {
viewModel.setSurveyId(id: id)
viewModel.getDetail()

createPublisher(for: viewModel.viewStateNative)
.catch { error -> Just<SurveyDetailViewState> in
let surveyDetailViewState = SurveyDetailViewState(
error: error.localizedDescription
)
return Just(surveyDetailViewState)
}
let viewState = createGuaranteedPublisher(
for: viewModel.viewStateNative,
fallback: SurveyDetailViewState()
)
let questionViewState = createGuaranteedPublisher(
for: viewModel.questionViewStateNative,
fallback: SurveyQuestionViewState()
)
viewState
.combineLatest(questionViewState)
.receive(on: DispatchQueue.main)
.sink { [weak self] value in
.sink { [weak self] viewState, questionViewState in
guard let self else { return }
self.updateStates(value)
self.updateStates(viewState, questionViewState)
}
.store(in: &cancellables)
}

func didPressNext() {
func didPressStart() {
viewModel.showQuestion()
}

private func updateStates(_ state: SurveyDetailViewState) {
func didPressNext(answers: [SurveyAnswer]) {
viewModel.addAnswer(values: answers)
}

func didPressSubmit() {
viewModel.submitAnswer()
}

private func updateStates(
_ state: SurveyDetailViewState,
_ questionState: SurveyQuestionViewState
) {
viewState = state
isShowingErrorAlert = !state.error.string.isEmpty
isLoading = state.isLoading && state.isShowingQuestion
isLoading = (state.isLoading || questionState.isLoading) && (state.isShowingQuestion)
isShowingSuccessConfirmation = questionState.isShowingSuccess
withAnimation(.easeIn(duration: .viewTransition)) {
isShowingSubmit = questionState.isShowingSubmit
questionIndex = Int(questionState.currentQuestionIndex)
}
if state.surveyDetail != nil, state.isShowingQuestion {
isShowingTitleNavigationBar = false
DispatchQueue.main.asyncAfter(deadline: .now() + .instant) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,19 @@ struct SurveyDetailView: View {
@StateObject var dataSource: DataSource

@State var isAnimating = true
@State var questionIndex = 0
// TODO: Replace with real answer object
@State var currentAnswers = [String]()
@State var isShowingQuitPrompt = false
@State var isShowingSuccessConfirmation = false
@State var currentAnswers = [SurveyAnswer]()

var body: some View {
ZStack {
surveyView
.if(!dataSource.isShowingTitleNavigationBar) { view in
view.navigationBarItems(trailing: closeButton)
}
if isShowingSuccessConfirmation {
if dataSource.isShowingSuccessConfirmation {
SubmissionSuccessView(
coordinator: coordinator,
isShowing: $isShowingSuccessConfirmation
isShowing: $dataSource.isShowingSuccessConfirmation
)
.ignoresSafeArea()
}
Expand Down Expand Up @@ -87,7 +84,7 @@ struct SurveyDetailView: View {
.transition(.move(edge: .leading).combined(with: .opacity))
} else if let surveyDetail = dataSource.viewState.surveyDetail {
animatedSurveyQuestionView(surveyDetail: surveyDetail)
.id(questionIndex)
.id(dataSource.questionIndex)
} else {
Spacer()
}
Expand Down Expand Up @@ -117,8 +114,7 @@ struct SurveyDetailView: View {
Spacer()
if dataSource.isShowingTitle {
startButton
} else if (dataSource.viewState.surveyDetail?.questions.count ?? 0) == questionIndex + 1 {
// TODO: Use ViewModel's State
} else if dataSource.isShowingSubmit {
submitButton
} else {
nextButton
Expand All @@ -128,7 +124,7 @@ struct SurveyDetailView: View {

var startButton: some View {
Button {
dataSource.didPressNext()
dataSource.didPressStart()
} label: {
Text(String.localizeId.survey_detail_start_button())
.primaryButton()
Expand All @@ -139,13 +135,8 @@ struct SurveyDetailView: View {

var nextButton: some View {
Button {
// TODO: Submit button logics
let totalItem = (dataSource.viewState.surveyDetail?.questions.count ?? 0) - 1
guard questionIndex < totalItem else { return }
dataSource.didPressNext(answers: currentAnswers)
currentAnswers = []
withAnimation(.easeIn(duration: .viewTransition)) {
questionIndex += 1
}
} label: {
Assets.nextButton
.image
Expand All @@ -158,14 +149,7 @@ struct SurveyDetailView: View {

var submitButton: some View {
Button {
// TODO: Submit action
dataSource.isLoading = true
DispatchQueue.main.asyncAfter(deadline: .now()) {
withAnimation(.easeInViewTransition) {
dataSource.isLoading = false
isShowingSuccessConfirmation = true
}
}
dataSource.didPressSubmit()
} label: {
Text(String.localizeId.survey_submit_button())
.primaryButton()
Expand All @@ -183,7 +167,7 @@ struct SurveyDetailView: View {
.resizable()
.frame(width: 28.0, height: 28.0)
.accessibility(.surveyQuestion(.closeButton))
.opacity(isShowingSuccessConfirmation ? 0.0 : 1.0)
.opacity(dataSource.isShowingSuccessConfirmation ? 0.0 : 1.0)
}
.disabled(isAnimating)
}
Expand Down Expand Up @@ -216,7 +200,7 @@ struct SurveyDetailView: View {
private func animatedSurveyQuestionView(surveyDetail: SurveyDetailUiModel) -> some View {
return SurveyQuestionView(
detail: surveyDetail,
questionIndex: $questionIndex,
questionIndex: $dataSource.questionIndex,
answers: $currentAnswers
)
.transition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct QuestionEmojiView: View {
}

@State var rating: Int = 0
@Binding var answers: [String]
@Binding var answers: [SurveyAnswer]

let type: EmojiType
let options: [Answer]
Expand All @@ -56,7 +56,7 @@ struct QuestionEmojiView: View {
Spacer()
}
.onChange(of: rating) {
answers = [options[$0].id]
answers = [.init(id: options[$0].id, answer: nil)]
blyscuit marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct QuestionMultiChoiceView: View {
let options: [Answer]

@State var inputs = Set<String>()
@Binding var answers: [String]
@Binding var answers: [SurveyAnswer]

var body: some View {
VStack {
Expand Down Expand Up @@ -56,6 +56,6 @@ struct QuestionMultiChoiceView: View {
} else {
inputs.insert(id)
}
answers = Array(inputs)
answers = inputs.map { .init(id: $0, answer: nil) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct QuestionMultiFormView: View {
}

@State var multiFormAnswers = [MultiFormAnswer]()
@Binding var answers: [String]
@Binding var answers: [SurveyAnswer]

var body: some View {
VStack(spacing: .lineSpacing) {
Expand All @@ -33,11 +33,11 @@ struct QuestionMultiFormView: View {
}
}
.onChange(of: multiFormAnswers) {
answers = $0.map { $0.input }
answers = $0.map { .init(id: $0.question.id, answer: $0.input) }
}
}

init(answers: [Answer], currentAnswers: Binding<[String]>) {
init(answers: [Answer], currentAnswers: Binding<[SurveyAnswer]>) {
_multiFormAnswers = .init(
initialValue: answers.map {
MultiFormAnswer(question: $0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
struct QuestionPickerView: View {

@State private var selectedId = ""
@Binding var answers: [String]
@Binding var answers: [SurveyAnswer]

let options: [Answer]

Expand All @@ -31,7 +31,7 @@ struct QuestionPickerView: View {
}
.pickerStyle(.wheel)
.onChange(of: selectedId) {
answers = [$0]
answers = [.init(id: $0, answer: nil)]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
struct QuestionRangePickerView: View {

@State var selectedId: String = ""
@Binding var answers: [String]
@Binding var answers: [SurveyAnswer]

let options: [Answer]
let helpText: String
Expand Down Expand Up @@ -59,7 +59,7 @@ struct QuestionRangePickerView: View {
}
}
.onChange(of: selectedId) {
answers = [$0]
answers = [.init(id: $0, answer: nil)]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import SwiftUI

struct QuestionTextAreaView: View {

let id: String
let placeholder: String?

@State var text = ""
@Binding var answers: [String]
@Binding var answers: [SurveyAnswer]

var body: some View {
ZStack(alignment: .topLeading) {
Expand All @@ -34,7 +35,7 @@ struct QuestionTextAreaView: View {
}
}
.onChange(of: text) {
answers = [$0]
answers = [.init(id: id, answer: $0)]
}
.onAppear {
UITextView.appearance().backgroundColor = .clear
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import Shared
import SwiftUI

typealias Answer = SurveyDetailUiModel.SurveyAnswer
typealias SurveyAnswer = SurveySubmissionUiModel.Answer

struct SurveyQuestionView: View {

let detail: SurveyDetailUiModel

@Binding var questionIndex: Int
@Binding var answers: [String]
@Binding var answers: [SurveyAnswer]

var body: some View {
VStack(alignment: .leading) {
Expand Down Expand Up @@ -51,7 +52,6 @@ struct SurveyQuestionView: View {

@ViewBuilder
func questionView(with question: SurveyDetailUiModel.SurveyIncluded) -> some View {
// TODO: Show real questions
switch question.displayType {
case .dropdown:
QuestionPickerView(answers: $answers, options: question.answers)
Expand All @@ -72,7 +72,11 @@ struct SurveyQuestionView: View {
case .textfield:
QuestionMultiFormView(answers: question.answers, currentAnswers: $answers)
case .textarea:
QuestionTextAreaView(placeholder: question.answers.first?.text, answers: $answers)
QuestionTextAreaView(
id: question.id,
placeholder: question.answers.first?.text,
answers: $answers
)
default: VStack {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ val viewModelModule = module {
factoryOf(::ResetPasswordViewModel)
factoryOf(::SplashViewModel)
factoryOf(::SurveySelectionViewModel)
factory { SurveyDetailViewModel(get()) }
factory { SurveyDetailViewModel(get(), get()) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package co.nimblehq.blisskmmic.presentation.model

import co.nimblehq.blisskmmic.domain.model.SurveySubmission

data class SurveySubmissionUiModel(
blyscuit marked this conversation as resolved.
Show resolved Hide resolved
val id: String,
val answers: List<Answer>
) {

data class Answer(
val id: String,
val answer: String? = null
)
}

fun SurveySubmissionUiModel.Answer.toSurveySubmission() = SurveySubmission.Answer(
id,
answer
)

fun List<SurveySubmissionUiModel>.toSurveySubmission(surveyId: String) = SurveySubmission(
surveyId,
map { model ->
SurveySubmission.Question(
model.id,
model.answers.map { it.toSurveySubmission() })
}
)
Loading