Skip to content

Commit 321cd58

Browse files
authored
Merge pull request #17 from patjackson52/feature/store-state
tweaks
2 parents d9e2130 + 63d9f1c commit 321cd58

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ data class AppState(val isLoadingProfiles: Boolean = false,
99
val currentQuestionIndex: Int = 0,
1010
val waitingForNextQuestion: Boolean = false,
1111
val waitingForResultsTap: Boolean = false,
12-
val questionClock: Int = 0,
12+
val questionClock: Int = -1,
1313
val questions: List<Question> = listOf(),
1414
val settings: UserSettings = UserSettings.defaults()) {
1515
companion object {
@@ -19,7 +19,7 @@ data class AppState(val isLoadingProfiles: Boolean = false,
1919
fun Question.profile(): Profile = profiles.find { ProfileId(it.id) == this.profileId }!!
2020

2121
val timerText: String
22-
get() = if (questionClock >= 0) questionClock.toString() else "TIME'S UP!!"
22+
get() = if (questionClock < 0) "" else if (questionClock >= 0) questionClock.toString() else "TIME'S UP!!"
2323

2424
val currentQuestion: Question?
2525
get() = if (questions.size > currentQuestionIndex)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class GameEngine(navigator: Navigator,
2020
private val viewEffectsMiddleware = ViewEffectsMiddleware()
2121
private val presenterFactory by lazy { PresenterFactory(this, networkContext) }
2222
val vibrateUtil = VibrateUtil(application)
23-
private val settingsMiddleware by lazy { SettingsMiddleware(LocalStorageSettingsRepository(userSettings(application))) }
23+
private val settingsMiddleware by lazy { SettingsMiddleware(LocalStorageSettingsRepository(userSettings(application)), networkContext) }
2424

2525
val appStore by lazy {
2626
SimpleStore(AppState.INITIAL_STATE, ::reducer)

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import com.beyondeye.reduks.ThunkImpl
66
import kotlinx.coroutines.*
77
import kotlin.coroutines.CoroutineContext
88

9-
class TimerThunks(private val networkContext: CoroutineContext, val store: Store<AppState>) : CoroutineScope {
10-
override val coroutineContext = networkContext + Job()
9+
class TimerThunks(private val backgroundContext: CoroutineContext, val store: Store<AppState>) : CoroutineScope {
10+
override val coroutineContext = backgroundContext + Job()
1111
private var timerJob: Job? = null
1212

1313
/**
@@ -16,17 +16,16 @@ class TimerThunks(private val networkContext: CoroutineContext, val store: Store
1616
* the timer and start the new one.
1717
*/
1818
fun startCountDownTimer(initialValue: Int): ThunkImpl<AppState> = ThunkFn { dispatcher, state ->
19-
store.dispatch(Actions.StartQuestionTimerAction(initialValue))
20-
if (timerJob != null) {
21-
timerJob?.cancel()
22-
}
23-
timerJob = launchTimer(1000, CoroutineScope(coroutineContext)) {
19+
if (timerJob == null || timerJob?.isActive == false) {
20+
store.dispatch(Actions.StartQuestionTimerAction(initialValue))
21+
timerJob = launchTimer(1000, CoroutineScope(coroutineContext)) {
2422

25-
if (store.state.questionClock > 0) {
26-
store.dispatch(Actions.DecrementCountDownAction())
27-
} else {
28-
store.dispatch(Actions.TimesUpAction())
29-
timerJob?.cancel()
23+
if (store.state.questionClock > 0) {
24+
store.dispatch(Actions.DecrementCountDownAction())
25+
} else {
26+
store.dispatch(Actions.TimesUpAction())
27+
timerJob?.cancel()
28+
}
3029
}
3130
}
3231
Any()

common/src/commonMain/kotlin/com/willowtreeapps/common/middleware/SettingsMiddleware.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,28 @@ import com.willowtreeapps.common.Actions
55
import com.willowtreeapps.common.Actions.ChangeNumQuestionsSettingsAction
66
import com.willowtreeapps.common.AppState
77
import com.willowtreeapps.common.repo.LocalStorageSettingsRepository
8+
import kotlinx.coroutines.CoroutineScope
9+
import kotlinx.coroutines.Job
10+
import kotlinx.coroutines.launch
11+
import kotlin.coroutines.CoroutineContext
812

913
/**
1014
* Save and Loads user settings from local storage
1115
*/
12-
class SettingsMiddleware(private val settings: LocalStorageSettingsRepository) {
16+
class SettingsMiddleware(private val settings: LocalStorageSettingsRepository,
17+
private val backgroundContext: CoroutineContext): CoroutineScope {
18+
override val coroutineContext: CoroutineContext
19+
get() = backgroundContext + Job()
20+
1321

1422
fun dispatch(store: Store<AppState>, nextDispatcher: (Any) -> Any, action: Any): Any {
15-
when (action) {
16-
is ChangeNumQuestionsSettingsAction -> settings.saveNumRounds(action.num)
23+
launch {
24+
when (action) {
25+
is ChangeNumQuestionsSettingsAction -> settings.saveNumRounds(action.num)
1726

18-
is Actions.LoadAllSettingsAction -> {
19-
store.dispatch(ChangeNumQuestionsSettingsAction(settings.loadNumRounds()))
27+
is Actions.LoadAllSettingsAction -> {
28+
store.dispatch(ChangeNumQuestionsSettingsAction(settings.loadNumRounds()))
29+
}
2030
}
2131
}
2232
return nextDispatcher(action)

0 commit comments

Comments
 (0)