Add Async image loading

This commit is contained in:
Ivan Vorobei
2019-06-06 22:24:34 +03:00
parent dbabd124fb
commit bcedea412a
914 changed files with 164 additions and 1 deletions

View 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)
}

View 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
}
}

View File

@@ -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

View 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