-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathAvatarTitleView.swift
407 lines (333 loc) · 13.9 KB
/
AvatarTitleView.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
import UIKit
// MARK: AvatarTitleView
/// A helper view used by `NavigationBar` capable of displaying a large title and an avatar.
class AvatarTitleView: UIView, TokenizedControlInternal, TwoLineTitleViewDelegate {
enum Style: Int {
case primary
case system
}
typealias TokenSetKeyType = AvatarTitleViewTokenSet.Tokens
lazy var tokenSet: AvatarTitleViewTokenSet = .init(style: { [weak self] in
self?.style ?? .primary
})
var personaData: Persona? {
didSet {
updateProfileButtonVisibility()
if let avatarState = avatar?.state {
avatarState.primaryText = personaData?.name
avatarState.secondaryText = personaData?.email
avatarState.image = personaData?.image
avatarState.imageBasedRingColor = personaData?.imageBasedRingColor
avatarState.hasRingInnerGap = personaData?.hasRingInnerGap ?? true
avatarState.isRingVisible = personaData?.isRingVisible ?? false
avatarState.presence = personaData?.presence ?? .none
avatarState.isOutOfOffice = personaData?.isOutOfOffice ?? false
let color = personaData?.color
avatarState.backgroundColor = color
avatarState.ringColor = color
}
}
}
var avatarSize: NavigationBar.ElementSize = .automatic {
didSet {
switch avatarSize {
case .automatic:
return
case .contracted:
avatar?.state.size = TokenSetType.compactAvatarSize
case .expanded:
avatar?.state.size = TokenSetType.avatarSize
}
}
}
var avatarAccessibilityLabel: String? {
return avatarCustomAccessibilityLabel ?? "Accessibility.LargeTitle.ProfileView".localized
}
var avatarCustomAccessibilityLabel: String? {
didSet {
updateAvatarAccessibility()
}
}
var avatarOverrideStyle: MSFAvatarStyle? {
didSet {
if let style = avatarOverrideStyle {
updateProfileButtonVisibility()
avatar?.state.style = style
}
}
}
var style: Style = .primary {
didSet {
updateAppearance()
twoLineTitleView.currentStyle = style == .primary ? .primary : .system
let avatarStyle: MSFAvatarStyle
if let avatarOverrideStyle {
avatarStyle = avatarOverrideStyle
} else {
avatarStyle = (style == .primary) ? .default : .accent
}
avatar?.state.style = avatarStyle
}
}
var onAvatarTapped: (() -> Void)? { // called in response to a tap on the MSFAvatar's view
didSet {
updateAvatarViewPointerInteraction()
updateAvatarAccessibility()
}
}
public func visibleAvatarView() -> UIView? {
if !showsProfileButton {
return nil
}
return avatar
}
private var avatar: MSFAvatar? // circular view displaying the profile information
private let titleContainerView = UIView()
private var titleStyle: NavigationBar.TitleStyle = .system {
didSet {
if oldValue != titleStyle {
updateTitleContainerView()
updateProfileButtonVisibility()
}
}
}
// TODO: Once we have an iOS 15 minimum, we can use UIButton.Configuration to eliminate the need for TwoLineTitleView here
private let titleButton = UIButton() // button used to display the title of the current navigation item
private let twoLineTitleView = TwoLineTitleView() // view used to display the title of the current navigation item if a subtitle exists
private let contentStackView = UIStackView() // containing stack view
private let tapGesture = UITapGestureRecognizer() // tap used to trigger expansion. Applied to entire navigation bar
private var showsProfileButton: Bool = true { // whether to display the customizable profile button
didSet {
avatar?.isHidden = !showsProfileButton
setupAccessibility()
}
}
private var hasLeftBarButtonItems: Bool = false {
didSet {
updateProfileButtonVisibility()
}
}
private var respondsToTaps: Bool = true // whether to respond to the various tap gestures/actions that are incorproated into the navigation bar
override init(frame: CGRect) {
super.init(frame: frame)
initBase()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initBase()
}
/// Base function for initialization
private func initBase() {
setupLayout()
setupAccessibility()
twoLineTitleView.delegate = self
tokenSet.registerOnUpdate(for: self) { [weak self] in
self?.updateAppearance()
}
}
// MARK: - Theme updates
open override func willMove(toWindow newWindow: UIWindow?) {
super.willMove(toWindow: newWindow)
guard let newWindow else {
return
}
tokenSet.update(newWindow.fluentTheme)
}
@objc private func updateAppearance() {
titleButton.setTitleColor(tokenSet[.titleColor].uiColor, for: .normal)
titleButton.titleLabel?.font = tokenSet[.largeTitleFont].uiFont
twoLineTitleView.currentStyle = style == .primary ? .primary : .system
twoLineTitleView.tokenSet.setOverrides(from: tokenSet, mapping: [
.titleColor: .titleColor,
.titleFont: .titleFont,
.subtitleColor: .subtitleColor,
.subtitleFont: .subtitleFont
])
}
// MARK: - Base Construction Methods
// Constructs various constants based on initial conditions
// Applies constants via autolayout to constructed views
// Also constructs gesture recognizers
private func setupLayout() {
// contentStackView layout
contentStackView.spacing = TokenSetType.contentStackViewSpacing
contentStackView.alignment = .center
contain(view: contentStackView, withInsets: UIEdgeInsets(top: 0,
left: TokenSetType.contentStackViewHorizontalInset,
bottom: 0,
right: TokenSetType.contentStackViewHorizontalInset))
// Avatar setup
let preferredFallbackImageStyle: MSFAvatarStyle = style == .primary ? .default : .accent
let avatar = MSFAvatar(style: preferredFallbackImageStyle,
size: TokenSetType.avatarSize)
let avatarState = avatar.state
avatarState.primaryText = personaData?.name
avatarState.secondaryText = personaData?.email
avatarState.image = personaData?.image
if let color = personaData?.color {
avatarState.backgroundColor = color
avatarState.ringColor = color
}
self.avatar = avatar
let avatarView = avatar
avatarView.translatesAutoresizingMaskIntoConstraints = false
avatarView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleAvatarViewTapped)))
contentStackView.addArrangedSubview(avatarView)
avatarView.centerYAnchor.constraint(equalTo: contentStackView.centerYAnchor).isActive = true
updateTitleContainerView()
contentStackView.addArrangedSubview(titleContainerView)
// title button setup
titleButton.setTitle(nil, for: .normal)
titleButton.titleLabel?.textAlignment = .left
titleButton.contentHorizontalAlignment = .left
titleButton.titleLabel?.adjustsFontSizeToFitWidth = true
titleButton.addTarget(self, action: #selector(AvatarTitleView.titleButtonTapped(sender:)), for: .touchUpInside)
titleButton.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
titleButton.titleLabel?.lineBreakMode = .byTruncatingTail
titleButton.titleLabel?.adjustsFontSizeToFitWidth = false
updateAppearance()
// tap gesture for entire titleView
tapGesture.addTarget(self, action: #selector(AvatarTitleView.handleTitleViewTapped(sender:)))
addGestureRecognizer(tapGesture)
titleButton.showsLargeContentViewer = true
updateAvatarViewPointerInteraction()
}
private func expansionAnimation() {
if avatarSize == .automatic {
avatar?.state.size = TokenSetType.avatarSize
}
layoutIfNeeded()
}
private func contractionAnimation() {
if avatarSize == .automatic {
avatar?.state.size = TokenSetType.compactAvatarSize
}
layoutIfNeeded()
}
private func updateAvatarViewPointerInteraction() {
avatar?.state.hasPointerInteraction = onAvatarTapped != nil
}
private func updateAvatarAccessibility() {
if let avatar = avatar {
let accessibilityLabel = avatarAccessibilityLabel
let avatarState = avatar.state
avatarState.accessibilityLabel = accessibilityLabel
avatarState.hasButtonAccessibilityTrait = onAvatarTapped != nil
let avatarView = avatar
avatarView.showsLargeContentViewer = true
avatarView.largeContentTitle = accessibilityLabel
}
}
// MARK: - UIActions
/// Target for the tap gesture on the avatar view, as it is not a button
///
/// - Parameter gesture: tap gesture on the AvatarView
@objc private func handleAvatarViewTapped(gesture: UITapGestureRecognizer) {
onAvatarTapped?()
}
/// Target for the Title Button's touchUpInside
///
/// - Parameter sender: title button
@objc private func titleButtonTapped(sender: UIButton) {
guard respondsToTaps else {
return
}
requestExpansion()
}
/// Target for the NavigationBar tap gesture
///
/// - Parameter sender: the tap gesture
@objc private func handleTitleViewTapped(sender: UITapGestureRecognizer) {
guard respondsToTaps else {
return
}
requestExpansion()
}
/// Posts a notification requesting that the navigation bar be animated into its larger state
private func requestExpansion() {
NotificationCenter.default.post(name: .accessoryExpansionRequested, object: self)
}
// MARK: - Content Update Methods
private func updateProfileButtonVisibility() {
showsProfileButton = titleStyle.usesLeadingAlignment && !hasLeftBarButtonItems && (personaData != nil || avatarOverrideStyle != nil)
}
private func updateTitleContainerView() {
titleContainerView.removeAllSubviews()
titleContainerView.contain(view: titleStyle == .largeLeading ? titleButton : twoLineTitleView)
}
/// Sets the interface with the provided item's details
///
/// - Parameter navigationItem: instance of UINavigationItem providing inteface information
func update(with navigationItem: UINavigationItem) {
hasLeftBarButtonItems = !(navigationItem.leftBarButtonItems?.isEmpty ?? true)
titleButton.setTitle(navigationItem.title, for: .normal)
titleStyle = navigationItem.titleStyle
twoLineTitleView.setup(navigationItem: navigationItem)
// Hide the avatar if TwoLineTitleView has a leading image for both title and subtitle.
if navigationItem.isTitleImageLeadingForTitleAndSubtitle {
showsProfileButton = false
} else {
updateProfileButtonVisibility()
}
if navigationItem.titleAccessory == nil {
// Use default behavior of requesting an accessory expansion
twoLineTitleView.delegate = self
}
}
// MARK: - Expansion/Contraction Methods
/// Calls the expansion animation block, optionally animated
///
/// - Parameter animated: to animate the block or not
func expand(animated: Bool) {
// Exit early if neither element's size is automatic
guard avatarSize == .automatic else {
return
}
if animated {
UIView.animate(withDuration: NavigationBar.expansionContractionAnimationDuration,
animations: expansionAnimation)
} else {
expansionAnimation()
}
}
/// Calls the contraction animation block, optionally animated
///
/// - Parameter animated: to animate the block or not
func contract(animated: Bool) {
// Exit early if neither element's size is automatic
guard avatarSize == .automatic else {
return
}
if animated {
UIView.animate(withDuration: NavigationBar.expansionContractionAnimationDuration,
animations: contractionAnimation)
} else {
contractionAnimation()
}
}
// MARK: - Accessibility
/// Updates various properties of the TitleView to properly conform to accessibility requirements
private func setupAccessibility() {
titleButton.accessibilityTraits = .header
updateAvatarAccessibility()
// Sets the accessibility elements in the same order as they are laid out in the content view.
accessibilityElements = contentStackView.arrangedSubviews.filter({ arrangedSubview in
return !arrangedSubview.isHidden
})
}
// MARK: - TwoLineTitleViewDelegate
func twoLineTitleViewDidTapOnTitle(_ twoLineTitleView: TwoLineTitleView) {
guard respondsToTaps, twoLineTitleView == self.twoLineTitleView else {
return
}
requestExpansion()
}
}
// MARK: - Notification.Name Declarations
extension NSNotification.Name {
static let accessoryExpansionRequested = Notification.Name("microsoft.fluentui.accessoryExpansionRequested")
}