-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathUIKitExampleViewController.swift
More file actions
116 lines (95 loc) · 3.22 KB
/
Copy pathUIKitExampleViewController.swift
File metadata and controls
116 lines (95 loc) · 3.22 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
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
import UIKit
import Brownie
class UIKitExampleViewController: UIViewController {
private var store: Store<BrownfieldStore>?
private var cancelSubscription: (() -> Void)?
private let counterLabel: UILabel = {
let label = UILabel()
label.font = .systemFont(ofSize: 24, weight: .bold)
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
private let userLabel: UILabel = {
let label = UILabel()
label.font = .systemFont(ofSize: 18)
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
private let nameTextField: UITextField = {
let field = UITextField()
field.borderStyle = .roundedRect
field.placeholder = "Enter name"
field.translatesAutoresizingMaskIntoConstraints = false
return field
}()
private let incrementButton: UIButton = {
var config = UIButton.Configuration.borderedProminent()
config.title = "Increment"
let button = UIButton(configuration: config)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
setupStore()
}
private func setupUI() {
title = "UIKit Example"
view.backgroundColor = .systemBackground
let titleLabel = UILabel()
titleLabel.text = "UIKit + Brownie Store"
titleLabel.font = .systemFont(ofSize: 20, weight: .bold)
titleLabel.textAlignment = .center
titleLabel.translatesAutoresizingMaskIntoConstraints = false
let stack = UIStackView(arrangedSubviews: [
titleLabel,
counterLabel,
userLabel,
nameTextField,
incrementButton
])
stack.axis = .vertical
stack.spacing = 16
stack.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stack)
NSLayoutConstraint.activate([
stack.centerXAnchor.constraint(equalTo: view.centerXAnchor),
stack.centerYAnchor.constraint(equalTo: view.centerYAnchor),
stack.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
stack.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20)
])
incrementButton.addTarget(self, action: #selector(incrementTapped), for: .touchUpInside)
nameTextField.addTarget(self, action: #selector(nameChanged), for: .editingChanged)
}
private func setupStore() {
store = StoreManager.get(key: BrownfieldStore.storeName, as: BrownfieldStore.self)
guard let store else {
counterLabel.text = "Store not found"
return
}
updateUI(with: store.state)
cancelSubscription = store.subscribe { [weak self] state in
self?.updateUI(with: state)
}
}
private func updateUI(with state: BrownfieldStore) {
counterLabel.text = "Count: \(Int(state.counter))"
userLabel.text = "User: \(state.user.name)"
if nameTextField.text != state.user.name && !nameTextField.isFirstResponder {
nameTextField.text = state.user.name
}
}
@objc private func incrementTapped() {
store?.set { $0.counter += 1 }
}
@objc private func nameChanged() {
guard let name = nameTextField.text else { return }
store?.set { $0.user.name = name }
}
deinit {
cancelSubscription?()
}
}