-
Notifications
You must be signed in to change notification settings - Fork 2
/
ViewModel.swift
67 lines (51 loc) · 1.46 KB
/
ViewModel.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
import Republished
import SwiftUI
@MainActor
final class ViewModel: ObservableObject {
// MARK: Lifecycle
init(model: DomainModel) {
_model = .init(wrappedValue: model)
}
// MARK: Internal
var info: String {
[
model.isEven ? "even" : nil,
model.isZero ? "zero" : nil,
model.isNegative ? "negative" : nil,
model.isPositive ? "positive" : nil,
model.isMax ? "MAXINT" : nil,
model.isMin ? "MININT" : nil,
model.isPrime ? "prime" : nil,
]
.compactMap { $0 }
.sorted()
.joined(separator: ", ")
}
var countString: String {
"\(model.count)"
}
func increment() {
model.set(count: model.count + 1)
}
func decrement() {
model.set(count: model.count - 1)
}
func rand() {
model.set(count: Int.random(in: Int.min ... Int.max))
}
func zero() {
model.set(count: 0)
}
// MARK: Private
// Here the @Republished property wrapper is used *instead* of
// an @Published property wrapper and hold the nested ObservableObject.
// (Note that there are no @Published wrappers in this file.)
// @Republished listens to the inner ObservableObject's
// change notifications and propagates them to the outer one.
// SwiftUI views can use properties here derived from the inner object
// just as they would use an @Published field.
//
// This outer object could also provide @Binding surfaces into
// the inner object's data.
@Republished private var model: DomainModel
}