Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object PoemsBrowserWorkflow :
}

private fun choosePoem(index: SelectedPoem) = action("goToPoem") {
nextState = index
state = index
}

private val clearSelection = choosePoem(-1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ object AreYouSureWorkflow : StatefulWorkflow<Unit, State, Finished, AlertContain

override fun snapshotState(state: State) = Snapshot.EMPTY

private val maybeQuit = action { nextState = Quitting }
private val maybeQuit = action { state = Quitting }
private val confirmQuit = action { setOutput(Finished) }
private val cancelQuit = action { nextState = Running }
private val cancelQuit = action { state = Running }
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ object HelloBackButtonWorkflow : StatefulWorkflow<Unit, State, Nothing, Renderin
override fun snapshotState(state: State): Snapshot = Snapshot.EMPTY

private val advance = action {
nextState = when (nextState) {
state = when (state) {
Able -> Baker
Baker -> Charlie
Charlie -> Able
}
}

private val retreat = action {
nextState = when (nextState) {
state = when (state) {
Able -> throw IllegalStateException()
Baker -> Able
Charlie -> Baker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ class DungeonAppWorkflow(
override fun snapshotState(state: State): Snapshot = Snapshot.EMPTY

private fun displayBoards(boards: Map<String, Board>) = action {
nextState = ChoosingBoard(boards.toList())
state = ChoosingBoard(boards.toList())
}

private fun selectBoard(index: Int) = action {
// No-op if we're not in the ChoosingBoard state.
val boards = (nextState as? ChoosingBoard)?.boards ?: return@action
nextState = PlayingGame(boards[index].first)
val boards = (state as? ChoosingBoard)?.boards ?: return@action
state = PlayingGame(boards[index].first)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class GameSessionWorkflow(

private class StartRunning(val board: Board) : WorkflowAction<State, Nothing> {
override fun Updater<State, Nothing>.apply() {
nextState = Running(board)
state = Running(board)
}
}

Expand All @@ -111,7 +111,7 @@ class GameSessionWorkflow(
when (output) {
Vibrate -> vibrate(50)
PlayerWasEaten -> {
nextState = GameOver(board)
state = GameOver(board)
vibrate(20)
vibrate(20)
vibrate(20)
Expand All @@ -121,7 +121,7 @@ class GameSessionWorkflow(
}
}

private fun restartGame() = action("restartGame") { nextState = Loading }
private fun restartGame() = action("restartGame") { state = Loading }

private fun vibrate(durationMs: Long) {
@Suppress("DEPRECATION")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ class AiWorkflow(

private val updateDirection = action("updateDirection") {
// Rotate 90 degrees.
val newDirection = when (nextState.direction) {
val newDirection = when (state.direction) {
UP -> RIGHT
RIGHT -> DOWN
DOWN -> LEFT
LEFT -> UP
}

nextState = nextState.copy(direction = newDirection)
state = state.copy(direction = newDirection)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ class GameWorkflow(

// Check if AI captured player.
if (newGame.isPlayerEaten) {
nextState = nextState.copy(game = newGame)
state = state.copy(game = newGame)
setOutput(PlayerWasEaten)
} else {
nextState = nextState.copy(game = newGame)
state = state.copy(game = newGame)
output?.let { setOutput(it) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ class PlayerWorkflow(

class StartMoving(private val direction: Direction) : Action() {
override fun Updater<Movement, Nothing>.apply() {
nextState += direction
state += direction
}
}

class StopMoving(private val direction: Direction) : Action() {
override fun Updater<Movement, Nothing>.apply() {
nextState -= direction
state -= direction
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,20 @@ class ShakeableTimeMachineWorkflow<in P, O : Any, out R : Any>(
}

private val onShake = action {
nextState = PlayingBack(Duration.INFINITE)
state = PlayingBack(Duration.INFINITE)
}

private inner class SeekAction(
private val newPosition: Duration
) : WorkflowAction<State, O> {
override fun Updater<State, O>.apply() {
nextState = PlayingBack(newPosition)
state = PlayingBack(newPosition)
}
}

private inner class ResumeRecordingAction : WorkflowAction<State, O> {
override fun Updater<State, O>.apply() {
nextState = Recording
state = Recording
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TimeMachineWorkflowTest {
val delegateWorkflow = Workflow.stateful<String, Nothing, DelegateRendering>(
initialState = "initial",
render = { state ->
val sink: Sink<String> = makeEventSink { nextState = it }
val sink: Sink<String> = makeEventSink { this.state = it }
DelegateRendering(state, setState = { sink.send(it) })
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ class BlinkingCursorWorkflow(
override fun snapshotState(state: Boolean): Snapshot = Snapshot.EMPTY

private fun setCursorShowing(showing: Boolean) = action {
nextState = showing
state = showing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ class HelloTerminalWorkflow : TerminalWorkflow,
private fun onKeystroke(key: KeyStroke): HelloTerminalAction = action {
when {
key.character == 'Q' -> setOutput(0)
key.keyType == Backspace -> nextState = nextState.backspace()
key.character != null -> nextState = nextState.append(key.character!!)
key.keyType == Backspace -> state = state.backspace()
key.character != null -> state = state.append(key.character!!)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ class EditTextWorkflow : StatefulWorkflow<EditTextProps, EditTextState, String,
) = action {
when (key.keyType) {
Character -> {
nextState = moveCursor(props, nextState, 1)
props.text.insertCharAt(nextState.cursorPosition, key.character!!)
state = moveCursor(props, state, 1)
props.text.insertCharAt(state.cursorPosition, key.character!!)
}

Backspace -> {
if (props.text.isNotEmpty()) {
nextState = moveCursor(props, nextState, -1)
props.text.removeRange(nextState.cursorPosition - 1, nextState.cursorPosition)
state = moveCursor(props, state, -1)
props.text.removeRange(state.cursorPosition - 1, state.cursorPosition)
}
}
ArrowLeft -> nextState = moveCursor(props, nextState, -1)
ArrowRight -> nextState = moveCursor(props, nextState, 1)
ArrowLeft -> state = moveCursor(props, state, -1)
ArrowRight -> state = moveCursor(props, state, 1)
else -> {
// Nothing to do.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,24 @@ class TodoWorkflow : TerminalWorkflow,
private fun onKeystroke(key: KeyStroke) = action {
@Suppress("NON_EXHAUSTIVE_WHEN")
when (key.keyType) {
ArrowUp -> nextState = nextState.moveFocusUp()
ArrowDown -> nextState = nextState.moveFocusDown()
Enter -> if (nextState.focusedField > TITLE_FIELD_INDEX) {
nextState = nextState.toggleChecked(nextState.focusedField)
ArrowUp -> state = state.moveFocusUp()
ArrowDown -> state = state.moveFocusDown()
Enter -> if (state.focusedField > TITLE_FIELD_INDEX) {
state = state.toggleChecked(state.focusedField)
}
}
}
}

private fun updateTitle(newTitle: String): TodoAction = action {
nextState = nextState.copy(title = newTitle)
state = state.copy(title = newTitle)
}

private fun setLabel(
index: Int,
text: String
): TodoAction = action {
nextState = nextState.copy(items = nextState.items.mapIndexed { i, item ->
state = state.copy(items = state.items.mapIndexed { i, item ->
if (index == i) item.copy(label = text) else item
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object HelloWorkflow : StatefulWorkflow<Unit, State, Nothing, Rendering>() {
override fun snapshotState(state: State): Snapshot = Snapshot.of(if (state == Hello) 1 else 0)

private val helloAction = action {
nextState = when (nextState) {
state = when (state) {
Hello -> Goodbye
Goodbye -> Hello
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object HelloWorkflow : StatefulWorkflow<Unit, State, Nothing, Rendering>() {
override fun snapshotState(state: State): Snapshot = Snapshot.of(if (state == Hello) 1 else 0)

private val helloAction = action {
nextState = when (nextState) {
state = when (state) {
Hello -> Goodbye
Goodbye -> Hello
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ object AppWorkflow : StatefulWorkflow<Unit, State, Nothing, Rendering>() {
data class CommitNewRowAction(val index: Int) : Action()

override fun Updater<State, Nothing>.apply() {
nextState = when (this@Action) {
ShowAddRowAction -> ChooseNewRow(nextState.rows)
is CommitNewRowAction -> ShowList(nextState.rows + allRowTypes[index])
state = when (this@Action) {
ShowAddRowAction -> ChooseNewRow(state.rows)
is CommitNewRowAction -> ShowList(state.rows + allRowTypes[index])
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ object EditableListWorkflow : StatefulWorkflow<Props, State, Nothing, Rendering>
position: Int,
newValue: Any?
) = action {
nextState = nextState.copy(rowValues = nextState.rowValues.mapIndexed { index, value ->
state = state.copy(rowValues = state.rowValues.mapIndexed { index, value ->
if (index == position) value.withValue(newValue) else value
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ internal sealed class Action : WorkflowAction<AuthState, AuthResult> {
final override fun Updater<AuthState, AuthResult>.apply() {
when (this@Action) {
is SubmitLogin -> {
nextState = when {
state = when {
email.isValidEmail -> Authorizing(email, password)
else -> LoginPrompt(email.emailValidationErrorMessage)
}
Expand All @@ -104,20 +104,20 @@ internal sealed class Action : WorkflowAction<AuthState, AuthResult> {

is HandleAuthResponse -> {
when {
response.isLoginFailure -> nextState = LoginPrompt(response.errorMessage)
response.twoFactorRequired -> nextState = SecondFactorPrompt(response.token)
response.isLoginFailure -> state = LoginPrompt(response.errorMessage)
response.twoFactorRequired -> state = SecondFactorPrompt(response.token)
else -> setOutput(Authorized(response.token))
}
}

is SubmitSecondFactor -> nextState = AuthorizingSecondFactor(tempToken, secondFactor)
is SubmitSecondFactor -> state = AuthorizingSecondFactor(tempToken, secondFactor)

CancelSecondFactor -> nextState = LoginPrompt()
CancelSecondFactor -> state = LoginPrompt()

is HandleSecondFactorResponse -> {
when {
response.isSecondFactorFailure ->
nextState = SecondFactorPrompt(tempToken, response.errorMessage)
state = SecondFactorPrompt(tempToken, response.errorMessage)
else -> setOutput(Authorized(response.token))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,50 +104,50 @@ sealed class Action : WorkflowAction<RunGameState, RunGameResult> {
when (this@Action) {
CancelNewGame -> setOutput(CanceledStart)

is StartGame -> nextState = Playing(PlayerInfo(x, o))
is StartGame -> state = Playing(PlayerInfo(x, o))

is StopPlaying -> {
val oldState = nextState as Playing
nextState = when (game.ending) {
val oldState = state as Playing
state = when (game.ending) {
Quitted -> MaybeQuitting(oldState.playerInfo, game)
else -> GameOver(oldState.playerInfo, game)
}
}

ConfirmQuit -> {
val oldState = nextState as MaybeQuitting
nextState = MaybeQuittingForSure(oldState.playerInfo, oldState.completedGame)
val oldState = state as MaybeQuitting
state = MaybeQuittingForSure(oldState.playerInfo, oldState.completedGame)
}

is ContinuePlaying -> {
nextState = Playing(playerInfo, turn)
state = Playing(playerInfo, turn)
}

ConfirmQuitAgain -> {
val oldState = nextState as MaybeQuittingForSure
nextState = GameOver(oldState.playerInfo, oldState.completedGame)
val oldState = state as MaybeQuittingForSure
state = GameOver(oldState.playerInfo, oldState.completedGame)
}

is HandleLogGame -> {
val oldState = nextState as GameOver
nextState = when (result) {
val oldState = state as GameOver
state = when (result) {
TRY_LATER -> oldState.copy(syncState = SAVE_FAILED)
LOGGED -> oldState.copy(syncState = SAVED)
}
}

TrySaveAgain -> {
val oldState = nextState as GameOver
val oldState = state as GameOver
check(oldState.syncState == SAVE_FAILED) {
"Should only receive $TrySaveAgain in syncState $SAVE_FAILED, " +
"was ${oldState.syncState}"
}
nextState = oldState.copy(syncState = SAVING)
state = oldState.copy(syncState = SAVING)
}

PlayAgain -> {
val (x, o) = (nextState as GameOver).playerInfo
nextState = NewGame(x, o)
val (x, o) = (state as GameOver).playerInfo
state = NewGame(x, o)
}

Exit -> setOutput(FinishedPlaying)
Expand Down
Loading