|
1 | 1 | package com.willowtreeapps.common |
2 | 2 |
|
3 | | -import com.beyondeye.reduks.ReducerFn |
4 | 3 | import com.willowtreeapps.common.Actions.* |
5 | 4 | import com.willowtreeapps.common.repo.Profile |
6 | 5 | import com.willowtreeapps.common.util.TimeUtil |
7 | 6 | import kotlin.math.abs |
8 | 7 | import kotlin.random.Random |
9 | 8 |
|
10 | 9 | /** |
11 | | - * Reducers and functions used by reducers are in this file. Functions must be pure functiuns without |
| 10 | + * Reducers and functions used by reducers are in this file. Functions must be pure functions without |
12 | 11 | * side effects. |
13 | 12 | */ |
14 | | - |
15 | | -val reducer = ReducerFn<AppState> { state, action -> |
16 | | - when (action) { |
17 | | - is FetchingProfilesStartedAction -> state.copy(isLoadingProfiles = true) |
18 | | - is FetchingProfilesSuccessAction -> { |
19 | | - val rounds = generateRounds(action.profiles, state.numQuestions) |
20 | | - state.copy(isLoadingProfiles = false, profiles = action.profiles, questions = rounds) |
21 | | - } |
22 | | - is FetchingProfilesFailedAction -> state.copy(isLoadingProfiles = false, errorLoadingProfiles = true, errorMsg = action.message) |
23 | | - is NamePickedAction -> { |
24 | | - val status = if (state.currentQuestionProfile().matches(action.name)) { |
25 | | - Question.Status.CORRECT |
26 | | - } else { |
27 | | - Question.Status.INCORRECT |
| 13 | +fun reducer(state: AppState, action: Any): AppState = |
| 14 | + when (action) { |
| 15 | + is FetchingProfilesStartedAction -> state.copy(isLoadingProfiles = true) |
| 16 | + is FetchingProfilesSuccessAction -> { |
| 17 | + val rounds = generateRounds(action.profiles, state.numQuestions) |
| 18 | + state.copy(isLoadingProfiles = false, profiles = action.profiles, questions = rounds) |
| 19 | + } |
| 20 | + is FetchingProfilesFailedAction -> state.copy(isLoadingProfiles = false, errorLoadingProfiles = true, errorMsg = action.message) |
| 21 | + is NamePickedAction -> { |
| 22 | + val status = if (state.currentQuestionProfile().matches(action.name)) { |
| 23 | + Question.Status.CORRECT |
| 24 | + } else { |
| 25 | + Question.Status.INCORRECT |
| 26 | + } |
| 27 | + val newQuestions = state.questions.toMutableList() |
| 28 | + newQuestions[state.currentQuestionIndex] = newQuestions[state.currentQuestionIndex].copy(answerName = action.name, status = status) |
| 29 | + state.copy(questions = newQuestions, waitingForNextQuestion = true) |
| 30 | + } |
| 31 | + is NextQuestionAction -> state.copy(waitingForNextQuestion = false, currentQuestionIndex = state.currentQuestionIndex + 1) |
| 32 | + is GameCompleteAction -> state.copy(waitingForResultsTap = true, waitingForNextQuestion = false, currentQuestionIndex = state.currentQuestionIndex + 1) |
| 33 | + is StartOverAction, is ResetGameStateAction -> AppState.INITIAL_STATE |
| 34 | + is StartQuestionTimerAction -> state.copy(questionClock = action.initialValue) |
| 35 | + is DecrementCountDownAction -> state.copy(questionClock = state.questionClock - 1) |
| 36 | + is TimesUpAction -> { |
| 37 | + val status = Question.Status.TIMES_UP |
| 38 | + val newQuestions = state.questions.toMutableList() |
| 39 | + newQuestions[state.currentQuestionIndex] = newQuestions[state.currentQuestionIndex].copy(answerName = "", status = status) |
| 40 | + state.copy(questions = newQuestions, waitingForNextQuestion = true, questionClock = -1) |
28 | 41 | } |
29 | | - val newQuestions = state.questions.toMutableList() |
30 | | - newQuestions[state.currentQuestionIndex] = newQuestions[state.currentQuestionIndex].copy(answerName = action.name, status = status) |
31 | | - state.copy(questions = newQuestions, waitingForNextQuestion = true) |
32 | | - } |
33 | | - is NextQuestionAction -> state.copy(waitingForNextQuestion = false, currentQuestionIndex = state.currentQuestionIndex + 1) |
34 | | - is GameCompleteAction -> state.copy(waitingForResultsTap = true, waitingForNextQuestion = false, currentQuestionIndex = state.currentQuestionIndex + 1) |
35 | | - is StartOverAction, is ResetGameStateAction -> AppState.INITIAL_STATE |
36 | | - is StartQuestionTimerAction -> state.copy(questionClock = action.initialValue) |
37 | | - is DecrementCountDownAction -> state.copy(questionClock = state.questionClock - 1) |
38 | | - is TimesUpAction -> { |
39 | | - val status = Question.Status.TIMES_UP |
40 | | - val newQuestions = state.questions.toMutableList() |
41 | | - newQuestions[state.currentQuestionIndex] = newQuestions[state.currentQuestionIndex].copy(answerName = "", status = status) |
42 | | - state.copy(questions = newQuestions, waitingForNextQuestion = true, questionClock = -1) |
43 | | - } |
44 | 42 |
|
45 | | - else -> throw AssertionError("Action ${action::class.simpleName} not handled") |
46 | | - } |
47 | | -} |
| 43 | + else -> throw AssertionError("Action ${action::class.simpleName} not handled") |
| 44 | + } |
48 | 45 |
|
49 | 46 | fun generateRounds(profiles: List<Profile>, n: Int): List<Question> = |
50 | 47 | profiles.takeRandomDistinct(n) |
|
0 commit comments