Latest 0.1.0
Homepage https://github.com/aldo-dev/RouterStateMachine
License MIT
Platforms ios 11.0
Dependencies SwiftyCollection
Authors

CircleCI
Version
License
Platform

Description

The framework provides 2 different state machines SimpleStateMachine and SyncronizedStateMachine. The second provides logic for blocking state changing until it’s finilized

Usage

  • Create road map provider. The class that will tell what transitions are allowed
    
    final class MockStateProvider: StateProvider {
    typealias T = MockState

private(set) var fromStates: [MockState] = []
private(set) var toStates: [MockState] = []

func canMove(fromState state: MockState, toState to: MockState) -> Bool {
fromStates.append(state)
toStates.append(to)
switch (state, to) {
case (.one, .two),
(.two, .three),
(.two, .one),
(.three,. two): return true
default: return false
}
}

}


- Create Delegate proxy to listen when a change / error happens. Here in the example the delegate proxy is wrapped into separate class. However it cab be anyclass that responsible to do action based on state change. For example Router that is responsible for Navigation
```swift
final class DelegateProxyTester {
var proxy: StateMachineDelegateProxy<MockState> {
let proxy = StateMachineDelegateProxy<MockState>()
proxy.willMoveCallback = self.willMove
proxy.errorMoving = self.errorMoving
return proxy
}

func willMove(from state: MockState, toState to: MockState) {
//do the logic based on state change
}

func errorMoving(from state: MockState, toState to: MockState) {
//process the error
}
}
  • Connect the parts
    mockProvider = MockStateProvider()
    proxyTester = DelegateProxyTester()
    machineToTest = SimpleStateMachine(initialState: .one, roadMapProvider: mockProvider)
    machineToTest.delegateProxy = proxyTester.proxy

Syncronized Machine

It behaves more like a decorator on top of SimpleStateMachine that handles logic for preventing state change until it’s allowed. Consider it’s like using semaphore inside.
To use it follow the step above with the difference that you need to provide different proxy

final class SyncDelegateProxyTester {

var proxy: SyncronizedMachineDelegateProxy<MockState> {
let proxy = SyncronizedMachineDelegateProxy<MockState>()
proxy.syncWillMove = willMove
proxy.errorMoving = errorMoving
return proxy
}

func willMove(from state: MockState, toState to: MockState, completion: @escaping () -> Void) {
//do the logic and when it's finished call completion
}

func errorMoving(from state: MockState, toState to: MockState) {
// process the error if need
}
}

Installation

RouterStateMachine is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'RouterStateMachine'

Author

ALDO Inc., [email protected]

License

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

Latest podspec

{
    "name": "RouterStateMachine",
    "version": "0.1.0",
    "summary": "A short description of RouterStateMachine.",
    "description": "The class that represents StateMachine patternt for Routers objects that are used in the concept ofnUseCases/Viper/Clean Architechture",
    "homepage": "https://github.com/aldo-dev/RouterStateMachine",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "ALDO Inc.": "[email protected]"
    },
    "source": {
        "git": "https://github.com/aldo-dev/RouterStateMachine.git",
        "tag": "0.1.0"
    },
    "platforms": {
        "ios": "11.0"
    },
    "swift_version": "4.2",
    "source_files": "RouterStateMachine/Classes/**/*",
    "dependencies": {
        "SwiftyCollection": []
    }
}

Pin It on Pinterest

Share This