-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ProjectNavigationSelectorView.swift
310 lines (252 loc) · 10.1 KB
/
ProjectNavigationSelectorView.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import KsApi
import Library
import Prelude
import UIKit
private enum ProjectNavigationSelectorViewStyles {
fileprivate enum Layout {
fileprivate static let layoutMargins: CGFloat = Styles.grid(3)
fileprivate static let selectedButtonBorderViewHeight: CGFloat = 2.0
fileprivate static let selectedButtonBorderViewVerticalOriginModifier: CGFloat = 1.0
}
}
internal protocol ProjectNavigationSelectorViewDelegate: AnyObject {
func projectNavigationSelectorViewDidSelect(_ view: ProjectNavigationSelectorView, index: Int)
}
final class ProjectNavigationSelectorView: UIView {
// MARK: - Properties
internal weak var delegate: ProjectNavigationSelectorViewDelegate?
private var selectedButtonBorderViewLeadingConstraint: NSLayoutConstraint?
private var selectedButtonBorderViewWidthConstraint: NSLayoutConstraint?
private let viewModel: ProjectNavigationSelectorViewModelType = ProjectNavigationSelectorViewModel()
private lazy var buttonsStackView: UIStackView = {
UIStackView(frame: .zero)
|> \.translatesAutoresizingMaskIntoConstraints .~ false
}()
private lazy var selectedButtonBorderView: UIView = {
UIView(frame: .zero)
|> \.translatesAutoresizingMaskIntoConstraints .~ false
}()
private lazy var scrollView: UIScrollView = {
UIScrollView(frame: .zero)
|> \.translatesAutoresizingMaskIntoConstraints .~ false
}()
private lazy var contentView: UIView = {
UIView(frame: .zero)
|> \.translatesAutoresizingMaskIntoConstraints .~ false
}()
// MARK: - Lifecycle
override init(frame: CGRect) {
super.init(frame: frame)
self.configureSubviews()
self.setupConstraints()
self.bindViewModel()
}
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
internal func configure(with projectProperties: (Project, RefTag?)) {
self.viewModel.inputs.configureNavigationSelector(with: projectProperties)
}
// MARK: - Styles
override func bindStyles() {
super.bindStyles()
_ = self
|> \.layoutMargins .~ .init(all: ProjectNavigationSelectorViewStyles.Layout.layoutMargins)
|> \.backgroundColor .~ .ksr_white
_ = self.buttonsStackView
|> rootStackViewStyle
_ = self.selectedButtonBorderView
|> selectedButtonBorderViewStyle
_ = self.scrollView
|> \.showsHorizontalScrollIndicator .~ false
|> \.showsVerticalScrollIndicator .~ false
}
// MARK: - View Model
override func bindViewModel() {
super.bindViewModel()
self.viewModel.outputs.animateButtonBottomBorderViewConstraints
.observeForUI()
.observeValues { [weak self] index in
self?.pinSelectedButtonBorderView(toIndex: index)
}
self.viewModel.outputs.configureNavigationSelectorUI
.observeForUI()
.observeValues { [weak self] sections in
self?.setupConstraintsForSelectedButtonBorderView(sections: sections)
}
self.viewModel.outputs.notifyDelegateProjectNavigationSelectorDidSelect
.observeForUI()
.observeValues { [weak self] index in
guard let self = self else { return }
self.delegate?.projectNavigationSelectorViewDidSelect(self, index: index)
}
self.viewModel.outputs.updateNavigationSelectorUI
.observeForUI()
.observeValues { [weak self] index in
self?.selectButton(atIndex: index)
}
}
// MARK: Functions
private func configureSubviews() {
_ = (self.contentView, self)
|> ksr_addSubviewToParent()
_ = (self.scrollView, self.contentView)
|> ksr_addSubviewToParent()
_ = (self.selectedButtonBorderView, self.contentView)
|> ksr_addSubviewToParent()
_ = (self.buttonsStackView, self.scrollView)
|> ksr_addSubviewToParent()
|> ksr_constrainViewToEdgesInParent()
}
private func setupConstraints() {
NSLayoutConstraint.activate([
self.scrollView.topAnchor.constraint(equalTo: self.contentView.topAnchor),
self.scrollView.leftAnchor.constraint(equalTo: self.contentView.leftAnchor),
self.scrollView.rightAnchor.constraint(equalTo: self.contentView.rightAnchor),
self.scrollView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor),
self.scrollView.heightAnchor.constraint(equalTo: self.buttonsStackView.heightAnchor),
self.contentView.bottomAnchor.constraint(equalTo: self.bottomAnchor),
self.contentView.leftAnchor.constraint(equalTo: self.leftAnchor),
self.contentView.rightAnchor.constraint(equalTo: self.rightAnchor),
self.contentView.topAnchor.constraint(equalTo: self.topAnchor),
self.contentView.heightAnchor.constraint(equalTo: self.heightAnchor)
])
}
private func setupConstraintsForSelectedButtonBorderView(sections: [NavigationSection]) {
let buttonViews: [UIButton] = sections.map { section in
var sectionIndex = 0
switch section {
case .overview:
sectionIndex = 0
case .campaign:
sectionIndex = 1
case .faq:
sectionIndex = 2
case .risks:
sectionIndex = 3
case .environmentalCommitments:
sectionIndex = 4
}
let navigationButton = UIButton()
|> UIButton.lens.backgroundColor .~ .ksr_white
|> UIButton.lens.tag .~ sectionIndex
|> UIButton.lens.targets .~ [
(self, #selector(buttonTapped(_:)), .touchUpInside)
]
|> UIButton.lens.title(for: .normal) %~ { _ in section.displayString.uppercased() }
|> UIButton.lens.titleColor(for: .normal) %~ { _ in .ksr_support_400 }
|> UIButton.lens.titleColor(for: .selected) %~ { _ in .ksr_trust_500 }
|> UIButton.lens.titleLabel.font .~ UIFont.ksr_footnote().weighted(.semibold)
|> UIButton.lens.titleLabel.textAlignment .~ .center
return navigationButton
}
let buttonViewConstraints: [NSLayoutConstraint] = buttonViews.map { button -> [NSLayoutConstraint]? in
guard let titleLabel = button.titleLabel else { return nil }
return [
titleLabel.topAnchor.constraint(equalTo: button.topAnchor),
titleLabel.leftAnchor.constraint(equalTo: button.leftAnchor, constant: Styles.grid(2)),
titleLabel.rightAnchor.constraint(equalTo: button.rightAnchor, constant: -Styles.grid(2)),
titleLabel.bottomAnchor.constraint(equalTo: button.bottomAnchor, constant: -Styles.grid(2))
]
}
.compact()
.flatMap { $0 }
_ = self.buttonsStackView
|> UIStackView.lens.arrangedSubviews .~ buttonViews
let firstButton = self.buttonsStackView.arrangedSubviews[0]
let leadingConstraint = self.selectedButtonBorderView.leadingAnchor
.constraint(
equalTo: firstButton.leadingAnchor
)
let widthConstraint = self.selectedButtonBorderView.widthAnchor
.constraint(
equalTo: firstButton.widthAnchor
)
NSLayoutConstraint.activate([
leadingConstraint,
widthConstraint,
self.selectedButtonBorderView.heightAnchor
.constraint(equalToConstant: ProjectNavigationSelectorViewStyles.Layout
.selectedButtonBorderViewHeight),
self.selectedButtonBorderView.bottomAnchor
.constraint(
equalTo: self.scrollView.bottomAnchor,
constant: ProjectNavigationSelectorViewStyles.Layout.selectedButtonBorderViewVerticalOriginModifier
)
])
NSLayoutConstraint.activate(buttonViewConstraints)
self.selectedButtonBorderViewLeadingConstraint = leadingConstraint
self.selectedButtonBorderViewWidthConstraint = widthConstraint
}
private func pinSelectedButtonBorderView(toIndex index: Int) {
guard let navigationSection = NavigationSection(rawValue: index),
let button = self.buttonsStackView.arrangedSubviews.first(where: { $0.tag == index }),
let buttonSection = NavigationSection(rawValue: button.tag),
buttonSection == navigationSection else { return }
let leadingConstant = button.frame.origin.x - safeAreaInsets.left
// The value of the constraint is originally set to the width of the first button so we have subtract this each time we want to calculate the constant
let widthConstant = button.frame.width - self.buttonsStackView.arrangedSubviews[0].frame
.width
UIView.animate(
withDuration: 0.3,
delay: 0.0,
options: .curveEaseOut,
animations: {
self.contentView.setNeedsLayout()
self.selectedButtonBorderViewLeadingConstraint?.constant = leadingConstant
self.selectedButtonBorderViewWidthConstraint?.constant = widthConstant
self.contentView.layoutIfNeeded()
}
)
let origin = CGPoint(
x: button.frame
.minX,
y: 0.0
)
let size = CGSize(
width: button.frame.size
.width,
height: 1.0
)
let scrollToRect = CGRect(
origin: origin,
size: size
)
self.scrollView.scrollRectToVisible(scrollToRect, animated: true)
}
private func selectButton(atIndex index: Int) {
let indexSection = NavigationSection(rawValue: index)
for button in self.buttonsStackView.arrangedSubviews {
let navigationSection = NavigationSection(rawValue: button.tag)
let validNavigationSection = navigationSection != nil
let isButtonSelected = validNavigationSection ? navigationSection == indexSection : false
let buttonSelectedFont = isButtonSelected ? UIFont.ksr_footnote().weighted(.bold) : UIFont
.ksr_footnote().weighted(.semibold)
_ = (button as? UIButton)
?|> UIButton.lens.isSelected .~ isButtonSelected
?|> UIButton.lens.titleLabel.font .~ buttonSelectedFont
}
}
// MARK: - Actions
@objc
private func buttonTapped(_ button: UIButton) {
self.viewModel.inputs.buttonTapped(index: button.tag)
}
}
// MARK: - Styles
private let selectedButtonBorderViewStyle: ViewStyle = { view in
view
|> \.backgroundColor .~ .ksr_trust_500
}
private let rootStackViewStyle: StackViewStyle = { stackView in
stackView
|> \.axis .~ NSLayoutConstraint.Axis.horizontal
|> \.isLayoutMarginsRelativeArrangement .~ true
|> \.layoutMargins .~ UIEdgeInsets.init(
top: Styles.grid(0),
left: Styles.grid(0),
bottom: Styles.grid(1),
right: Styles.grid(0)
)
|> \.spacing .~ Styles.grid(2)
}