https://github.com/alexruperez/statemachine
State machine creation framework written in Swift inspired by GKStateMachine from Apple GameplayKit
https://github.com/alexruperez/statemachine
finite finite-state-automata finite-state-machine framework gameplay gameplaykit gkstatemachine machine state state-machine swift
Last synced: 4 months ago
JSON representation
State machine creation framework written in Swift inspired by GKStateMachine from Apple GameplayKit
- Host: GitHub
- URL: https://github.com/alexruperez/statemachine
- Owner: alexruperez
- License: mit
- Created: 2018-01-21T20:32:28.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-28T12:42:05.000Z (about 8 years ago)
- Last Synced: 2025-01-23T10:03:30.948Z (about 1 year ago)
- Topics: finite, finite-state-automata, finite-state-machine, framework, gameplay, gameplaykit, gkstatemachine, machine, state, state-machine, swift
- Language: Swift
- Homepage:
- Size: 1.57 MB
- Stars: 48
- Watchers: 5
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README

# StateMachine
[](http://twitter.com/alexruperez)
[](http://cocoapods.org/pods/ArchitStateMachine)
[](http://cocoapods.org/pods/ArchitStateMachine)
[](http://cocoapods.org/pods/ArchitStateMachine)
[](https://swift.org)
[](https://github.com/Carthage/Carthage)
[](https://github.com/apple/swift-package-manager)
[](https://travis-ci.org/alexruperez/StateMachine)
[](https://codecov.io/gh/alexruperez/StateMachine)
[](https://codebeat.co/projects/github-com-alexruperez-statemachine-master)
[](https://bettercodehub.com/)
Swift library to create [**Finite-state machines**](https://en.wikipedia.org/wiki/Finite-state_machine) inspired by [GKStateMachine](https://developer.apple.com/documentation/gameplaykit/gkstatemachine) from Apple [GameplayKit](https://developer.apple.com/library/content/documentation/General/Conceptual/GameplayKit_Guide/StateMachine.html) framework.
[**Test StateMachine working live!**](https://appetize.io/app/1rzk2knb6927f4m08vpjtg4mmr?device=iphone8&scale=75&orientation=landscape&osVersion=11.1&deviceColor=black)
## 🌟 Features
- [x] Define States and Their Behavior.
- [x] Create and Drive a State Machine.
- [x] Subscribe/unsubscribe to State changes.
- [x] UIApplication and UIApplicationDelegate extensions with application life cycle State Machine embedded.
- [x] StatefulViewController subclass with UIViewController life cycle State Machine embedded.
## 📲 Installation
StateMachine is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod 'ArchitStateMachine'
```
#### Or you can install it with [Carthage](https://github.com/Carthage/Carthage):
```ogdl
github "alexruperez/StateMachine"
```
#### Or install it with [Swift Package Manager](https://swift.org/package-manager/):
```swift
dependencies: [
.package(url: "https://github.com/alexruperez/StateMachine.git")
]
```
## 🐒 Usage
#### Define States and Their Behavior:
```swift
class MyState: State {
func isValidNext(state type: S.Type) -> Bool where S : State {
switch type {
case is OneValidNextState.Type, is OtherValidNextState.Type:
return true
default:
return false
}
}
}
class OneValidNextState: State {
func isValidNext(state type: S.Type) -> Bool where S : State {
return type is OtherValidNextState.Type
}
func didEnter(from previous: State?) {
// Your code here
}
}
class OtherValidNextState: State {
func isValidNext(state type: S.Type) -> Bool where S : State {
return false
}
func willExit(to next: State) {
// Your code here
}
}
```
#### Create and Drive a State Machine:
```swift
let stateMachine = StateMachine([MyState(), OneValidNextState(), OtherValidNextState()])
stateMachine.enter(OneValidNextState.self)
stateMachine.enter(OtherValidNextState.self)
```
#### Subscribe/unsubscribe to State changes:
```swift
let subscriptionToken = stateMachine.subscribe { (previous, current) in
// Your code here
}
stateMachine.unsubscribe(subscriptionToken)
stateMachine.unsubscribeAll()
```
#### UIApplication and UIApplicationDelegate extensions with application life cycle State Machine embedded:

```swift
if UIApplication.shared.stateMachine.current is ActiveApplicationState {
// Your code here
}
```
#### StatefulViewController subclass with UIViewController life cycle State Machine embedded:

```swift
let viewController = StatefulViewController()
if viewController.stateMachine.current is AppearingViewControllerState {
// Your code here
}
```
## ❤️ Etc.
* Contributions are very welcome.
* Attribution is appreciated (let's spread the word!), but not mandatory.
## 👨💻 Authors
[alexruperez](https://github.com/alexruperez), contact@alexruperez.com
## 👮♂️ License
StateMachine is available under the MIT license. See the LICENSE file for more info.