Skip to content

Commit fec4d58

Browse files
committed
fix game results not showing
1 parent 06693a4 commit fec4d58

File tree

5 files changed

+39
-40
lines changed

5 files changed

+39
-40
lines changed

app/src/main/java/com/willowtreeapps/namegame/store/QuestionFragment.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
182182

183183
private fun fadeNextButton(after: () -> Unit) {
184184
btn_next.animate().alpha(0f).withEndAction {
185+
lastCorrectBtn?.alpha = 0f
185186
btn_next.visibility = View.GONE
186187
after()
187188
}

common/src/commonMain/kotlin/com/willowtreeapps/common/GameEngine.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class GameEngine(navigator: Navigator, application: Any = Any(),
1818
val vibrateUtil = VibrateUtil(application)
1919

2020
val appStore by lazy {
21-
SimpleStore(AppState.INITIAL_STATE, reducer)
21+
SimpleStore(AppState.INITIAL_STATE, ::reducer)
2222
.applyMiddleware(::thunkMiddleware,
2323
viewEffectsMiddleware::dispatch,
2424
navigationMiddleware::dispatch)

common/src/commonMain/kotlin/com/willowtreeapps/common/Reducers.kt

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
11
package com.willowtreeapps.common
22

3-
import com.beyondeye.reduks.ReducerFn
43
import com.willowtreeapps.common.Actions.*
54
import com.willowtreeapps.common.repo.Profile
65
import com.willowtreeapps.common.util.TimeUtil
76
import kotlin.math.abs
87
import kotlin.random.Random
98

109
/**
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
1211
* side effects.
1312
*/
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)
2841
}
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-
}
4442

45-
else -> throw AssertionError("Action ${action::class.simpleName} not handled")
46-
}
47-
}
43+
else -> throw AssertionError("Action ${action::class.simpleName} not handled")
44+
}
4845

4946
fun generateRounds(profiles: List<Profile>, n: Int): List<Question> =
5047
profiles.takeRandomDistinct(n)

common/src/commonMain/kotlin/com/willowtreeapps/common/ui/GameResultsPresenter.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.willowtreeapps.common.ui
22

3+
import com.beyondeye.reduks.SelectorSubscriberFn
34
import com.beyondeye.reduks.Store
4-
import com.beyondeye.reduks.StoreSubscriber
5-
import com.beyondeye.reduks.StoreSubscriberFn
65
import com.willowtreeapps.common.Actions
76
import com.willowtreeapps.common.AppState
87
import com.willowtreeapps.common.Presenter
8+
import com.willowtreeapps.common.boundary.toGameResultsViewState
99

1010
class GameResultsPresenter(val store: Store<AppState>) : Presenter<GameResultsView>() {
11-
override fun makeSubscriber(): StoreSubscriber<AppState> {
12-
return StoreSubscriberFn { }
11+
override fun makeSubscriber() = SelectorSubscriberFn(store) {
12+
withAnyChange { view?.showResults(state.toGameResultsViewState()) }
1313
}
1414

1515
fun startOverTapped() {

common/src/commonMain/kotlin/com/willowtreeapps/common/ui/PresenterFactory.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import kotlin.coroutines.CoroutineContext
88
* PresenterFactory that creates presenters for all views in the application.
99
* Each view must attach/detach itself as it becomes visible/not visible.
1010
* Attaching returns a presenter to the view.
11+
* PresenterFactory subscribes to changes in state, and passes state to presenters.
1112
*/
1213
internal class PresenterFactory(private val gameEngine: GameEngine, networkContext: CoroutineContext) : StoreSubscriber<AppState> {
1314

@@ -21,7 +22,7 @@ internal class PresenterFactory(private val gameEngine: GameEngine, networkConte
2122
private val gameResultsPresenter by lazy { GameResultsPresenter(gameEngine.appStore) }
2223

2324

24-
fun <T : View?> attachView(view: T): Presenter<out View?> {
25+
fun <T : View> attachView(view: T): Presenter<out View?> {
2526
Logger.d("AttachView: $view")
2627
if (subscription == null) {
2728
subscription = gameEngine.appStore.subscribe(this)

0 commit comments

Comments
 (0)