Skip to content

Commit

Permalink
Refactor the Flow class
Browse files Browse the repository at this point in the history
  • Loading branch information
tyegah committed May 5, 2022
1 parent 06187ed commit c6ecfa9
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions EDGameEngine/Flow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,31 @@ class Flow {

func start() {
if let firstQuestion = questions.first {
router.routeTo(question: firstQuestion, answerCallback: routeNext(from: firstQuestion))
router.routeTo(question: firstQuestion, answerCallback: nextCallback(from: firstQuestion))
}
else {
router.routeTo(result: result)
}
}

private func routeNext(from question:String) -> Router.AnswerCallback {
private func nextCallback(from question:String) -> Router.AnswerCallback {
return {[weak self] answer in
if let self = self {
self.routeNext(question: question, answer: answer)
self.routeNext(question, answer)
}
}
}

private func routeNext(question:String, answer:String) {
if let currentQuestionIndex = self.questions.firstIndex(of: question) {
self.result[question] = answer
if currentQuestionIndex+1 < self.questions.count {
let nextQuestion = self.questions[currentQuestionIndex+1]
self.router.routeTo(question: nextQuestion, answerCallback: self.routeNext(from: nextQuestion))
private func routeNext(_ question:String,_ answer:String) {
if let currentQuestionIndex = questions.firstIndex(of: question) {
result[question] = answer
let nextQuestionIndex = currentQuestionIndex+1
if nextQuestionIndex < questions.count {
let nextQuestion = questions[nextQuestionIndex]
router.routeTo(question: nextQuestion, answerCallback: nextCallback(from: nextQuestion))
}
else {
self.router.routeTo(result: self.result)
router.routeTo(result: result)
}
}
}
Expand Down

0 comments on commit c6ecfa9

Please sign in to comment.