/
PetSelectorBulletinPage.swift
359 lines (240 loc) · 11.1 KB
/
PetSelectorBulletinPage.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**
* BulletinBoard
* Copyright (c) 2017 Alexis Aubry. Licensed under the MIT license.
*/
import UIKit
import BulletinBoard
/**
* An item that displays a choice with two buttons.
*
* This item demonstrates how to create a bulletin item with a custom interface, and changing the
* next item based on user interaction.
*/
class PetSelectorBulletinPage: BulletinItem {
/**
* The object managing the item. Required by the `BulletinItem` protocol.
*
* You can use it to switch to the previous/next item or dismiss the bulletin.
*
* Always check if it is nil, as the manager will unset this value when the current item changes.
*/
weak var manager: BulletinManager?
/**
* Whether the item can be dismissed. If you set this value to `true`, the user will be able
* to dimiss the bulletin by tapping outside the card.
*
* You should set it to true for optional items or if it is the last in a configuration sequence.
*/
var isDismissable: Bool = false
/**
* The item to display after this one. You can modify it at runtime based on user selection for
* instance.
*
* Here, we will change the time based on the pet chosen by the user.
*/
var nextItem: BulletinItem?
/**
* An object that creates standard inteface components.
*
* You should use it to create views whenever possible. It also allows to customize the tint color
* of the buttons.
*/
let interfaceFactory = BulletinInterfaceFactory()
// MARK: - Interface Elements
private var catButtonContainer: ContainerView<UIButton>!
private var dogButtonContainer: ContainerView<UIButton>!
private var saveButtonContainer: ContainerView<HighlightButton>!
private var selectionFeedbackGenerator = SelectionFeedbackGenerator()
// MARK: - BulletinItem
/**
* Called by the manager when the item is about to be removed from the bulletin.
*
* Use this function as an opportunity to do any clean up or remove tap gesture recognizers /
* button targets from your views to avoid retain cycles.
*/
func tearDown() {
catButtonContainer?.contentView.removeTarget(self, action: nil, for: .touchUpInside)
dogButtonContainer?.contentView.removeTarget(self, action: nil, for: .touchUpInside)
saveButtonContainer?.contentView.removeTarget(self, action: nil, for: .touchUpInside)
}
/**
* Called by the manager to build the view hierachy of the bulletin.
*
* We need to return the view in the order we want them displayed. You should use a
* `BulletinInterfaceFactory` to generate standard views, such as title labels and buttons.
*/
func makeArrangedSubviews() -> [UIView] {
var arrangedSubviews = [UIView]()
let favoriteTabIndex = BulletinDataSource.favoriteTabIndex
// Title Label
let titleLabel = interfaceFactory.makeTitleLabel()
titleLabel.text = "Choose your Favorite"
arrangedSubviews.append(titleLabel)
// Description Label
let isDescriptionCompact = false // The text is short, so we don't need to display it with a smaller font
let descriptionLabel = interfaceFactory.makeDescriptionLabel(isCompact: isDescriptionCompact)
descriptionLabel.text = "Your favorite pets will appear when you open the app."
arrangedSubviews.append(descriptionLabel)
// Pets Stack
// We add choice cells to a group stack because they need less spacing
let petsStack = interfaceFactory.makeGroupStack(spacing: 16)
arrangedSubviews.append(petsStack)
// Cat Button
let catButtonContainer = createChoiceCell(title: "🐱 Cats", isSelected: favoriteTabIndex == 0)
catButtonContainer.contentView.addTarget(self, action: #selector(catButtonTapped), for: .touchUpInside)
petsStack.addArrangedSubview(catButtonContainer)
self.catButtonContainer = catButtonContainer
// Dog Button
let dogButtonContainer = createChoiceCell(title: "🐶 Dogs", isSelected: favoriteTabIndex == 1)
dogButtonContainer.contentView.addTarget(self, action: #selector(dogButtonTapped), for: .touchUpInside)
petsStack.addArrangedSubview(dogButtonContainer)
self.dogButtonContainer = dogButtonContainer
// Save Button
let saveButtonContainer = interfaceFactory.makeActionButton(title: "Save")
saveButtonContainer.contentView.addTarget(self, action: #selector(saveButtonTapped), for: .touchUpInside)
arrangedSubviews.append(saveButtonContainer)
return arrangedSubviews
}
// MARK: - Custom Views
/**
* Creates a custom choice cell.
*/
func createChoiceCell(title: String, isSelected: Bool) -> ContainerView<UIButton> {
let button = UIButton(type: .system)
button.setTitle(title, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .semibold)
button.contentHorizontalAlignment = .center
let buttonContainer = ContainerView<UIButton>(button)
buttonContainer.layer.cornerRadius = 12
buttonContainer.layer.borderWidth = 2
buttonContainer.heightAnchor.constraint(equalToConstant: 55).isActive = true
let buttonColor = isSelected ? interfaceFactory.tintColor : .lightGray
buttonContainer.layer.borderColor = buttonColor.cgColor
buttonContainer.contentView.setTitleColor(buttonColor, for: .normal)
buttonContainer.layer.borderColor = buttonColor.cgColor
return buttonContainer
}
// MARK: - Touch Events
/// Called when the cat button is tapped.
@objc func catButtonTapped() {
// Play haptic feedback
selectionFeedbackGenerator.prepare()
selectionFeedbackGenerator.selectionChanged()
// Update UI
let catButtonColor = interfaceFactory.tintColor
catButtonContainer?.layer.borderColor = catButtonColor.cgColor
catButtonContainer?.contentView.setTitleColor(catButtonColor, for: .normal)
let dogButtonColor = UIColor.lightGray
dogButtonContainer?.layer.borderColor = dogButtonColor.cgColor
dogButtonContainer?.contentView.setTitleColor(dogButtonColor, for: .normal)
// Send a notification to inform observers of the change
NotificationCenter.default.post(name: .FavoriteTabIndexDidChange,
object: self,
userInfo: ["Index": 0])
// Set the next item
nextItem = PetSelectorValidationBulletinPage(animalName: "cats", animalEmoji: "🐱")
}
/// Called when the dog button is tapped.
@objc func dogButtonTapped() {
// Play haptic feedback
selectionFeedbackGenerator.prepare()
selectionFeedbackGenerator.selectionChanged()
// Update UI
let catButtonColor = UIColor.lightGray
catButtonContainer?.layer.borderColor = catButtonColor.cgColor
catButtonContainer?.contentView.setTitleColor(catButtonColor, for: .normal)
let dogButtonColor = interfaceFactory.tintColor
dogButtonContainer?.layer.borderColor = dogButtonColor.cgColor
dogButtonContainer?.contentView.setTitleColor(dogButtonColor, for: .normal)
// Send a notification to inform observers of the change
NotificationCenter.default.post(name: .FavoriteTabIndexDidChange,
object: self,
userInfo: ["Index": 1])
// Set the next item
nextItem = PetSelectorValidationBulletinPage(animalName: "dogs", animalEmoji: "🐶")
}
/// Called when the save button is tapped.
@objc func saveButtonTapped() {
// Play haptic feedback
selectionFeedbackGenerator.prepare()
selectionFeedbackGenerator.selectionChanged()
// Ask the manager to present the next item.
displayNextItem()
}
}
/**
* A bulletin page that allows the user to validate its selection
*
* This item demonstrates popping to the previous item.
*/
class PetSelectorValidationBulletinPage: BulletinItem {
weak var manager: BulletinManager? = nil
var isDismissable: Bool = false
var nextItem: BulletinItem? = BulletinDataSource.makeCompletionPage()
let interfaceFactory = BulletinInterfaceFactory()
// MARK: - Configuration
let animalName: String
let animalEmoji: String
init(animalName: String, animalEmoji: String) {
self.animalName = animalName
self.animalEmoji = animalEmoji
}
private var selectionFeedbackGenerator = SelectionFeedbackGenerator()
private var successFeedbackGenerator = SuccessFeedbackGenerator()
// MARK: - Interface Elements
private var validateButton: ContainerView<HighlightButton>?
private var backButton: UIButton?
// MARK: - BulletinItem
func tearDown() {
validateButton?.contentView.removeTarget(self, action: nil, for: .touchUpInside)
backButton?.removeTarget(self, action: nil, for: .touchUpInside)
}
func makeArrangedSubviews() -> [UIView] {
var arrangedSubviews = [UIView]()
// Title Label
let titleLabel = interfaceFactory.makeTitleLabel()
titleLabel.text = "Choose your Favorite"
arrangedSubviews.append(titleLabel)
// Emoji
let emojiLabel = UILabel()
emojiLabel.numberOfLines = 1
emojiLabel.textAlignment = .center
emojiLabel.adjustsFontSizeToFitWidth = true
emojiLabel.font = UIFont.systemFont(ofSize: 66)
emojiLabel.text = animalEmoji
arrangedSubviews.append(emojiLabel)
// Description Label
let descriptionLabel = interfaceFactory.makeDescriptionLabel(isCompact: false)
descriptionLabel.text = "You chose \(animalName) as your favorite animal type. Are you sure?"
arrangedSubviews.append(descriptionLabel)
// Validate Button
let buttonsStack = interfaceFactory.makeGroupStack()
arrangedSubviews.append(buttonsStack)
let validateButton = interfaceFactory.makeActionButton(title: "Validate")
buttonsStack.addArrangedSubview(validateButton)
validateButton.contentView.addTarget(self, action: #selector(validateButtonTapped), for: .touchUpInside)
self.validateButton = validateButton
// Back Button
let backButton = interfaceFactory.makeAlternativeButton(title: "Change")
buttonsStack.addArrangedSubview(backButton)
backButton.addTarget(self, action: #selector(backButtonTapped), for: .touchUpInside)
self.backButton = backButton
return arrangedSubviews
}
// MARK: - Touch Events
@objc private func validateButtonTapped() {
// Play success haptic feedback
successFeedbackGenerator.prepare()
successFeedbackGenerator.success()
// Display next item
nextItem = BulletinDataSource.makeCompletionPage()
displayNextItem()
}
@objc private func backButtonTapped() {
// Play selection haptic feedback
selectionFeedbackGenerator.prepare()
selectionFeedbackGenerator.selectionChanged()
// Display previous item
manager?.popItem()
}
}