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
|
//
// ShortcutsDataSource.swift
// MullvadVPN
//
// Created by Nikolay Davydov on 20.08.2022.
// Copyright © 2022 Mullvad VPN AB. All rights reserved.
//
import IntentsUI
import UIKit
final class ShortcutsDataSource: NSObject,
UITableViewDataSource,
UITableViewDelegate,
ShortcutsManagerDelegate
{
private enum CellReuseIdentifiers: String, CaseIterable {
case basicCell
var reusableViewClass: AnyClass {
switch self {
case .basicCell:
return SettingsCell.self
}
}
}
private enum HeaderFooterReuseIdentifier: String, CaseIterable {
case spacer
var reusableViewClass: AnyClass {
switch self {
case .spacer:
return EmptyTableViewHeaderFooterView.self
}
}
}
enum Section: String {
case shortcuts
}
struct Item: Hashable {
let title: String
let shortcut: INShortcut
let voiceShortcut: INVoiceShortcut?
var isAdded: Bool {
return voiceShortcut != nil
}
}
private var snapshot = DataSourceSnapshot<Section, Item>()
weak var delegate: ShortcutsDataSourceDelegate?
weak var tableView: UITableView? {
didSet {
tableView?.delegate = self
tableView?.dataSource = self
registerClasses()
}
}
override init() {
super.init()
updateDataSnapshot(voiceShortcuts: [])
ShortcutsManager.shared.delegate = self
ShortcutsManager.shared.updateVoiceShortcuts()
}
private func registerClasses() {
CellReuseIdentifiers.allCases.forEach { cellIdentifier in
tableView?.register(
cellIdentifier.reusableViewClass,
forCellReuseIdentifier: cellIdentifier.rawValue
)
}
HeaderFooterReuseIdentifier.allCases.forEach { reuseIdentifier in
tableView?.register(
reuseIdentifier.reusableViewClass,
forHeaderFooterViewReuseIdentifier: reuseIdentifier.rawValue
)
}
}
private func updateDataSnapshot(voiceShortcuts: [INVoiceShortcut]) {
var items = [Item]()
for data in ShortcutData.allCases {
guard let shortcut = data.shortcut else { continue }
let voiceShortcut = voiceShortcuts.first(where: { voiceShortcut in
isVoiceShortcut(voiceShortcut, invokes: shortcut)
})
let item = Item(
title: data.title,
shortcut: shortcut,
voiceShortcut: voiceShortcut
)
items.append(item)
}
var newSnapshot = DataSourceSnapshot<Section, Item>()
newSnapshot.appendSections([.shortcuts])
newSnapshot.appendItems(items, in: .shortcuts)
snapshot = newSnapshot
}
/// Returns whether the voice shortcut performs the same action as the specified shortcut.
private func isVoiceShortcut(
_ voiceShortcut: INVoiceShortcut,
invokes shortcut: INShortcut
) -> Bool {
if let a = voiceShortcut.shortcut.intent, let b = shortcut.intent {
return type(of: a) == type(of: b)
}
return false
}
// MARK: - UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionIdentifier = snapshot.section(at: section)!
return snapshot.numberOfItems(in: sectionIdentifier) ?? 0
}
func numberOfSections(in tableView: UITableView) -> Int {
return snapshot.numberOfSections()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = snapshot.itemForIndexPath(indexPath)!
let cell = tableView.dequeueReusableCell(
withIdentifier: CellReuseIdentifiers.basicCell.rawValue,
for: indexPath
)
if let cell = cell as? SettingsCell {
cell.titleLabel.text = item.title
cell.disclosureType = item.isAdded ? .tick : .none
}
return cell
}
// MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let item = snapshot.itemForIndexPath(indexPath) else { return }
delegate?.shortcutsDataSource(self, didSelectItem: item)
tableView.deselectRow(at: indexPath, animated: true)
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return tableView.dequeueReusableHeaderFooterView(
withIdentifier: HeaderFooterReuseIdentifier.spacer.rawValue
)
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UIMetrics.sectionSpacing
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0
}
// MARK: - ShortcutsManagerDelegate
func shortcutsManager(
_ shortcutsManager: ShortcutsManager,
didReceiveVoiceShortcuts voiceShortcuts: [INVoiceShortcut]
) {
updateDataSnapshot(voiceShortcuts: voiceShortcuts)
tableView?.reloadData()
}
}
private extension ShortcutsDataSource {
enum ShortcutData: CaseIterable {
case start
case reconnect
case stop
var title: String {
switch self {
case .start:
return NSLocalizedString(
"SHORTCUTS_NAME_START_VPN",
tableName: "Shortcuts",
value: "Start VPN",
comment: ""
)
case .reconnect:
return NSLocalizedString(
"SHORTCUTS_NAME_RECONNECT_VPN",
tableName: "Shortcuts",
value: "Reconnect VPN",
comment: ""
)
case .stop:
return NSLocalizedString(
"SHORTCUTS_NAME_STOP_VPN",
tableName: "Shortcuts",
value: "Stop VPN",
comment: ""
)
}
}
var shortcut: INShortcut? {
let intent: INIntent
switch self {
case .start:
intent = StartVPNIntent()
case .reconnect:
intent = ReconnectVPNIntent()
case .stop:
intent = StopVPNIntent()
}
intent.suggestedInvocationPhrase = title
guard let shortcut = INShortcut(intent: intent) else {
assertionFailure("The shortcut has an invalid intent.")
return nil
}
return shortcut
}
}
}
|