-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathPreviewDeviceSelectionViewController.swift
139 lines (111 loc) · 4.52 KB
/
PreviewDeviceSelectionViewController.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
import Foundation
class PreviewDeviceSelectionViewController: UIViewController {
enum PreviewDevice: String, CaseIterable {
case desktop = "desktop"
case tablet = "tablet"
case mobile = "mobile"
static var `default`: PreviewDevice {
return UIDevice.current.userInterfaceIdiom == .pad ? .tablet : .mobile
}
var title: String {
switch self {
case .desktop:
return NSLocalizedString("Desktop", comment: "Title for the desktop web preview")
case .tablet:
return NSLocalizedString("Tablet", comment: "Title for the tablet web preview")
case .mobile:
return NSLocalizedString("Mobile", comment: "Title for the mobile web preview")
}
}
var width: CGFloat {
switch self {
case .desktop:
return 1200
case .tablet:
return 800
case .mobile:
return 400
}
}
static var available: [PreviewDevice] {
return [.mobile, .tablet, .desktop]
}
var viewportScript: String {
return String(format: "let parent = document.querySelector('meta[name=viewport]'); parent.setAttribute('content', 'width=%1$d, initial-scale=0');", NSInteger(width))
}
}
var selectedOption: PreviewDevice = PreviewDevice.default
var onDeviceChange: ((PreviewDevice) -> Void)?
lazy var tableView: UITableView = {
let tableView = UITableView()
tableView .translatesAutoresizingMaskIntoConstraints = false
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = .clear
tableView.alwaysBounceVertical = false
tableView.separatorInset = .zero
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
let blurEffect: UIBlurEffect
blurEffect = UIBlurEffect(style: .systemMaterial)
let effectView = UIVisualEffectView(effect: blurEffect)
effectView.backgroundColor = .clear
effectView.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .clear
view.addSubview(effectView)
view.pinSubviewToAllEdges(effectView)
effectView.contentView.addSubview(tableView)
effectView.contentView.pinSubviewToAllEdges(tableView)
tableView.separatorEffect = UIVibrancyEffect(blurEffect: blurEffect)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
override var preferredContentSize: CGSize {
get {
tableView.layoutIfNeeded()
return CGSize(width: 240, height: tableView.contentSize.height)
}
set {
// No-op - Cell is calculated from the table view's contentSize
}
}
}
extension PreviewDeviceSelectionViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return PreviewDevice.available.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.backgroundColor = .clear
let device = PreviewDevice.available[indexPath.row]
cell.textLabel?.text = device.title
cell.textLabel?.font = WPStyleGuide.regularTextFont()
if device == selectedOption {
cell.accessoryType = .checkmark
cell.accessibilityTraits = [.button, .selected]
} else {
cell.accessoryType = .none
cell.accessibilityTraits = .button
}
return cell
}
// This is a "hack" that prevents the bottom cell's separator from being shown.
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view = UIView()
view.backgroundColor = .clear
return view
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.1
}
}
extension PreviewDeviceSelectionViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let newlySelectedDeviceMode = PreviewDevice.available[indexPath.row]
if newlySelectedDeviceMode != selectedOption {
onDeviceChange?(newlySelectedDeviceMode)
}
dismiss(animated: true, completion: nil)
}
}