mirror of
https://github.com/ivanvorobei/SwiftUI.git
synced 2026-03-21 08:59:15 +01:00
Add Async image loading
This commit is contained in:
11
Examples/Time Travel/SwiftUITimeTravel/TimeTravelView/StateMachine.swift
Executable file
11
Examples/Time Travel/SwiftUITimeTravel/TimeTravelView/StateMachine.swift
Executable file
@@ -0,0 +1,11 @@
|
||||
/// Conforming types serve as the state of a time travelable application
|
||||
public protocol StateMachine {
|
||||
|
||||
/// Events define things that can happen within your application that change its state.
|
||||
///
|
||||
/// This might include things like text editing, button taps, or network responses.
|
||||
associatedtype Event
|
||||
|
||||
/// Applies an event to the current state.
|
||||
mutating func update(with event: Event)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import SwiftUI
|
||||
|
||||
struct TimeTravelBarView : View {
|
||||
|
||||
@EnvironmentObject var store: Store<TodoState>
|
||||
|
||||
var body: some View {
|
||||
let indexBinding = Binding<Double>(
|
||||
getValue: { Double(self.store.currentStateIndex) },
|
||||
setValue: { self.store.currentStateIndex = Int($0) })
|
||||
|
||||
return Slider(value: indexBinding, from: 0, through: Double(store.stateCount-1))
|
||||
.background(Color.white)
|
||||
.frame(height: 44.0, alignment: .bottom)
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
struct TimeTravelBarView_Previews : PreviewProvider {
|
||||
static var previews: some View {
|
||||
TimeTravelBarView()
|
||||
}
|
||||
}
|
||||
#endif
|
||||
39
Examples/Time Travel/SwiftUITimeTravel/TimeTravelView/TimeTravelView.swift
Executable file
39
Examples/Time Travel/SwiftUITimeTravel/TimeTravelView/TimeTravelView.swift
Executable file
@@ -0,0 +1,39 @@
|
||||
import SwiftUI
|
||||
|
||||
public struct TimeTravelView<StateType, Content>: View where StateType: StateMachine, Content: View {
|
||||
|
||||
let initialState: StateType
|
||||
|
||||
private let content: Content
|
||||
|
||||
@State var store: Store<StateType>? = nil
|
||||
|
||||
public init(initialState: StateType, content: () -> Content) {
|
||||
self.initialState = initialState
|
||||
self.content = content()
|
||||
}
|
||||
|
||||
public var body: some View {
|
||||
|
||||
let store = self.store ?? Store(state: initialState)
|
||||
if (self.store == nil) {
|
||||
self.store = store
|
||||
}
|
||||
|
||||
return VStack {
|
||||
content
|
||||
TimeTravelBarView()
|
||||
}
|
||||
.environmentObject(store)
|
||||
}
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
struct TimeTravelView_Previews : PreviewProvider {
|
||||
static var previews: some View {
|
||||
TimeTravelView(initialState: TodoState()) {
|
||||
TodoListView()
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user