Swiftpack.co - backslash-f/cgkstatemachine as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by backslash-f.
backslash-f/cgkstatemachine v0.1.0
A custom GKStateMachine subclass that allows GKState changes to be observed using Combine.
⭐️ 2
🕓 2 years ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/backslash-f/cgkstatemachine.git", from: "v0.1.0")

swift-version swift-package-manager platforms ci-status license

CGKStateMachine

A custom GKStateMachine subclass that allows GKState changes to be observed using Combine.

Usage Examples

// Create a state machines of `CGKStateMachine` type.
let cgkStateMachine = CGKStateMachine(
    states: [
        FirstState(),
        SecondState()
    ]
)

// Create states of `CGKState` type.
class FirstState: CGKState {}
class SecondState: CGKState {}

// Sink `publishedState`.
var cancellables = Set<AnyCancellable>()

cgkStateMachine.publishedState
    .sink { state in
        // React here 👈🏻👈🏻👈🏻
    }
    .store(in: &cancellables)

// Enter states and get notified.
cgkStateMachine.enter(FirstState.self)
cgkStateMachine.enter(SecondState.self)

Notice

Apple implemented a publisher(for:) on NSObject.
This publisher allows for subscribing to KVO changes on classes that inherit from NSObject.

GKStateMachine inherits from NSObject.
So in theory one could try using:

gkStateMachine.publisher(for: \.currentState).sink { state in
    ...
}

... however it doesn't seem that GKStateMachine is KVO-compliant.
In my experience, with the above approach only the first state gets published.

That's why CGKStateMachine was created.

Integration

Xcode

Use Xcode's built-in support for SPM.

or...

Package.swift

In your Package.swift, add CGKStateMachine as a dependency:

dependencies: [
  .package(url: "https://github.com/backslash-f/cgkstatemachine", from: "0.1.0")
],

Associate the dependency with your target:

targets: [
  .target(name: "App", dependencies: ["CGKStateMachine"])
]

Run: swift build

GitHub

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

Release Notes

v0.1.0
2 years ago

First release is first.

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