-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathPhotonActionSheet.swift
660 lines (568 loc) · 26.8 KB
/
PhotonActionSheet.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import Storage
import SnapKit
import Shared
private struct PhotonActionSheetUX {
static let MaxWidth: CGFloat = 414
static let Padding: CGFloat = 10
static let HeaderFooterHeight: CGFloat = 20
static let RowHeight: CGFloat = 50
static let BorderWidth: CGFloat = 0.5
static let BorderColor = UIColor(white: 0, alpha: 0.1)
static let CornerRadius: CGFloat = 10
static let SiteImageViewSize = 52
static let IconSize = CGSize(width: 24, height: 24)
static let SiteHeaderName = "PhotonActionSheetSiteHeaderView"
static let TitleHeaderName = "PhotonActionSheetTitleHeaderView"
static let CellName = "PhotonActionSheetCell"
static let CloseButtonHeight: CGFloat = 56
static let TablePadding: CGFloat = 6
}
public struct PhotonActionSheetItem {
public fileprivate(set) var title: String
public fileprivate(set) var text: String?
public fileprivate(set) var iconString: String?
public fileprivate(set) var iconURL: URL?
public var isEnabled: Bool // Used by toggles like nightmode to switch tint color
public fileprivate(set) var accessory: PhotonActionSheetCellAccessoryType
public fileprivate(set) var accessoryText: String?
public fileprivate(set) var bold: Bool = false
public fileprivate(set) var handler: ((PhotonActionSheetItem) -> Void)?
init(title: String, text: String? = nil, iconString: String? = nil, iconURL: URL? = nil, isEnabled: Bool = false, accessory: PhotonActionSheetCellAccessoryType = .None, accessoryText: String? = nil, bold: Bool? = false, handler: ((PhotonActionSheetItem) -> Void)? = nil) {
self.title = title
self.iconString = iconString
self.iconURL = iconURL
self.isEnabled = isEnabled
self.accessory = accessory
self.handler = handler
self.text = text
self.accessoryText = accessoryText
self.bold = bold ?? false
}
}
private enum PresentationStyle {
case centered // used in the home panels
case bottom // used to display the menu on phone sized devices
case popover // when displayed on the iPad
}
class PhotonActionSheet: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate {
fileprivate(set) var actions: [[PhotonActionSheetItem]]
private var site: Site?
private let style: PresentationStyle
private var tintColor = UIColor.Photon.Grey80
private var heightConstraint: Constraint?
var tableView = UITableView(frame: .zero, style: .grouped)
lazy var tapRecognizer: UITapGestureRecognizer = {
let tapRecognizer = UITapGestureRecognizer()
tapRecognizer.addTarget(self, action: #selector(dismiss))
tapRecognizer.numberOfTapsRequired = 1
tapRecognizer.cancelsTouchesInView = false
tapRecognizer.delegate = self
return tapRecognizer
}()
lazy var closeButton: UIButton = {
let button = UIButton()
button.setTitle(Strings.CloseButtonTitle, for: .normal)
button.backgroundColor = UIConstants.AppBackgroundColor
button.setTitleColor(UIConstants.SystemBlueColor, for: .normal)
button.layer.cornerRadius = PhotonActionSheetUX.CornerRadius
button.titleLabel?.font = DynamicFontHelper.defaultHelper.DeviceFontExtraLargeBold
button.addTarget(self, action: #selector(dismiss), for: .touchUpInside)
button.accessibilityIdentifier = "PhotonMenu.close"
return button
}()
var photonTransitionDelegate: UIViewControllerTransitioningDelegate? {
didSet {
self.transitioningDelegate = photonTransitionDelegate
}
}
init(site: Site, actions: [PhotonActionSheetItem]) {
self.site = site
self.actions = [actions]
self.style = .centered
super.init(nibName: nil, bundle: nil)
}
init(title: String? = nil, actions: [[PhotonActionSheetItem]], style presentationStyle: UIModalPresentationStyle) {
self.actions = actions
self.style = presentationStyle == .popover ? .popover : .bottom
super.init(nibName: nil, bundle: nil)
self.title = title
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
if style == .centered {
applyBackgroundBlur()
self.tintColor = UIConstants.SystemBlueColor
}
view.addGestureRecognizer(tapRecognizer)
view.addSubview(tableView)
view.accessibilityIdentifier = "Action Sheet"
// In a popover the popover provides the blur background
// Not using a background color allows the view to style correctly with the popover arrow
if self.popoverPresentationController == nil {
tableView.backgroundColor = UIConstants.AppBackgroundColor.withAlphaComponent(0.7)
let blurEffect = UIBlurEffect(style: .light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
tableView.backgroundView = blurEffectView
} else {
tableView.backgroundColor = .clear
}
let width = min(self.view.frame.size.width, PhotonActionSheetUX.MaxWidth) - (PhotonActionSheetUX.Padding * 2)
if style == .bottom {
self.view.addSubview(closeButton)
closeButton.snp.makeConstraints { make in
make.centerX.equalTo(self.view.snp.centerX)
make.width.equalTo(width)
make.height.equalTo(PhotonActionSheetUX.CloseButtonHeight)
make.bottom.equalTo(self.view.safeArea.bottom).inset(PhotonActionSheetUX.Padding)
}
}
if style == .popover {
self.actions = actions.map({ $0.reversed() }).reversed()
tableView.snp.makeConstraints { make in
make.edges.equalTo(self.view)
}
} else {
tableView.snp.makeConstraints { make in
make.centerX.equalTo(self.view.snp.centerX)
switch style {
case .bottom, .popover:
make.bottom.equalTo(closeButton.snp.top).offset(-PhotonActionSheetUX.Padding)
case .centered:
make.centerY.equalTo(self.view.snp.centerY)
}
make.width.equalTo(width)
}
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.bounces = false
tableView.delegate = self
tableView.dataSource = self
tableView.keyboardDismissMode = .onDrag
tableView.register(PhotonActionSheetCell.self, forCellReuseIdentifier: PhotonActionSheetUX.CellName)
tableView.register(PhotonActionSheetSiteHeaderView.self, forHeaderFooterViewReuseIdentifier: PhotonActionSheetUX.SiteHeaderName)
tableView.register(PhotonActionSheetTitleHeaderView.self, forHeaderFooterViewReuseIdentifier: PhotonActionSheetUX.TitleHeaderName)
tableView.register(PhotonActionSheetSeparator.self, forHeaderFooterViewReuseIdentifier: "SeparatorSectionHeader")
tableView.register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "EmptyHeader")
tableView.estimatedRowHeight = PhotonActionSheetUX.RowHeight
tableView.estimatedSectionFooterHeight = PhotonActionSheetUX.HeaderFooterHeight
// When the menu style is centered the header is much bigger than default. Set a larger estimated height to make sure autolayout sizes the view correctly
tableView.estimatedSectionHeaderHeight = (style == .centered) ? PhotonActionSheetUX.RowHeight : PhotonActionSheetUX.HeaderFooterHeight
tableView.isScrollEnabled = true
tableView.showsVerticalScrollIndicator = false
tableView.layer.cornerRadius = PhotonActionSheetUX.CornerRadius
tableView.separatorStyle = .none
tableView.cellLayoutMarginsFollowReadableWidth = false
tableView.accessibilityIdentifier = "Context Menu"
let footer = UIView(frame: CGRect(width: tableView.frame.width, height: PhotonActionSheetUX.Padding))
tableView.tableHeaderView = footer
tableView.tableFooterView = footer.clone()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let maxHeight = self.view.frame.height - (style == .bottom ? PhotonActionSheetUX.CloseButtonHeight : 0)
tableView.snp.makeConstraints { make in
heightConstraint?.deactivate()
// The height of the menu should be no more than 80 percent of the screen
heightConstraint = make.height.equalTo(min(self.tableView.contentSize.height, maxHeight * 0.8)).constraint
}
if style == .popover {
self.preferredContentSize = self.tableView.contentSize
}
}
private func applyBackgroundBlur() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
if let screenshot = appDelegate.window?.screenshot() {
let blurredImage = screenshot.applyBlur(withRadius: 5,
blurType: BOXFILTER,
tintColor: UIColor.black.withAlphaComponent(0.2),
saturationDeltaFactor: 1.8,
maskImage: nil)
let imageView = UIImageView(image: blurredImage)
view.addSubview(imageView)
}
}
@objc func dismiss(_ gestureRecognizer: UIGestureRecognizer?) {
self.dismiss(animated: true, completion: nil)
}
deinit {
tableView.dataSource = nil
tableView.delegate = nil
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if self.traitCollection.verticalSizeClass != previousTraitCollection?.verticalSizeClass
|| self.traitCollection.horizontalSizeClass != previousTraitCollection?.horizontalSizeClass {
updateViewConstraints()
}
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if tableView.frame.contains(touch.location(in: self.view)) {
return false
}
return true
}
func numberOfSections(in tableView: UITableView) -> Int {
return actions.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return actions[section].count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var action = actions[indexPath.section][indexPath.row]
guard let handler = action.handler else {
self.dismiss(nil)
return
}
// Switches can be toggled on/off without dismissing the menu
if action.accessory == .Switch {
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()
action.isEnabled = !action.isEnabled
actions[indexPath.section][indexPath.row] = action
self.tableView.deselectRow(at: indexPath, animated: true)
self.tableView.reloadData()
} else {
self.dismiss(nil)
}
return handler(action)
}
func tableView(_ tableView: UITableView, hasFullWidthSeparatorForRowAtIndexPath indexPath: IndexPath) -> Bool {
return false
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: PhotonActionSheetUX.CellName, for: indexPath) as! PhotonActionSheetCell
let action = actions[indexPath.section][indexPath.row]
cell.tintColor = self.tintColor
cell.configure(with: action)
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// If we have multiple sections show a separator for each one except the first.
if section > 0 {
return tableView.dequeueReusableHeaderFooterView(withIdentifier: "SeparatorSectionHeader")
}
if let site = site {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: PhotonActionSheetUX.SiteHeaderName) as! PhotonActionSheetSiteHeaderView
header.tintColor = self.tintColor
header.configure(with: site)
return header
} else if let title = title {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: PhotonActionSheetUX.TitleHeaderName) as! PhotonActionSheetTitleHeaderView
header.tintColor = self.tintColor
header.configure(with: title)
return header
}
// A header height of at least 1 is required to make sure the default header size isnt used when laying out with AutoLayout
let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: "EmptyHeader")
view?.snp.makeConstraints { make in
make.height.equalTo(1)
}
return view
}
// A footer height of at least 1 is required to make sure the default footer size isnt used when laying out with AutoLayout
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: "EmptyHeader")
view?.snp.makeConstraints { make in
make.height.equalTo(1)
}
return view
}
}
private class PhotonActionSheetTitleHeaderView: UITableViewHeaderFooterView {
static let Padding: CGFloat = 12
lazy var titleLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.font = DynamicFontHelper.defaultHelper.SmallSizeRegularWeightAS
titleLabel.numberOfLines = 1
titleLabel.textColor = UIAccessibilityDarkerSystemColorsEnabled() ? UIColor.black : UIColor.gray
return titleLabel
}()
lazy var separatorView: UIView = {
let separatorLine = UIView()
separatorLine.backgroundColor = UIColor.lightGray
return separatorLine
}()
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
self.backgroundView = UIView()
self.backgroundView?.backgroundColor = .clear
contentView.addSubview(titleLabel)
titleLabel.snp.makeConstraints { make in
make.leading.equalTo(contentView).offset(PhotonActionSheetTitleHeaderView.Padding)
make.trailing.equalTo(contentView)
make.top.equalTo(contentView).offset(PhotonActionSheetUX.TablePadding)
}
contentView.addSubview(separatorView)
separatorView.snp.makeConstraints { make in
make.leading.trailing.equalTo(self)
make.top.equalTo(titleLabel.snp.bottom).offset(PhotonActionSheetUX.TablePadding)
make.bottom.equalTo(contentView).inset(PhotonActionSheetUX.TablePadding)
make.height.equalTo(0.5)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configure(with title: String) {
self.titleLabel.text = title
}
override func prepareForReuse() {
self.titleLabel.text = nil
}
}
private class PhotonActionSheetSiteHeaderView: UITableViewHeaderFooterView {
static let Padding: CGFloat = 12
static let VerticalPadding: CGFloat = 2
lazy var titleLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.font = DynamicFontHelper.defaultHelper.MediumSizeBoldFontAS
titleLabel.textAlignment = .left
titleLabel.numberOfLines = 2
return titleLabel
}()
lazy var descriptionLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.font = DynamicFontHelper.defaultHelper.MediumSizeRegularWeightAS
titleLabel.textAlignment = .left
titleLabel.numberOfLines = 1
return titleLabel
}()
lazy var siteImageView: UIImageView = {
let siteImageView = UIImageView()
siteImageView.contentMode = .center
siteImageView.clipsToBounds = true
siteImageView.layer.cornerRadius = PhotonActionSheetUX.CornerRadius
siteImageView.layer.borderColor = PhotonActionSheetUX.BorderColor.cgColor
siteImageView.layer.borderWidth = PhotonActionSheetUX.BorderWidth
return siteImageView
}()
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
self.backgroundView = UIView()
self.backgroundView?.backgroundColor = .clear
contentView.addSubview(siteImageView)
siteImageView.snp.remakeConstraints { make in
make.top.equalTo(contentView).offset(PhotonActionSheetSiteHeaderView.Padding)
make.centerY.equalTo(contentView)
make.leading.equalTo(contentView).offset(PhotonActionSheetSiteHeaderView.Padding)
make.size.equalTo(PhotonActionSheetUX.SiteImageViewSize)
}
let stackView = UIStackView(arrangedSubviews: [titleLabel, descriptionLabel])
stackView.spacing = PhotonActionSheetSiteHeaderView.VerticalPadding
stackView.alignment = .leading
stackView.axis = .vertical
contentView.addSubview(stackView)
stackView.snp.makeConstraints { make in
make.leading.equalTo(siteImageView.snp.trailing).offset(PhotonActionSheetSiteHeaderView.Padding)
make.trailing.equalTo(contentView).inset(PhotonActionSheetSiteHeaderView.Padding)
make.centerY.equalTo(siteImageView.snp.centerY)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepareForReuse() {
self.siteImageView.image = nil
self.siteImageView.backgroundColor = UIColor.clear
}
func configure(with site: Site) {
self.siteImageView.setFavicon(forSite: site) { (color, url) in
self.siteImageView.backgroundColor = color
self.siteImageView.image = self.siteImageView.image?.createScaled(PhotonActionSheetUX.IconSize)
}
self.titleLabel.text = site.title.isEmpty ? site.url : site.title
self.descriptionLabel.text = site.tileURL.baseDomain
}
}
private struct PhotonActionSheetCellUX {
static let LabelColor = UIConstants.SystemBlueColor
static let BorderWidth: CGFloat = CGFloat(0.5)
static let CellSideOffset = 20
static let TitleLabelOffset = 10
static let CellTopBottomOffset = 12
static let StatusIconSize = 24
static let SelectedOverlayColor = UIColor(white: 0.0, alpha: 0.25)
static let CornerRadius: CGFloat = 3
}
private class PhotonActionSheetSeparator: UITableViewHeaderFooterView {
let separatorLineView = UIView()
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
self.backgroundView = UIView()
self.backgroundView?.backgroundColor = .clear
separatorLineView.backgroundColor = UIColor.lightGray
self.contentView.addSubview(separatorLineView)
separatorLineView.snp.makeConstraints { make in
make.leading.trailing.equalTo(self)
make.centerY.equalTo(self)
make.height.equalTo(0.5)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
public enum PhotonActionSheetCellAccessoryType {
case Disclosure
case Switch
case Text
case None
}
private class PhotonActionSheetCell: UITableViewCell {
static let Padding: CGFloat = 16
static let HorizontalPadding: CGFloat = 10
static let VerticalPadding: CGFloat = 2
static let IconSize = 16
lazy var titleLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.font = DynamicFontHelper.defaultHelper.LargeSizeRegularWeightAS
titleLabel.minimumScaleFactor = 0.8 // Scale the font if we run out of space
titleLabel.textColor = PhotonActionSheetCellUX.LabelColor
titleLabel.setContentHuggingPriority(.defaultHigh, for: .vertical)
titleLabel.numberOfLines = 4
titleLabel.adjustsFontSizeToFitWidth = true
return titleLabel
}()
lazy var subtitleLabel: UILabel = {
let textLabel = UILabel()
textLabel.font = DynamicFontHelper.defaultHelper.SmallSizeRegularWeightAS
textLabel.setContentHuggingPriority(.defaultHigh, for: .vertical)
textLabel.minimumScaleFactor = 0.75 // Scale the font if we run out of space
textLabel.textColor = PhotonActionSheetCellUX.LabelColor
textLabel.numberOfLines = 3
textLabel.adjustsFontSizeToFitWidth = true
return textLabel
}()
lazy var statusIcon: UIImageView = {
let statusIcon = UIImageView()
statusIcon.contentMode = .scaleAspectFit
statusIcon.clipsToBounds = true
statusIcon.layer.cornerRadius = PhotonActionSheetCellUX.CornerRadius
statusIcon.setContentHuggingPriority(.required, for: .horizontal)
return statusIcon
}()
lazy var disclosureLabel: UILabel = {
let label = UILabel()
return label
}()
lazy var toggleSwitch: UIImageView = {
let toggle = UIImageView(image: UIImage(named: "menu-Toggle-Off"))
toggle.contentMode = .scaleAspectFit
return toggle
}()
lazy var selectedOverlay: UIView = {
let selectedOverlay = UIView()
selectedOverlay.backgroundColor = PhotonActionSheetCellUX.SelectedOverlayColor
selectedOverlay.isHidden = true
return selectedOverlay
}()
lazy var disclosureIndicator: UIImageView = {
let disclosureIndicator = UIImageView(image: UIImage(named: "menu-Disclosure"))
disclosureIndicator.contentMode = .scaleAspectFit
disclosureIndicator.layer.cornerRadius = PhotonActionSheetCellUX.CornerRadius
disclosureIndicator.setContentHuggingPriority(.required, for: .horizontal)
return disclosureIndicator
}()
lazy var stackView: UIStackView = {
let stackView = UIStackView()
stackView.spacing = PhotonActionSheetCell.Padding
stackView.alignment = .center
stackView.axis = .horizontal
return stackView
}()
override var isSelected: Bool {
didSet {
self.selectedOverlay.isHidden = !isSelected
}
}
override func prepareForReuse() {
self.statusIcon.image = nil
disclosureIndicator.removeFromSuperview()
disclosureLabel.removeFromSuperview()
toggleSwitch.removeFromSuperview()
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
isAccessibilityElement = true
contentView.addSubview(selectedOverlay)
backgroundColor = .clear
selectedOverlay.snp.makeConstraints { make in
make.edges.equalTo(contentView)
}
// Setup our StackViews
let textStackView = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel])
textStackView.spacing = PhotonActionSheetCell.VerticalPadding
textStackView.setContentHuggingPriority(.defaultLow, for: .horizontal)
textStackView.alignment = .leading
textStackView.axis = .vertical
stackView.addArrangedSubview(statusIcon)
stackView.addArrangedSubview(textStackView)
contentView.addSubview(stackView)
let padding = PhotonActionSheetCell.Padding
let topPadding = PhotonActionSheetCell.HorizontalPadding
stackView.snp.makeConstraints { make in
make.edges.equalTo(contentView).inset(UIEdgeInsets(top: topPadding, left: padding, bottom: topPadding, right: padding))
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configure(with action: PhotonActionSheetItem) {
titleLabel.text = action.title
titleLabel.textColor = self.tintColor
titleLabel.textColor = action.accessory == .Text ? titleLabel.textColor.withAlphaComponent(0.6) : titleLabel.textColor
subtitleLabel.text = action.text
subtitleLabel.textColor = self.tintColor
subtitleLabel.isHidden = action.text == nil
titleLabel.font = action.bold ? DynamicFontHelper.defaultHelper.DeviceFontLargeBold : DynamicFontHelper.defaultHelper.LargeSizeRegularWeightAS
accessibilityIdentifier = action.iconString
accessibilityLabel = action.title
selectionStyle = action.handler != nil ? .default : .none
if let iconName = action.iconString, let image = UIImage(named: iconName)?.withRenderingMode(.alwaysTemplate) {
statusIcon.sd_setImage(with: action.iconURL, placeholderImage: image, options: []) { (img, err, _, _) in
if let img = img {
self.statusIcon.image = img.createScaled(CGSize(width: 30, height: 30))
}
}
// When the iconURL is not nil we are most likely showing a profile picture.
// In that case we do not need a tint color. And make sure the image is sized correctly
// This is for the sync profile button in the menu
if action.iconURL == nil {
statusIcon.tintColor = self.tintColor
} else {
self.statusIcon.image = self.statusIcon.image?.createScaled(CGSize(width: 30, height: 30))
}
if statusIcon.superview == nil {
stackView.insertArrangedSubview(statusIcon, at: 0)
}
} else {
statusIcon.removeFromSuperview()
}
switch action.accessory {
case .Text:
disclosureLabel.font = action.bold ? DynamicFontHelper.defaultHelper.DeviceFontLargeBold : DynamicFontHelper.defaultHelper.LargeSizeRegularWeightAS
disclosureLabel.text = action.accessoryText
disclosureLabel.textColor = titleLabel.textColor
stackView.addArrangedSubview(disclosureLabel)
case .Disclosure:
stackView.addArrangedSubview(disclosureIndicator)
case .Switch:
let image = action.isEnabled ? UIImage(named: "menu-Toggle-On") : UIImage(named: "menu-Toggle-Off")
toggleSwitch.isAccessibilityElement = true
toggleSwitch.accessibilityIdentifier = action.isEnabled ? "enabled" : "disabled"
toggleSwitch.image = image
stackView.addArrangedSubview(toggleSwitch)
default:
break // Do nothing. The rest are not supported yet.
}
}
}