Swiftpack.co -  alexruperez/StateMachine as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
alexruperez/StateMachine
State machine creation framework written in Swift inspired by GKStateMachine from Apple GameplayKit
.package(url: "https://github.com/alexruperez/StateMachine.git", from: "0.1.2")

StateMachine

StateMachine

Twitter Version License Platform Swift Carthage compatible Swift Package Manager Compatible Build Status codecov codebeat badge BCH compliance

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

Test StateMachine working live!

๐ŸŒŸ Features

  • โ˜‘ Define States and Their Behavior.
  • โ˜‘ Create and Drive a State Machine.
  • โ˜‘ Subscribe/unsubscribe to State changes.
  • โ˜‘ UIApplication and UIApplicationDelegate extensions with application life cycle State Machine embedded.
  • โ˜‘ 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, contact@alexruperez.com

๐Ÿ‘ฎโ€โ™‚๏ธ License

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

GitHub

link
Stars: 41
Last commit: 3 years ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jackโ€™d and SCRUFF. We are two of the worldโ€™s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

StateMachine 0.1.2
3 years ago
  • Subscription method update

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API