/// Model
struct Cinnamon { let value: Int = 0 }
enum Event { case increase }
45
Slide 48
Slide 48 text
/// Model
struct Cinnamon { let value: Int = 0 }
enum Event { case increase }
func reducer(state: Cinnamon, event: Event) -> Cinnamon {
var newState = state
if event == .increase {
newState.value += 1
}
return newState
}
/// UI
let builder = Form(state: Cinnamon(), reducer: reducer)
46
Slide 49
Slide 49 text
47
Slide 50
Slide 50 text
48
Slide 51
Slide 51 text
49
Slide 52
Slide 52 text
struct AddressBook {
var contacts: [Person]
var searchTerm: String
var scrollPosition: Int
}
50
Slide 53
Slide 53 text
struct AddressBookApp {
struct Data {
var contacts: [Person]
}
var data: Data
struct UI {
var searchTerm: String
var scrollPosition: Int
}
var ui: UI
}
51
Slide 54
Slide 54 text
52
Slide 55
Slide 55 text
53
Slide 56
Slide 56 text
54
Slide 57
Slide 57 text
55
Slide 58
Slide 58 text
56
Slide 59
Slide 59 text
57
Slide 60
Slide 60 text
58
Slide 61
Slide 61 text
59
Slide 62
Slide 62 text
60
Slide 63
Slide 63 text
61
Slide 64
Slide 64 text
62
Slide 65
Slide 65 text
63
Slide 66
Slide 66 text
64
Slide 67
Slide 67 text
65
Slide 68
Slide 68 text
private var subscribers: [String: (State)->Void] = [:]
public func subscribe(_ subscriber: @escaping (State)->Void)
-> String {
let token = UUID().uuidString
subscribers[token] = subscriber
subscriber(state)
return token
}
66
Slide 69
Slide 69 text
67
Slide 70
Slide 70 text
protocol FormComponent {
associatedtype State
func setup(with state: State)
func update(state: State)
-> Void
}
public func subscribe
(_ subscriber: Component) -> String
where Component.State == State
{...
68