-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContentView.swift
136 lines (107 loc) · 3.35 KB
/
ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// ContentView.swift
// prototype
//
// Created by cjc on 11/1/20.
//
import UIKit
import SwiftUI
import AVFoundation
import ReSwift
import Combine
import Foundation
/* ----------------------------------------------------------------
ReSwift + SwiftUI: taken from ReSwiftUI-StoreSubscriber
https://github.com/ReSwift/ReSwift/issues/455#issuecomment-714791966
---------------------------------------------------------------- */
protocol HasName {
var name: String { get }
}
struct ChangeName: Action {
let to: String
}
func nameReducer(action: Action, state: String?) -> String {
switch action {
case let action as ChangeName:
return action.to
default:
return state ?? ""
}
}
struct StoreSubscriber<State, Content: View>: View {
let content: (State, @escaping DispatchFunction) -> Content
@EnvironmentObject var store: AnyObservableStore
init(@ViewBuilder content: @escaping (State, @escaping DispatchFunction) -> Content) {
self.content = content
}
var body: some View {
content(store.state() as! State, store.dispatch)
}
}
struct ContentView: View {
var body: some View {
StoreSubscriber { (state: HasName, dispatch: @escaping DispatchFunction) in
VStack {
Text(state.name)
TextField("Name", text: Binding(
get: { state.name },
set: { dispatch(ChangeName(to: $0)) }
))
}
}
}
}
struct ContentView_Previews : PreviewProvider {
private struct MockState: StateType, HasName {
var name: String = "mock"
}
private static func mockReducer(action: Action, state: MockState?) -> MockState {
return MockState(
name: nameReducer(action: action, state: state?.name)
)
}
// injects store into environment
static var previews: some View {
ContentView()
.environmentObject(
AnyObservableStore(store: ObservableStore(reducer: mockReducer, state: MockState()))
)
}
}
/* ----------------------------------------------------------------
From ReSwiftUI-StoreSubscriber's SceneDelegate
https://github.com/ReSwift/ReSwift/issues/455#issuecomment-714791966
---------------------------------------------------------------- */
protocol ObservableStoreType: DispatchingStoreType, ObservableObject {
associatedtype State: StateType
/// The current state stored in the store.
var state: State! { get }
}
class ObservableStore<State: StateType>: Store<State>, ObservableStoreType {
override func _defaultDispatch(action: Action) {
objectWillChange.send()
super._defaultDispatch(action: action)
}
}
class AnyObservableStore: ObservableObject {
let state: () -> Any?
let dispatch: (Action) -> Void
var disposeBag: [AnyCancellable] = []
init<State>(store: ObservableStore<State>) {
state = { store.state as Any? }
dispatch = store.dispatch
disposeBag.append(
store.objectWillChange.sink { [unowned self] in
self.objectWillChange.send()
}
)
}
}
struct AppState: StateType, Equatable, HasName {
var name: String
}
func appReducer(action: Action, state: AppState?) -> AppState {
return AppState(
name: nameReducer(action: action, state: state?.name)
)
}