Giter Club home page Giter Club logo

statemachine's Introduction

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

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

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

statemachine's People

Contributors

alexruperez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.