Skip to content

Commit 620f672

Browse files
author
Patrick Jackson
committed
refactor view and presenters into a single file
1 parent 664d7b3 commit 620f672

File tree

14 files changed

+71
-76
lines changed

14 files changed

+71
-76
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ ext {
4848
reduxVersion = '0.2.6'
4949
reduxThunkVersion = '0.2.8'
5050
reduxPresenterMiddlewareVersion = '0.2.7'
51-
reduxReselectVersion = '0.2.7'
51+
reduxReselectVersion = '0.2.8'
5252
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ val reducer: Reducer<AppState> = { state: AppState, action ->
2323
questions = action.itemsHolder.questions)
2424
}
2525
is FetchingItemsFailedAction -> state.copy(isLoadingItems = false, errorLoadingItems = true, errorMsg = action.message)
26-
is UiActions.NamePicked -> {
26+
is NamePickedAction -> {
2727
val answerName: String?
2828
val status = if (state.currentQuestionItem().equalsDisplayName(action.name)) {
2929
answerName = action.name

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

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package com.willowtreeapps.common.ui
22

3+
import com.willowtreeapps.common.boundary.toGameResultsViewState
4+
35

46
interface GameResultsView : GameBaseView {
57
fun showResults(viewState: GameResultsViewState)
68

79
override fun presenter() = gameResultsPresenter
810
}
11+
12+
val gameResultsPresenter = presenter<GameResultsView> {{
13+
withAnyChange { showResults(state.toGameResultsViewState()) }
14+
}}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.willowtreeapps.common.ui
22

3-
import com.willowtreeapps.common.*
3+
import com.willowtreeapps.common.AppState
44
import com.willowtreeapps.common.external.*
5-
import com.willowtreeapps.common.util.isAndroid
6-
import org.reduxkotlin.middleware
75

86

97
typealias GameBaseView = ViewWithProvider<AppState>
@@ -16,17 +14,3 @@ fun <V: GameBaseView> presenter(actions: PresenterBuilder<AppState, V>): Present
1614
fun <V: GameBaseView> presenterWithViewArg(actions: PresenterBuilderWithViewArg<AppState, V>): Presenter<View, AppState> {
1715
return createGenericPresenter(actions) as Presenter<View, AppState>
1816
}
19-
20-
val startPresenter = presenter<StartView> {{
21-
withSingleField({ it.isLoadingItems }) {
22-
if (state.isLoadingItems) {
23-
showLoading()
24-
} else {
25-
hideLoading()
26-
}
27-
}
28-
29-
withSingleField({ it.errorLoadingItems }) {
30-
showError(state.errorMsg)
31-
}
32-
}}

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

Lines changed: 0 additions & 38 deletions
This file was deleted.

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

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

3+
import com.willowtreeapps.common.Question
4+
import com.willowtreeapps.common.boundary.toQuestionViewState
5+
import com.willowtreeapps.common.middleware.UiActions
6+
37
interface QuestionView : GameBaseView {
48
override fun presenter() = questionPresenter
59

@@ -21,3 +25,37 @@ interface QuestionView : GameBaseView {
2125

2226
fun closeMic()
2327
}
28+
29+
val questionPresenter = presenter<QuestionView> {{
30+
withSingleField({ it.questionClock }, { setTimerText(state.toQuestionViewState()) })
31+
32+
withSingleField({
33+
it.currentQuestion?.itemId?.id ?: Any()
34+
}, { showProfile(state.toQuestionViewState()) })
35+
36+
withSingleField({ it.waitingForNextQuestion }) {
37+
if (state.waitingForNextQuestion) {
38+
if (state.settings.microphoneMode) {
39+
closeMic()
40+
if (!state.isGameComplete()) {
41+
store.dispatch(UiActions.NextQuestionDelayed())
42+
}
43+
}
44+
when (state.currentQuestion?.status) {
45+
Question.Status.CORRECT -> {
46+
showCorrectAnswer(state.toQuestionViewState(), state.isGameComplete())
47+
}
48+
Question.Status.INCORRECT -> {
49+
store.dispatch(UiActions.VibrateAction())
50+
showWrongAnswer(state.toQuestionViewState(), state.isGameComplete())
51+
}
52+
Question.Status.TIMES_UP -> {
53+
store.dispatch(UiActions.VibrateAction())
54+
showTimesUp(state.toQuestionViewState(), state.isGameComplete())
55+
}
56+
Question.Status.UNANSWERED -> throw IllegalStateException("Question status cannot be Unanswered when waiting for next round == true")
57+
}
58+
}
59+
}
60+
}}
61+

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

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.willowtreeapps.common.ui
22

33
import com.willowtreeapps.common.AppState
4+
import com.willowtreeapps.common.boundary.toViewState
45
import com.willowtreeapps.common.external.Presenter
56
import com.willowtreeapps.common.external.View
67

@@ -9,4 +10,8 @@ interface SettingsView: GameBaseView {
910
fun askForMicPermissions()
1011

1112
override fun presenter(): Presenter<View, AppState> = settingsPresenter
12-
}
13+
}
14+
15+
val settingsPresenter = presenter<SettingsView> {{
16+
withSingleField({ it.settings }) { showSettings(state.settings.toViewState()) }
17+
}}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,17 @@ interface StartView : GameBaseView {
1212

1313
override fun presenter(): Presenter<View, AppState> = startPresenter
1414
}
15+
16+
val startPresenter = presenter<StartView> {{
17+
withSingleField({ it.isLoadingItems }) {
18+
if (state.isLoadingItems) {
19+
showLoading()
20+
} else {
21+
hideLoading()
22+
}
23+
}
24+
25+
withSingleField({ it.errorLoadingItems }) {
26+
showError(state.errorMsg)
27+
}
28+
}}

0 commit comments

Comments
 (0)