Last active
April 30, 2021 05:06
-
-
Save patoi/5bd05929289072796929 to your computer and use it in GitHub Desktop.
Swift State machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: # 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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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