Skip to content

Commit 04a7460

Browse files
committed
adds new board to game over screen
1 parent ff53530 commit 04a7460

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

samples/dungeon/app/src/main/java/com/squareup/sample/dungeon/DungeonAppWorkflow.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ class DungeonAppWorkflow(
6262

6363
is PlayingGame -> {
6464
val sessionProps = GameSessionWorkflow.Props(renderState.boardPath, renderProps.paused)
65-
val gameScreen = context.renderChild(gameSessionWorkflow, sessionProps)
65+
val gameScreen = context.renderChild(gameSessionWorkflow, sessionProps) {
66+
action { state = LoadingBoardList }
67+
}
6668
gameScreen
6769
}
6870
}

samples/dungeon/app/src/main/java/com/squareup/sample/dungeon/GameSessionWorkflow.kt

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
package com.squareup.sample.dungeon
44

55
import android.os.Vibrator
6+
import com.squareup.sample.dungeon.GameSessionWorkflow.Output
7+
import com.squareup.sample.dungeon.GameSessionWorkflow.Output.NewBoard
68
import com.squareup.sample.dungeon.GameSessionWorkflow.Props
79
import com.squareup.sample.dungeon.GameSessionWorkflow.State
810
import com.squareup.sample.dungeon.GameSessionWorkflow.State.GameOver
@@ -21,7 +23,10 @@ import com.squareup.workflow1.ui.Screen
2123
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
2224
import com.squareup.workflow1.ui.modal.AlertContainerScreen
2325
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
2428
import com.squareup.workflow1.ui.modal.AlertScreen.Button.POSITIVE
29+
import com.squareup.workflow1.ui.modal.AlertScreen.Event.ButtonClicked
2530

2631
typealias 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

Comments
 (0)