A simple State Machine to organize your ViewController
This demonstrate the cycle of a basic ViewController
I want to show different view to notify user the current state of the App
- refreshing : show ActityIndocator
- error : show error image and a
retry
button - list: show the data that retrived from the network
And this works perfect with FSM (Finite-state machine)
There 2 main class, ViewStateMachine and StateMachine
StateMachine
is a generic FSMViewStateMachine
is build onStateMachine
for switching views when enter different state
Check demo code for example
Check ViewStateMachineDemo.swift for example
DefaultViewStateMachineDelegate
demonstrate switch view when state changed
let uiStateDelegate = DefaultViewStateMachineDelegate()
lazy var uiState:ViewStateMachine<DefaultViewStateMachineDelegate> = {
let m = ViewStateMachine(superView: self.view, delegate: self.uiStateDelegate)
return m
}()
let failedView = StateView.loadFromXib()
failedView.label.text = "failed"
self.uiState.enter(state:ViewState(failed:failedView))
Check StateMachineDemo.swift for example
enum DemoState {
case error
case success
}
class StateMachineDemo: UIViewController {
lazy var stateMachine:StateMachine<StateMachineDemo> = {
let sm = StateMachine(delegate: self)
return sm
}()
}
extension StateMachineDemo: StateMachineDelegate {
typealias State = DemoState
func canEnter(state: DemoState) -> Bool {
return true
}
func transit(from: DemoState?, to: DemoState) {
switch to {
case .error:
print("error")
case .success:
print("success")
}
}
}
self.stateMachine.enter(state: .success)
This project is for studying, you'd prefer using libs below :
- weibo : @王大屁帅2333
- email : iShawnwang2333@gmail.com
It seems that my README is a bit longer than the code i write :D
Distributed under the GPL v3 license. See LICENSE for more information.