33package com.squareup.sample.dungeon
44
55import android.os.Vibrator
6+ import com.squareup.sample.dungeon.GameSessionWorkflow.Output
7+ import com.squareup.sample.dungeon.GameSessionWorkflow.Output.NewBoard
68import com.squareup.sample.dungeon.GameSessionWorkflow.Props
79import com.squareup.sample.dungeon.GameSessionWorkflow.State
810import com.squareup.sample.dungeon.GameSessionWorkflow.State.GameOver
@@ -21,7 +23,10 @@ import com.squareup.workflow1.ui.Screen
2123import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
2224import com.squareup.workflow1.ui.modal.AlertContainerScreen
2325import 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
2428import com.squareup.workflow1.ui.modal.AlertScreen.Button.POSITIVE
29+ import com.squareup.workflow1.ui.modal.AlertScreen.Event.ButtonClicked
2530
2631typealias BoardPath = String
2732
@@ -33,7 +38,7 @@ class GameSessionWorkflow(
3338 private val gameWorkflow : GameWorkflow ,
3439 private val vibrator : Vibrator ,
3540 private val boardLoader : BoardLoader
36- ) : StatefulWorkflow<Props, State, Nothing , AlertContainerScreen<Any>>() {
41+ ) : StatefulWorkflow<Props, State, Output , AlertContainerScreen<Any>>() {
3742
3843 data class Props (
3944 val boardPath : BoardPath ,
@@ -46,6 +51,10 @@ class GameSessionWorkflow(
4651 data class GameOver (val board : Board ) : State()
4752 }
4853
54+ sealed class Output {
55+ object NewBoard : Output()
56+ }
57+
4958 override fun initialState (
5059 props : Props ,
5160 snapshot : Snapshot ?
@@ -75,10 +84,20 @@ class GameSessionWorkflow(
7584 val gameScreen = context.renderChild(gameWorkflow, gameInput) { noAction() }
7685
7786 val gameOverDialog = AlertScreen (
78- buttons = mapOf (POSITIVE to " Restart" ),
87+ buttons = mapOf (POSITIVE to " Restart" , NEUTRAL to " New board " ),
7988 message = " You've been eaten, try again." ,
8089 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+ }
82101 )
83102
84103 AlertContainerScreen (gameScreen, gameOverDialog)
@@ -112,6 +131,8 @@ class GameSessionWorkflow(
112131
113132 private fun restartGame () = action(" restartGame" ) { state = Loading }
114133
134+ private fun newBoard () = action(" newBoard" ) { setOutput(NewBoard ) }
135+
115136 private fun vibrate (durationMs : Long ) {
116137 @Suppress(" DEPRECATION" )
117138 vibrator.vibrate(durationMs)
0 commit comments