-
Hi, SCA community. I'm trying to wrap my head around designing the communication between multiple children. The concrete example is an app that shows a single-choice question (1) with answers, as shown in the following screenshots and structs. How can I handle the state when an answer is tapped but wrong? In the AnswerView that is tapped I can change the color to red (2), but in the correct AnswerView, I would need to change the color to green (4).
App screenshots by Kavsoft Show reducersQuestion.swift struct Question: ReducerProtocol {
struct State: Equatable, Hashable {
var questionData: QuestionData // the content of the question, like the text and the answers
var questionsInSession: Int // total amount of questions per session
var questionIndex: Int // the current to show the progress bar
var answers: IdentifiedArrayOf<Answer.State>
var answered: Bool = false
}
enum Action: Equatable {
case answer(id: Answer.State.ID, action: Answer.Action)
case closeSession
case goToNextQuestion
}
var body: some ReducerProtocol<State, Action> {
Reduce { state, action in
switch action {
case .answer(id: _, action: .answerTapped):
state.answered = true
return .none
case .answer:
return .none
case .closeSession:
return .none
case .goToNextQuestion:
return .none
}
}
.forEach(\.answers, action: /Action.answer(id:action:)) {
Answer()
}
}
} Answer.swift struct Answer: ReducerProtocol {
struct State: Equatable, Hashable, Identifiable {
let answerData: AnswerData // the content of the answer, like the text and whether it is correct
var id: Id {
Id(answerData.id)
}
typealias Id = Tagged<Self, String>
}
enum Action: Equatable {
case answerTapped
}
public var body: some ReducerProtocol<State, Action> {
Reduce { _, action in
switch action {
case .answerTapped:
return .none
}
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The parent should be responsible for that logic,
|
Beta Was this translation helpful? Give feedback.
The parent should be responsible for that logic,
for example, in your Question reducer: