mirror of
https://github.com/ivanvorobei/SwiftUI.git
synced 2026-03-27 03:41:21 +01:00
Add Async image loading
This commit is contained in:
44
Examples/Time Travel/SwiftUITimeTravel/TimeTravelView/Store.swift
Executable file
44
Examples/Time Travel/SwiftUITimeTravel/TimeTravelView/Store.swift
Executable file
@@ -0,0 +1,44 @@
|
||||
import SwiftUI
|
||||
import Combine
|
||||
|
||||
public final class Store<StateType>: BindableObject where StateType: StateMachine {
|
||||
|
||||
private let initialState: StateType
|
||||
private var subsequentStates: [StateType] = []
|
||||
|
||||
public let didChange = PassthroughSubject<Void, Never>()
|
||||
|
||||
public init(state: StateType) {
|
||||
initialState = state
|
||||
}
|
||||
|
||||
var allStates: [StateType] {
|
||||
[[initialState], subsequentStates].flatMap({ $0 })
|
||||
}
|
||||
|
||||
var stateCount: Int {
|
||||
1 + subsequentStates.count
|
||||
}
|
||||
|
||||
var currentStateIndex: Int = 0 {
|
||||
didSet {
|
||||
withAnimation {
|
||||
didChange.send(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The current state of the store. This will update as time traveling occurs.
|
||||
public var state: StateType {
|
||||
allStates[currentStateIndex]
|
||||
}
|
||||
|
||||
/// Dispatches an event to be applied to the current state.
|
||||
public func dispatch(event: StateType.Event) {
|
||||
var newState = state
|
||||
newState.update(with: event)
|
||||
subsequentStates.append(newState)
|
||||
currentStateIndex = stateCount - 1
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user