3
3
package com.squareup.sample.dungeon
4
4
5
5
import android.os.Vibrator
6
+ import com.squareup.sample.dungeon.GameSessionWorkflow.Output
7
+ import com.squareup.sample.dungeon.GameSessionWorkflow.Output.NewBoard
6
8
import com.squareup.sample.dungeon.GameSessionWorkflow.Props
7
9
import com.squareup.sample.dungeon.GameSessionWorkflow.State
8
10
import com.squareup.sample.dungeon.GameSessionWorkflow.State.GameOver
@@ -21,7 +23,10 @@ import com.squareup.workflow1.ui.Screen
21
23
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
22
24
import com.squareup.workflow1.ui.modal.AlertContainerScreen
23
25
import com.squareup.workflow1.ui.modal.AlertScreen
26
+ import com.squareup.workflow1.ui.modal.AlertScreen.Button.NEGATIVE
27
+ import com.squareup.workflow1.ui.modal.AlertScreen.Button.NEUTRAL
24
28
import com.squareup.workflow1.ui.modal.AlertScreen.Button.POSITIVE
29
+ import com.squareup.workflow1.ui.modal.AlertScreen.Event.ButtonClicked
25
30
26
31
typealias BoardPath = String
27
32
@@ -33,7 +38,7 @@ class GameSessionWorkflow(
33
38
private val gameWorkflow : GameWorkflow ,
34
39
private val vibrator : Vibrator ,
35
40
private val boardLoader : BoardLoader
36
- ) : StatefulWorkflow<Props, State, Nothing , AlertContainerScreen<Any>>() {
41
+ ) : StatefulWorkflow<Props, State, Output , AlertContainerScreen<Any>>() {
37
42
38
43
data class Props (
39
44
val boardPath : BoardPath ,
@@ -46,6 +51,10 @@ class GameSessionWorkflow(
46
51
data class GameOver (val board : Board ) : State()
47
52
}
48
53
54
+ sealed class Output {
55
+ object NewBoard : Output()
56
+ }
57
+
49
58
override fun initialState (
50
59
props : Props ,
51
60
snapshot : Snapshot ?
@@ -75,10 +84,20 @@ class GameSessionWorkflow(
75
84
val gameScreen = context.renderChild(gameWorkflow, gameInput) { noAction() }
76
85
77
86
val gameOverDialog = AlertScreen (
78
- buttons = mapOf (POSITIVE to " Restart" ),
87
+ buttons = mapOf (POSITIVE to " Restart" , NEUTRAL to " New board " ),
79
88
message = " You've been eaten, try again." ,
80
89
cancelable = false ,
81
- onEvent = { context.actionSink.send(restartGame()) }
90
+ onEvent = {
91
+ if (it is ButtonClicked ) {
92
+ context.actionSink.send(
93
+ when (it.button) {
94
+ POSITIVE -> restartGame()
95
+ NEUTRAL -> newBoard()
96
+ NEGATIVE -> noAction()
97
+ }
98
+ )
99
+ }
100
+ }
82
101
)
83
102
84
103
AlertContainerScreen (gameScreen, gameOverDialog)
@@ -112,6 +131,8 @@ class GameSessionWorkflow(
112
131
113
132
private fun restartGame () = action(" restartGame" ) { state = Loading }
114
133
134
+ private fun newBoard () = action(" newBoard" ) { setOutput(NewBoard )}
135
+
115
136
private fun vibrate (durationMs : Long ) {
116
137
@Suppress(" DEPRECATION" )
117
138
vibrator.vibrate(durationMs)
0 commit comments