-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObservableTestView.swift
More file actions
76 lines (63 loc) · 1.56 KB
/
ObservableTestView.swift
File metadata and controls
76 lines (63 loc) · 1.56 KB
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
//
// ObservableTestView.swift
// AsyncReactorExample
//
// Created by Dominik Arnhof on 21.11.24.
//
import SwiftUI
import Reactor
@available(iOS 17.0, *)
@Observable
class ObservableTestReactor: Reactor {
enum SyncAction {
case count
case enterText(String)
}
@Observable
class State {
var count = 0
var text = "test"
}
private(set) var state = State()
init(state: State = State()) {
self.state = state
print("reactor init")
}
func action(_ action: SyncAction) {
switch action {
case .count:
state.count += 1
case .enterText(let text):
state.text = text
}
}
}
@available(iOS 17.0, *)
struct ObservableTestView: View {
@Environment(ObservableTestReactor.self)
private var reactor
@ActionBinding(ObservableTestReactor.self, keyPath: \.text, action: ObservableTestReactor.SyncAction.enterText)
private var text: String
var body: some View {
let _ = Self._printChanges()
VStack {
Text(reactor.count.formatted())
Button {
reactor.action(.count)
} label: {
Text("+")
}
Text(reactor.text)
TextField("Text", text: $text)
.textFieldStyle(.roundedBorder)
.padding()
}
}
}
#Preview {
if #available(iOS 17.0, *) {
ReactorView(ObservableTestReactor()) {
ObservableTestView()
}
}
}