Latest 0.1.2
Homepage https://github.com/alexruperez/StateMachine
License MIT
Platforms ios 8.0, tvos 9.0, osx 10.9, watchos 2.0
Frameworks Foundation
Authors

StateMachine

StateMachine

Twitter
Version
License
Platform
Swift
Carthage compatible
Swift Package Manager Compatible
Build Status
codecov

Swift library to create Finite-state machines inspired by GKStateMachine from Apple GameplayKit framework.

Test StateMachine working live!

🌟 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. To install
it, simply add the following line to your Podfile:

pod 'ArchitStateMachine'

Or you can install it with Carthage:

github "alexruperez/StateMachine"

Or install it with Swift Package Manager:

dependencies: [
    .package(url: "https://github.com/alexruperez/StateMachine.git")
]

🐒 Usage

Define States and Their Behavior:

class MyState: State {
    func isValidNext<S>(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<S>(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<S>(state type: S.Type) -> Bool where S : State {
        return false
    }

    func willExit(to next: State) {
        // Your code here
    }
}

Create and Drive a State Machine:

let stateMachine = StateMachine([MyState(), OneValidNextState(), OtherValidNextState()])

stateMachine.enter(OneValidNextState.self)

stateMachine.enter(OtherValidNextState.self)

Subscribe/unsubscribe to State changes:

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:

UIApplication

if UIApplication.shared.stateMachine.current is ActiveApplicationState {
    // Your code here
}

StatefulViewController subclass with UIViewController life cycle State Machine embedded:

StatefulViewController

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, [email protected]

👮‍♂️ License

StateMachine is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "ArchitStateMachine",
    "version": "0.1.2",
    "summary": "State machine creation framework written in Swift inspired by GKStateMachine from Apple GameplayKit",
    "homepage": "https://github.com/alexruperez/StateMachine",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Alex Rupu00e9rez": "[email protected]"
    },
    "source": {
        "git": "https://github.com/alexruperez/StateMachine.git",
        "tag": "0.1.2"
    },
    "social_media_url": "https://twitter.com/alexruperez",
    "module_name": "StateMachine",
    "platforms": {
        "ios": "8.0",
        "tvos": "9.0",
        "osx": "10.9",
        "watchos": "2.0"
    },
    "source_files": "Core/*.{h,swift}",
    "ios": {
        "source_files": "UIKit/*.{h,swift}",
        "frameworks": "UIKit"
    },
    "tvos": {
        "source_files": "UIKit/*.{h,swift}",
        "frameworks": "UIKit"
    },
    "frameworks": "Foundation",
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This