@@ -4,76 +4,82 @@ import org.reduxkotlin.ActionTypes
44import com.willowtreeapps.common.Actions.*
55import com.willowtreeapps.common.util.NO_MATCH
66import com.willowtreeapps.common.util.match
7+ import org.reduxkotlin.castingReducer
78
89/* *
910 * Reducers and functions used by reducers are in this file. Functions must be pure functions without
1011 * side effects.
1112 */
12- fun reducer (state : AppState , action : Any ): AppState =
13- when (action) {
14- is ActionTypes .INIT -> { AppState .INITIAL_STATE }
15- is FetchingItemsStartedAction -> state.copy(isLoadingItems = true )
16- is FetchingItemsSuccessAction -> {
17- state.copy(isLoadingItems = false ,
18- items = action.itemsHolder.items,
19- questionTitle = action.itemsHolder.questionTitle,
20- questions = action.itemsHolder.questions)
21- }
22- is FetchingItemsFailedAction -> state.copy(isLoadingItems = false , errorLoadingItems = true , errorMsg = action.message)
23- is NamePickedAction -> {
24- val answerName: String?
25- val status = if (state.currentQuestionItem().equalsDisplayName(action.name)) {
26- answerName = action.name
27- Question .Status .CORRECT
28- } else {
29- val correctIndex = state.currentQuestion?.choices?.indexOfFirst { it.id == state.currentQuestion?.itemId }
30- val matchingIndex = match(action.name, state.currentQuestion!! .choices.map { it.displayName() })
31- when (matchingIndex) {
32- NO_MATCH -> {
33- answerName = null
34- Question .Status .INCORRECT
35- }
36- correctIndex -> {
37- answerName = state.currentQuestion!! .choices[matchingIndex].displayName()
38- Question .Status .CORRECT
39- }
40- else -> {
41- answerName = state.currentQuestion!! .choices[matchingIndex].displayName()
42- Question .Status .INCORRECT
43- }
13+ val reducer= castingReducer { state: AppState , action ->
14+ when (action) {
15+ is ActionTypes .INIT -> {
16+ AppState .INITIAL_STATE
17+ }
18+ is FetchingItemsStartedAction -> state.copy(isLoadingItems = true )
19+ is FetchingItemsSuccessAction -> {
20+ state.copy(isLoadingItems = false ,
21+ items = action.itemsHolder.items,
22+ questionTitle = action.itemsHolder.questionTitle,
23+ questions = action.itemsHolder.questions)
24+ }
25+ is FetchingItemsFailedAction -> state.copy(isLoadingItems = false , errorLoadingItems = true , errorMsg = action.message)
26+ is NamePickedAction -> {
27+ val answerName: String?
28+ val status = if (state.currentQuestionItem().equalsDisplayName(action.name)) {
29+ answerName = action.name
30+ Question .Status .CORRECT
31+ } else {
32+ val correctIndex = state.currentQuestion?.choices?.indexOfFirst { it.id == state.currentQuestion?.itemId }
33+ val matchingIndex = match(action.name, state.currentQuestion!! .choices.map { it.displayName() })
34+ when (matchingIndex) {
35+ NO_MATCH -> {
36+ answerName = null
37+ Question .Status .INCORRECT
38+ }
39+ correctIndex -> {
40+ answerName = state.currentQuestion!! .choices[matchingIndex].displayName()
41+ Question .Status .CORRECT
42+ }
43+ else -> {
44+ answerName = state.currentQuestion!! .choices[matchingIndex].displayName()
45+ Question .Status .INCORRECT
4446 }
4547 }
46-
47- val newQuestions = state.questions.toMutableList()
48- newQuestions[state.currentQuestionIndex] = newQuestions[state.currentQuestionIndex].copy(answerName = answerName,
49- status = status,
50- answerNameInterpretedAs = action.name)
51- state.copy(questions = newQuestions, waitingForNextQuestion = true )
52- }
53- is NextQuestionAction -> state.copy(waitingForNextQuestion = false , currentQuestionIndex = state.currentQuestionIndex + 1 )
54- is GameCompleteAction -> state.copy(waitingForNextQuestion = false , currentQuestionIndex = state.currentQuestionIndex + 1 )
55- is StartOverAction , is ResetGameStateAction -> AppState .INITIAL_STATE .copy(settings = state.settings)
56- is StartQuestionTimerAction -> state.copy(questionClock = action.initialValue)
57- is DecrementCountDownAction -> state.copy(questionClock = state.questionClock - 1 )
58- is TimesUpAction -> {
59- val status = Question .Status .TIMES_UP
60- val newQuestions = state.questions.toMutableList()
61- newQuestions[state.currentQuestionIndex] = newQuestions[state.currentQuestionIndex].copy(answerName = " " , status = status)
62- state.copy(questions = newQuestions, waitingForNextQuestion = true , questionClock = - 1 )
6348 }
6449
65- is ChangeNumQuestionsSettingsAction -> state.copy(settings = state.settings.copy(numQuestions = action.num))
66- is ChangeCategorySettingsAction -> state.copy(settings = state.settings.copy(categoryId = action.categoryId))
67- is ChangeMicrophoneModeSettingsAction -> state.copy(settings = state.settings.copy(microphoneMode = action.enabled))
68- is SettingsLoadedAction -> state.copy(settings = action.settings)
50+ val newQuestions = state.questions.toMutableList()
51+ newQuestions[state.currentQuestionIndex] = newQuestions[state.currentQuestionIndex].copy(answerName = answerName,
52+ status = status,
53+ answerNameInterpretedAs = action.name)
54+ state.copy(questions = newQuestions, waitingForNextQuestion = true )
55+ }
56+ is NextQuestionAction -> state.copy(waitingForNextQuestion = false , currentQuestionIndex = state.currentQuestionIndex + 1 )
57+ is GameCompleteAction -> state.copy(waitingForNextQuestion = false , currentQuestionIndex = state.currentQuestionIndex + 1 )
58+ is StartOverAction , is ResetGameStateAction -> AppState .INITIAL_STATE .copy(settings = state.settings)
59+ is StartQuestionTimerAction -> state.copy(questionClock = action.initialValue)
60+ is DecrementCountDownAction -> state.copy(questionClock = state.questionClock - 1 )
61+ is TimesUpAction -> {
62+ val status = Question .Status .TIMES_UP
63+ val newQuestions = state.questions.toMutableList()
64+ newQuestions[state.currentQuestionIndex] = newQuestions[state.currentQuestionIndex].copy(answerName = " " , status = status)
65+ state.copy(questions = newQuestions, waitingForNextQuestion = true , questionClock = - 1 )
66+ }
67+
68+ is ChangeNumQuestionsSettingsAction -> state.copy(settings = state.settings.copy(numQuestions = action.num))
69+ is ChangeCategorySettingsAction -> state.copy(settings = state.settings.copy(categoryId = action.categoryId))
70+ is ChangeMicrophoneModeSettingsAction -> state.copy(settings = state.settings.copy(microphoneMode = action.enabled))
71+ is SettingsLoadedAction -> state.copy(settings = action.settings)
6972
70- is WillowTreeSignInSuccessAction -> state.copy(settings = state.settings.copy(isWillowTree = true ))
71- is WillowTreeSignOutSuccessAction -> state.copy(settings = state.settings.copy(isWillowTree = false ))
72- is LoadAllSettingsAction -> {state}
73+ is WillowTreeSignInSuccessAction -> state.copy(settings = state.settings.copy(isWillowTree = true ))
74+ is WillowTreeSignOutSuccessAction -> state.copy(settings = state.settings.copy(isWillowTree = false ))
75+ is LoadAllSettingsAction -> {
76+ state
77+ }
7378
74- else -> {
75- Logger .d(" Action ${action::class .simpleName} not handled" )
76- state
77- }
79+ else -> {
80+ Logger .d(" Action ${action::class .simpleName} not handled" )
81+ state
7882 }
83+ }
84+ }
7985
0 commit comments