@@ -18,7 +18,6 @@ import com.squareup.sample.gameworkflow.SyncState.SAVING
18
18
import com.squareup.workflow1.Snapshot
19
19
import com.squareup.workflow1.StatefulWorkflow
20
20
import com.squareup.workflow1.Workflow
21
- import com.squareup.workflow1.action
22
21
import com.squareup.workflow1.runningWorker
23
22
import com.squareup.workflow1.rx2.asWorker
24
23
import com.squareup.workflow1.ui.Screen
@@ -88,8 +87,12 @@ class RealRunGameWorkflow(
88
87
namePrompt = NewGameScreen (
89
88
renderState.defaultXName,
90
89
renderState.defaultOName,
91
- onCancel = context.eventHandler { setOutput(CanceledStart ) },
92
- onStartGame = context.eventHandler { x, o -> state = Playing (PlayerInfo (x, o)) }
90
+ onCancel = context.safeEventHandler<NewGame > {
91
+ setOutput(CanceledStart )
92
+ },
93
+ onStartGame = context.safeEventHandler<NewGame , String , String > { _, x, o ->
94
+ state = Playing (PlayerInfo (x, o))
95
+ }
93
96
)
94
97
)
95
98
}
@@ -119,15 +122,11 @@ class RealRunGameWorkflow(
119
122
message = " Do you really want to concede the game?" ,
120
123
positive = " I Quit" ,
121
124
negative = " No" ,
122
- confirmQuit = context.eventHandler {
123
- (state as ? MaybeQuitting )?.let { oldState ->
124
- state = MaybeQuittingForSure (oldState.playerInfo, oldState.completedGame)
125
- }
125
+ confirmQuit = context.safeEventHandler<MaybeQuitting > { oldState ->
126
+ state = MaybeQuittingForSure (oldState.playerInfo, oldState.completedGame)
126
127
},
127
- continuePlaying = context.eventHandler {
128
- (state as ? MaybeQuitting )?.let { oldState ->
129
- state = Playing (oldState.playerInfo, oldState.completedGame.lastTurn)
130
- }
128
+ continuePlaying = context.safeEventHandler<MaybeQuitting > { oldState ->
129
+ state = Playing (oldState.playerInfo, oldState.completedGame.lastTurn)
131
130
}
132
131
)
133
132
)
@@ -142,15 +141,11 @@ class RealRunGameWorkflow(
142
141
message = " Really?" ,
143
142
positive = " Yes!!" ,
144
143
negative = " Sigh, no" ,
145
- confirmQuit = context.eventHandler {
146
- (state as ? MaybeQuittingForSure )?.let { oldState ->
147
- state = GameOver (oldState.playerInfo, oldState.completedGame)
148
- }
144
+ confirmQuit = context.safeEventHandler<MaybeQuittingForSure > { oldState ->
145
+ state = GameOver (oldState.playerInfo, oldState.completedGame)
149
146
},
150
- continuePlaying = context.eventHandler {
151
- (state as ? MaybeQuittingForSure )?.let { oldState ->
152
- state = Playing (oldState.playerInfo, oldState.completedGame.lastTurn)
153
- }
147
+ continuePlaying = context.safeEventHandler<MaybeQuittingForSure > { oldState ->
148
+ state = Playing (oldState.playerInfo, oldState.completedGame.lastTurn)
154
149
}
155
150
)
156
151
)
@@ -169,43 +164,37 @@ class RealRunGameWorkflow(
169
164
renderState,
170
165
onTrySaveAgain = context.trySaveAgain(),
171
166
onPlayAgain = context.playAgain(),
172
- onExit = context.eventHandler { setOutput(FinishedPlaying ) }
167
+ onExit = context.safeEventHandler< GameOver > { setOutput(FinishedPlaying ) }
173
168
)
174
169
)
175
170
}
176
171
}
177
172
178
- private fun stopPlaying (game : CompletedGame ) = action {
179
- val oldState = state as Playing
173
+ private fun stopPlaying (game : CompletedGame ) = safeAction<Playing >(" stopPlaying" ) { oldState ->
180
174
state = when (game.ending) {
181
175
Quitted -> MaybeQuitting (oldState.playerInfo, game)
182
176
else -> GameOver (oldState.playerInfo, game)
183
177
}
184
178
}
185
179
186
- private fun handleLogGame (result : GameLog .LogResult ) = action {
187
- val oldState = state as GameOver
180
+ private fun handleLogGame (result : GameLog .LogResult ) = safeAction<GameOver > { oldState ->
188
181
state = when (result) {
189
182
TRY_LATER -> oldState.copy(syncState = SAVE_FAILED )
190
183
LOGGED -> oldState.copy(syncState = SAVED )
191
184
}
192
185
}
193
186
194
- private fun RenderContext.playAgain () = eventHandler {
195
- (state as ? GameOver )?.let { oldState ->
196
- val (x, o) = oldState.playerInfo
197
- state = NewGame (x, o)
198
- }
187
+ private fun RenderContext.playAgain () = safeEventHandler<GameOver > { oldState ->
188
+ val (x, o) = oldState.playerInfo
189
+ state = NewGame (x, o)
199
190
}
200
191
201
- private fun RenderContext.trySaveAgain () = eventHandler {
202
- (state as ? GameOver )?.let { oldState ->
203
- check(oldState.syncState == SAVE_FAILED ) {
204
- " Should only fire trySaveAgain in syncState $SAVE_FAILED , " +
205
- " was ${oldState.syncState} "
206
- }
207
- state = oldState.copy(syncState = SAVING )
192
+ private fun RenderContext.trySaveAgain () = safeEventHandler<GameOver > { oldState ->
193
+ check(oldState.syncState == SAVE_FAILED ) {
194
+ " Should only fire trySaveAgain in syncState $SAVE_FAILED , " +
195
+ " was ${oldState.syncState} "
208
196
}
197
+ state = oldState.copy(syncState = SAVING )
209
198
}
210
199
211
200
override fun snapshotState (state : RunGameState ): Snapshot = state.toSnapshot()
0 commit comments