Skip to content

YuLeiFuYun/YLStateMachine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.