Skip to content

Instantly share code, notes, and snippets.

@patoi
Last active April 30, 2021 05:06
Show Gist options
  • Save patoi/5bd05929289072796929 to your computer and use it in GitHub Desktop.
Save patoi/5bd05929289072796929 to your computer and use it in GitHub Desktop.
Swift State machine
//: # State machine: handling three state and transition
import GameplayKit
class RetrievingDataState: GKState {
override func isValidNextState(stateClass: AnyClass) -> Bool {
print("\n---> \(stateClass) class")
return (stateClass == DataAvailableState.self) || (stateClass == DataNotAvailableState.self)
}
override func didEnterWithPreviousState(previousState: GKState?) {
// Perform here the actions that you want to do with your UI when it enters this state like for example, showing and starting an activity indicator
print("searching data start")
}
override func willExitWithNextState(nextState: GKState) {
// Perform here the actions that you want to do with your UI when it exits this state like for example, stopping the activity indicator.
print("End of search")
}
}
class DataAvailableState: GKState {
// Can't translate from DataNotAvailableState
override func isValidNextState(stateClass: AnyClass) -> Bool {
print("\n---> \(stateClass) class")
return stateClass == RetrievingDataState.self
}
override func didEnterWithPreviousState(previousState: GKState?) {
// Perform here the actions that you want to do with your UI when it enters this state.
print("data available: show it")
}
override func willExitWithNextState(nextState: GKState) {
// Perform here the actions that you want to do with your UI when it exits this state.
print("data available end")
}
}
class DataNotAvailableState: GKState {
override func isValidNextState(stateClass: AnyClass) -> Bool {
print("\n---> \(stateClass) class")
return stateClass == RetrievingDataState.self
}
override func didEnterWithPreviousState(previousState: GKState?) {
// Perform here the actions that you want to do with your UI when it enters this state.
print("data not available state started: show not found message")
}
override func willExitWithNextState(nextState: GKState) {
// Perform here the actions that you want to do with your UI when it exits this state.
print("data not available state end")
}
}
// create states
let retrievingDataState = RetrievingDataState()
let dataAvailableState = DataAvailableState()
let dataNotAvailableState = DataNotAvailableState()
// state machine
let stateMachine = GKStateMachine(states: [retrievingDataState, dataAvailableState, dataNotAvailableState])
stateMachine.enterState(RetrievingDataState)
stateMachine.enterState(DataAvailableState)
// valid state transition
stateMachine.enterState(RetrievingDataState)
stateMachine.enterState(DataNotAvailableState)
@patoi
Copy link
Author

patoi commented Oct 10, 2015

Output:

searching data start

---> DataAvailableState class
End of search
data available: show it

---> RetrievingDataState class
data available end
searching data start

---> DataNotAvailableState class
End of search
data not available state started: show not found message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment