-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathConfigureTabsViewController.swift
349 lines (277 loc) · 11.4 KB
/
ConfigureTabsViewController.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
//
// ConfigureTabsViewController.swift
// Strongbox
//
// Created by Strongbox on 26/12/2022.
// Copyright © 2022 Mark McGuill. All rights reserved.
//
import Foundation
extension Notification.Name {
enum ConfigureTabs {
static let tabsChanged = Notification.Name("VisibleTabsConfigurationChanged")
}
}
class ConfigureTabsViewController: UITableViewController, UIAdaptivePresentationControllerDelegate {
let allTabs: [BrowseViewType] = [
.home,
.favourites,
.tags,
.hierarchy,
.list,
.totpList,
.passkeys,
.sshKeys,
.attachments,
.expiredAndExpiring,
.auditIssues,
.watchEntries,
]
var visibleTabs: [BrowseViewType] = []
var hiddenTabs: [BrowseViewType] = []
@objc var model: Model!
@objc
class func fromStoryboard(model: Model) -> UINavigationController {
let storyboard = UIStoryboard(name: "ConfigureTabs", bundle: nil)
let nav = storyboard.instantiateInitialViewController() as! UINavigationController
let me = nav.topViewController as! Self
me.model = model
return nav
}
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.presentationController?.delegate = self
let numeric = model.metadata.visibleTabs
visibleTabs = numeric.compactMap { num in
BrowseViewType(rawValue: num.uintValue)
}
hiddenTabs = Set(allTabs).subtracting(Set(visibleTabs)).sorted(by: { v1, v2 in
v1.rawValue > v2.rawValue
})
tableView.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setEditing(true, animated: false)
}
override func tableView(_: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
(indexPath.section == 1 && hiddenTabs.count > 0) || (indexPath.section == 0 && visibleTabs.count > 1)
}
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
if sourceIndexPath.section == destinationIndexPath.section, sourceIndexPath.row == destinationIndexPath.row {
return
}
if sourceIndexPath.section == 0, destinationIndexPath.section == 1, visibleTabs.count < 2 {
return
}
if sourceIndexPath.section > 1 {
return
}
let tab: BrowseViewType
if sourceIndexPath.section == 0 {
tab = visibleTabs.remove(at: sourceIndexPath.row)
} else {
tab = hiddenTabs.remove(at: sourceIndexPath.row)
}
if destinationIndexPath.section == 0 {
visibleTabs.insert(tab, at: destinationIndexPath.row)
} else {
if hiddenTabs.count == 0 {
hiddenTabs.insert(tab, at: 0)
} else {
hiddenTabs.insert(tab, at: destinationIndexPath.row)
}
}
tableView.reloadData()
}
override func tableView(_: UITableView, editingStyleForRowAt _: IndexPath) -> UITableViewCell.EditingStyle {
.none
}
override func tableView(_: UITableView, shouldIndentWhileEditingRowAt _: IndexPath) -> Bool {
false
}
override func numberOfSections(in _: UITableView) -> Int {
3
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 2, visibleTabs.count > 1 {
return 0.0
}
return super.tableView(tableView, heightForRowAt: indexPath)
}
override func tableView(_: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return NSLocalizedString("generic_visible", comment: "Visible")
} else if section == 1 {
return NSLocalizedString("generic_item_is_hidden", comment: "Hidden")
} else {
return visibleTabs.count == 1 ? NSLocalizedString("generic_settings", comment: "Settings") : nil
}
}
override func tableView(_: UITableView, titleForFooterInSection section: Int) -> String? {
if section == 0 {
return NSLocalizedString("configure_tabs_footer_visible_tabs", comment: "These tabs will be visible in this order, providing there are eligible items to display. You must always have at least one visible tab.")
} else if section == 1 {
return NSLocalizedString("configure_tabs_footer_hidden_tabs", comment: "These tabs will remain hidden.")
} else {
return visibleTabs.count == 1 ? NSLocalizedString("configure_tabs_footer_text_hide_tab_bar", comment: "You can choose to completely hide the bottom tab bar since you have only a single visible tab. Tap to toggle.") : nil
}
}
override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return visibleTabs.count
} else if section == 1 {
return hiddenTabs.count == 0 ? 1 : hiddenTabs.count
} else {
return 1
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ConfigureTabsCellIdentifier", for: indexPath)
if indexPath.section == 0 || indexPath.section == 1 {
cell.selectionStyle = .none
cell.accessoryType = .none
cell.editingAccessoryType = .none
var viewType: BrowseViewType? = nil
if indexPath.section == 0 {
viewType = visibleTabs[indexPath.row]
} else if indexPath.section == 1 {
if hiddenTabs.count != 0 {
viewType = hiddenTabs[indexPath.row]
}
}
if let viewType {
cell.textLabel?.text = titleForViewType(viewType: viewType)
cell.imageView?.image = imageForViewType(viewType: viewType)
cell.imageView?.preferredSymbolConfiguration = UIImage.SymbolConfiguration(scale: .large)
cell.imageView?.tintColor = imageTintForViewType(viewType: viewType)
} else {
cell.textLabel?.text = NSLocalizedString("configure_tabs_drag_tabs_to_hide", comment: "Drag tabs here to hide them")
cell.textLabel?.textColor = .secondaryLabel
cell.tintColor = .secondaryLabel
}
return cell
} else {
cell.textLabel?.text = NSLocalizedString("configure_tabs_option_hide_tab_bar_completely", comment: "Hide Tab Bar Completely")
cell.textLabel?.textColor = .label
cell.imageView?.image = nil
cell.accessoryType = model.metadata.hideTabBarIfOnlySingleTab ? .checkmark : .none
cell.editingAccessoryType = model.metadata.hideTabBarIfOnlySingleTab ? .checkmark : .none
cell.selectionStyle = .default
return cell
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 2 {
model.metadata.hideTabBarIfOnlySingleTab = !model.metadata.hideTabBarIfOnlySingleTab
}
tableView.deselectRow(at: indexPath, animated: true)
tableView.reloadSections([2], with: .automatic)
}
func titleForViewType(viewType: BrowseViewType) -> String {
switch viewType {
case .tags:
return NSLocalizedString("browse_prefs_item_subtitle_tags", comment: "Tags")
case .hierarchy:
return NSLocalizedString("browse_prefs_view_as_folders", comment: "Groups")
case .list:
return NSLocalizedString("browse_prefs_view_as_flat_list", comment: "Entries")
case .totpList:
return NSLocalizedString("quick_view_title_totp_entries_title", comment: "2FA Codes")
case .favourites:
return NSLocalizedString("browse_vc_section_title_pinned", comment: "Favourites")
case .home:
return NSLocalizedString("navigation_tab_home", comment: "Favourites")
case .passkeys:
return NSLocalizedString("generic_noun_plural_passkeys", comment: "Passkeys")
case .sshKeys:
return NSLocalizedString("sidebar_quick_view_keeagent_ssh_keys_title", comment: "SSH Keys")
case .attachments:
return NSLocalizedString("item_details_section_header_attachments", comment: "Attachments")
case .expiredAndExpiring:
return NSLocalizedString("quick_view_title_expired_and_expiring", comment: "Expired & Expiring")
case .auditIssues:
return NSLocalizedString("browse_vc_action_audit", comment: "Audit")
case .watchEntries:
return NSLocalizedString("sidebar_watch_entries", comment: "Watch Entries")
@unknown default:
return "🔴 UNKNOWN"
}
}
func imageTintForViewType(viewType: BrowseViewType) -> UIColor? {
switch viewType {
case .favourites:
return UIColor.systemYellow
case .hierarchy:
return UIColor.systemBlue
case .list:
return UIColor.systemBlue
case .totpList:
return UIColor.systemIndigo
case .tags:
return UIColor.systemBlue
case .home:
return UIColor.systemBlue
case .passkeys:
return UIColor.systemPurple
case .sshKeys:
return UIColor.systemIndigo
case .attachments:
return UIColor.systemMint
case .expiredAndExpiring:
return UIColor.systemCyan
case .auditIssues:
return UIColor.systemOrange
case .watchEntries:
return UIColor.systemBlue
@unknown default:
return nil
}
}
func imageForViewType(viewType: BrowseViewType) -> UIImage {
let imageName: String
switch viewType {
case .tags:
imageName = "tag.fill"
case .hierarchy:
imageName = "folder.fill"
case .list:
imageName = "list.bullet"
case .totpList:
imageName = "timer"
case .favourites:
imageName = "star.fill"
case .home:
imageName = "house.fill"
case .passkeys:
imageName = "person.badge.key.fill"
case .sshKeys:
var img = "network"
if #available(iOS 17.0, *) {
img = "apple.terminal.fill"
}
imageName = img
case .attachments:
imageName = "doc.richtext.fill"
case .expiredAndExpiring:
imageName = "calendar"
case .auditIssues:
imageName = "checkmark.shield.fill"
case .watchEntries:
imageName = "applewatch"
@unknown default:
imageName = "questionmark.circle.fill"
}
return UIImage(systemName: imageName, withConfiguration: UIImage.SymbolConfiguration(scale: .small))!
}
@IBAction func onDone(_: Any?) {
let numeric = visibleTabs.map { tab in
NSNumber(value: tab.rawValue)
}
model.metadata.visibleTabs = numeric
NotificationCenter.default.post(name: .ConfigureTabs.tabsChanged, object: nil)
dismiss(animated: true)
}
func presentationControllerDidDismiss(_: UIPresentationController) {
onDone(nil)
}
}