Swiftpack.co - YuLeiFuYun/YLStateMachine as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by YuLeiFuYun.
YuLeiFuYun/YLStateMachine 1.1.2
A simple state machine.
⭐️ 6
🕓 3 years ago
.package(url: "https://github.com/YuLeiFuYun/YLStateMachine.git", from: "1.1.2")

YLStateMachine

Requirements

  • iOS 13.0+
  • Swift 5.1+

Installation

Cocoapods

To integrate YLStateMachine into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '13.0'
use_frameworks!

target 'MyApp' do
  # your other pod
  # ...
  pod 'YLStateMachine'
end

Run pod install to build your dependencies.

Swift Package Manager

Select File > Swift Packages > Add Package Dependency. Enter https://github.com/YuLeiFuYun/YLStateMachine.git in the "Choose Package Repository" dialog.

Usage

Create a “state" adopts and conforms to the StateType protocol:

enum RefreshState: StateType {
    case initial
    case refreshing
    case paginated
	...
    
    var stability: Stability {
        switch self {
        case .refreshing:
            return .transitional
        default:
            return .stable
        }
    }
    
    static var initialState: RefreshState {
        return .initial
    }
}

Create a “action” adopts and conforms to the ActionType protocol:

enum RefreshAction: ActionType {
    case pullToRefresh
    case loadingMore
    
    var transitionState: RefreshState {
        switch self {
        ...
        }
    }
}

Create a “operator” adopts and conforms to the OperatorType protocol:

class RefreshOperator: OperatorType {
    typealias Action = RefreshAction
    ...
    
    // Optional
    func startTransition(_ state: Action.State) {
        ...
    }
    
    // Required
    func transition(with action: RefreshAction, completion: @escaping (RefreshState) -> Void) {
        ...
    }
    
    // Optional
    func endTransition(_ state: Action.State) {
        ...
    }
}

Create a state machine:

let refreshOperator = RefreshOperator()
let refreshStateMachine = StateMachine(operator: refreshOperator)

Finally, using this state machine.

refreshStateMachine.trigger(someAction) {
    ...
}

Check out YLRefreshKit to learn how to use it in practice.

License

YLStateMachine is released under the MIT license. See LICENSE for details.

GitHub

link
Stars: 6
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

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