-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
ExampleCounter.swift
56 lines (48 loc) · 1.09 KB
/
ExampleCounter.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
import Atoms
import SwiftUI
struct CounterAtom: StateAtom, Hashable {
func defaultValue(context: Context) -> Int {
0
}
}
struct CounterScreen: View {
@Watch(CounterAtom())
var count
var body: some View {
VStack {
Text("Count: \(count)").font(.largeTitle)
CountStepper()
}
.fixedSize()
.navigationTitle("Counter")
}
}
struct CountStepper: View {
@WatchState(CounterAtom())
var count
var body: some View {
#if os(tvOS) || os(watchOS)
HStack {
Button("-") { count -= 1 }
Button("+") { count += 1 }
}
#else
Stepper(value: $count) {}
.labelsHidden()
#endif
}
}
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
public struct ExampleCounter: View {
public init() {}
public var body: some View {
CounterScreen()
}
}
struct CounterScreen_Preview: PreviewProvider {
static var previews: some View {
AtomRoot {
CounterScreen()
}
}
}