-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathDatabaseHomeViewModel.swift
559 lines (435 loc) · 15.4 KB
/
DatabaseHomeViewModel.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
//
// DatabaseHomeViewModel.swift
// Strongbox
//
// Created by Strongbox on 27/07/2024.
// Copyright © 2024 Mark McGuill. All rights reserved.
//
import Foundation
import SwiftUI
enum DatabaseNavigationDestination {
case allEntries
case groups
case totps
case favourites
case auditIssues
case sshKeys
case passkeys
case attachments
case expiredAndExpiring
case tags(tag: String?)
case entryDetail(uuid: UUID)
case recycleBin
case watchEntries
}
@objc
enum HomeViewSection: Int, CaseIterable, Identifiable {
case favourites
case navigation
case quickTags
case otherViews
var id: Self { self }
var title: LocalizedStringKey {
switch self {
case .favourites:
return "browse_vc_section_title_pinned"
case .navigation:
return "generic_noun_navigation"
case .otherViews:
return "quick_view_section_title_quick_views"
case .quickTags:
return "home_quick_tags_section_header"
}
}
var imageName: String {
switch self {
case .favourites:
return "star"
case .navigation:
return "location"
case .otherViews:
return "scope"
case .quickTags:
return "tag"
}
}
}
protocol DatabaseActionsInterface {
var hasDoneDatabaseOnLaunchTasks: Bool { get set }
var syncStatus: SyncStatus { get }
var isRunningAsyncUpdate: Bool { get }
var lastAsyncUpdateResult: AsyncJobResult? { get }
var tabBarControllerIsHidden: Bool { get }
var biometricsIsFaceId: Bool { get }
var watchIsPairedAndInstalled: Bool { get }
func close()
func navigateTo(destination: DatabaseNavigationDestination, homeModel: DatabaseHomeViewModel)
func onPulldownToRefresh() async
func interactiveSyncAppleWatch(_ withInteractiveGuide: Bool, allowUserToOptOut: Bool)
func updateAndQueueSync() async -> Bool
func copyPassword(entry: any SwiftEntryModelInterface)
func copyAllFields(entry: any SwiftEntryModelInterface)
func copyUsername(entry: any SwiftEntryModelInterface)
func copyTotp(entry: any SwiftEntryModelInterface)
func copyUrl(entry: any SwiftEntryModelInterface)
func copyEmail(entry: any SwiftEntryModelInterface)
func copyNotes(entry: any SwiftEntryModelInterface)
func copyAndLaunch(entry: any SwiftEntryModelInterface)
func copyCustomField(key: String, entry: any SwiftEntryModelInterface)
func showPassword(entry: any SwiftEntryModelInterface)
func showAuditDrillDown(entry: any SwiftEntryModelInterface)
func deleteItem(item: any SwiftItemModelInterface) async -> Bool
func emptyRecycleBin() async
func presentConfigureTabsView()
func presentCustomizeView()
func presentConvenienceUnlockPreferences()
func presentAutoFillSettings()
func presentAuditSettings()
func presentAutoLockSettings()
func presentAdvancedSettings()
func presentHardwareKeySettings()
func presentEncryptionSettings()
func presentSetMasterCredentials()
func onAddEntry()
func exportDatabase()
func printDatabase()
}
struct DummyDatabaseActionsInterface: DatabaseActionsInterface {
var hasDoneDatabaseOnLaunchTasks: Bool = false
var syncStatus: SyncStatus = .init(databaseId: UUID().uuidString)
var isRunningAsyncUpdate: Bool = false
var lastAsyncUpdateResult: AsyncJobResult? = nil
func interactiveSyncAppleWatch(_: Bool, allowUserToOptOut _: Bool) {
swlog("DummyDatabaseActionsInterface::interactiveSyncAppleWatch() called")
}
func presentHardwareKeySettings() {
swlog("DummyDatabaseActionsInterface::presentHardwareKeySettings() called")
}
func onAddEntry() {
swlog("DummyDatabaseActionsInterface::onAddEntry() called")
}
func exportDatabase() {
swlog("DummyDatabaseActionsInterface::exportDatabase() called")
}
func printDatabase() {
swlog("DummyDatabaseActionsInterface::printDatabase() called")
}
func presentSetMasterCredentials() {
swlog("DummyDatabaseActionsInterface::presentSetMasterCredentials() called")
}
func presentEncryptionSettings() {
swlog("DummyDatabaseActionsInterface::func presentEncryptionSettings() called")
}
func presentAutoFillSettings() {
swlog("DummyDatabaseActionsInterface::presentAutoFillSettings() called")
}
func presentAuditSettings() {
swlog("DummyDatabaseActionsInterface::presentAuditSettings() called")
}
func presentAutoLockSettings() {
swlog("DummyDatabaseActionsInterface::presentAutoLockSettings() called")
}
func presentAdvancedSettings() {
swlog("DummyDatabaseActionsInterface::presentAdvancedSettings() called")
}
func presentConvenienceUnlockPreferences() {
swlog("DummyDatabaseActionsInterface::presentConvenienceUnlockPreferences() called")
}
func presentConfigureTabsView() {
swlog("DummyDatabaseActionsInterface::presentConfigureTabsView() called")
}
func presentCustomizeView() {
swlog("DummyDatabaseActionsInterface::presentCustomizeView() called")
}
var tabBarControllerIsHidden: Bool = true
var biometricsIsFaceId: Bool = true
var watchIsPairedAndInstalled: Bool = true
func emptyRecycleBin() async {
swlog("DummyDatabaseActionsInterface::emptyRecycleBin() called")
}
func deleteItem(item _: any SwiftItemModelInterface) async -> Bool {
swlog("DummyDatabaseActionsInterface::deleteItem() called")
return true
}
func copyAllFields(entry _: any SwiftEntryModelInterface) {
swlog("DummyDatabaseActionsInterface::copyAllFields() called")
}
func copyUsername(entry _: any SwiftEntryModelInterface) {
swlog("DummyDatabaseActionsInterface::copyUsername() called")
}
func copyTotp(entry _: any SwiftEntryModelInterface) {
swlog("DummyDatabaseActionsInterface::copyTotp() called")
}
func copyUrl(entry _: any SwiftEntryModelInterface) {
swlog("DummyDatabaseActionsInterface::copyUrl() called")
}
func copyEmail(entry _: any SwiftEntryModelInterface) {
swlog("DummyDatabaseActionsInterface::copyEmail() called")
}
func copyNotes(entry _: any SwiftEntryModelInterface) {
swlog("DummyDatabaseActionsInterface::copyNotes() called")
}
func copyAndLaunch(entry _: any SwiftEntryModelInterface) {
swlog("DummyDatabaseActionsInterface::copyAndLaunch() called")
}
func copyCustomField(key _: String, entry _: any SwiftEntryModelInterface) {
swlog("DummyDatabaseActionsInterface::copyCustomField() called")
}
func showAuditDrillDown(entry _: any SwiftEntryModelInterface) {
swlog("DummyDatabaseActionsInterface::showAuditDrillDown() called")
}
func showPassword(entry _: any SwiftEntryModelInterface) {
swlog("DummyDatabaseActionsInterface::showPassword() called")
}
func copyPassword(entry _: any SwiftEntryModelInterface) {
swlog("DummyDatabaseActionsInterface::copyPassword() called")
}
func close() {}
func navigateTo(destination: DatabaseNavigationDestination, homeModel _: DatabaseHomeViewModel) {
swlog("DummyDatabaseActionsInterface::navigateTo() called with \(destination)")
}
func onPulldownToRefresh() async {
swlog("DummyDatabaseActionsInterface::onPulldownToRefresh() called")
}
func updateAndQueueSync() async -> Bool {
swlog("DummyDatabaseActionsInterface::updateAndQueueSync() called")
return true
}
}
class DatabaseHomeViewModel: ObservableObject {
@Published
var database: SwiftDatabaseModelInterface
var actions: DatabaseActionsInterface
init(database: SwiftDatabaseModelInterface = SwiftDummyDatabaseModel.testModel,
externalWorldAdaptor: DatabaseActionsInterface = DummyDatabaseActionsInterface())
{
self.database = database
actions = externalWorldAdaptor
}
var isItemsCanBeExported: Bool { database.isItemsCanBeExported }
var entryCount: Int { database.entryCount }
var groupCount: Int { database.groupCount }
func search(searchText: String, searchScope: SearchScope) -> [any SwiftEntryModelInterface] {
database.search(searchText: searchText, searchScope: searchScope)
}
func close() {
actions.close()
}
func navigateTo(destination: DatabaseNavigationDestination) {
actions.navigateTo(destination: destination, homeModel: self)
}
func onPulldownToRefresh() async {
await actions.onPulldownToRefresh()
}
func toggleFavourite(entry: any SwiftEntryModelInterface) {
let needsSave = entry.toggleFavourite()
if needsSave {
Task {
await actions.updateAndQueueSync()
}
}
}
func toggleAppleWatch(entry: any SwiftEntryModelInterface) {
let needsSave = entry.toggleAppleWatch()
if needsSave {
Task {
await actions.updateAndQueueSync()
}
}
actions.interactiveSyncAppleWatch(false, allowUserToOptOut: false)
}
func syncAppleWatchNow() {
actions.interactiveSyncAppleWatch(true, allowUserToOptOut: false)
}
func copyPassword(entry: any SwiftEntryModelInterface) {
actions.copyPassword(entry: entry)
}
func copyAllFields(entry: any SwiftEntryModelInterface) {
actions.copyAllFields(entry: entry)
}
func copyUsername(entry: any SwiftEntryModelInterface) {
actions.copyUsername(entry: entry)
}
func copyTotp(entry: any SwiftEntryModelInterface) {
actions.copyTotp(entry: entry)
}
func copyUrl(entry: any SwiftEntryModelInterface) {
actions.copyUrl(entry: entry)
}
func copyEmail(entry: any SwiftEntryModelInterface) {
actions.copyEmail(entry: entry)
}
func copyNotes(entry: any SwiftEntryModelInterface) {
actions.copyNotes(entry: entry)
}
func copyAndLaunch(entry: any SwiftEntryModelInterface) {
actions.copyAndLaunch(entry: entry)
}
func copyCustomField(key: String, entry: any SwiftEntryModelInterface) {
actions.copyCustomField(key: key, entry: entry)
}
func showPassword(entry: any SwiftEntryModelInterface) {
actions.showPassword(entry: entry)
}
func showAuditDrillDown(entry: any SwiftEntryModelInterface) {
actions.showAuditDrillDown(entry: entry)
}
func canRecycle(item: any SwiftItemModelInterface) -> Bool {
database.canRecycle(item: item)
}
func deleteItem(item: any SwiftItemModelInterface) async -> Bool {
await actions.deleteItem(item: item)
}
var expireCount: Int {
database.expiredEntryCount + database.expiringEntryCount
}
var showOtherViews: Bool {
let total = database.tagCount +
(auditModel.isEnabled ? 1 : 0) +
database.passkeyEntryCount +
database.sshKeyEntryCount +
database.attachmentsEntryCount +
expireCount +
database.recycleBinCount
return total > 0 && database.isHomeViewSectionVisible(section: .otherViews)
}
var showNavigationSection: Bool {
let total = entryCount +
database.favourites.count +
database.totpCodeEntries.count +
database.groupCount
return total > 0 && database.isHomeViewSectionVisible(section: .navigation)
}
var showFavouritesSection: Bool {
!database.favourites.isEmpty && database.isHomeViewSectionVisible(section: .favourites)
}
var showQuickTagsSection: Bool {
database.tagCount > 0 && database.isHomeViewSectionVisible(section: .quickTags)
}
var allHomeSectionsInvisible: Bool {
let vis = HomeViewSection.allCases.first { isHomeViewSectionVisible(section: $0) }
return vis == nil
}
var showEmptyDatabaseView: Bool {
!showFavouritesSection && !showNavigationSection && !showQuickTagsSection && !showOtherViews
}
var auditModel: AuditViewModel {
database.auditModel
}
var startWithSearch: Bool {
get {
database.startWithSearch
}
set {
database.startWithSearch = newValue
}
}
var hasDoneDatabaseOnLaunchTasks: Bool {
get {
actions.hasDoneDatabaseOnLaunchTasks
}
set {
actions.hasDoneDatabaseOnLaunchTasks = newValue
}
}
var tabBarControllerIsHidden: Bool {
actions.tabBarControllerIsHidden
}
var biometricsIsFaceId: Bool {
actions.biometricsIsFaceId
}
func presentConfigureTabsView() {
actions.presentConfigureTabsView()
}
func presentCustomizeView() {
actions.presentCustomizeView()
}
func presentConvenienceUnlockPreferences() {
actions.presentConvenienceUnlockPreferences()
}
func presentAutoFillSettings() {
actions.presentAutoFillSettings()
}
func presentAuditSettings() {
actions.presentAuditSettings()
}
func presentAutoLockSettings() {
actions.presentAutoLockSettings()
}
func presentAdvancedSettings() {
actions.presentAdvancedSettings()
}
func presentHardwareKeySettings() {
actions.presentHardwareKeySettings()
}
func presentEncryptionSettings() {
actions.presentEncryptionSettings()
}
func presentSetMasterCredentials() {
actions.presentSetMasterCredentials()
}
var disableExport: Bool {
database.disableExport
}
var disablePrinting: Bool {
database.disablePrinting
}
func onAddEntry() {
actions.onAddEntry()
}
func exportDatabase() {
actions.exportDatabase()
}
func printDatabase() {
actions.printDatabase()
}
var syncStatus: SyncStatus? {
actions.syncStatus
}
var isRunningAsyncUpdate: Bool {
actions.isRunningAsyncUpdate
}
var lastAsyncUpdateResult: AsyncJobResult? {
actions.lastAsyncUpdateResult
}
var showIcons: Bool {
database.showIcons
}
var twoFactorShowSeparator: Bool {
database.twoFactorShowSeparator
}
var title: String {
database.nickName
}
var subtitle: String {
var status: [String] = []
if database.isReadOnly {
status.append(NSLocalizedString("databases_toggle_read_only_context_menu", comment: "Read-Only"))
}
if database.isInOfflineMode {
status.append(NSLocalizedString("browse_vc_pulldown_refresh_offline_title", comment: "Offline Mode"))
}
return status.count > 1 ? String(format: "(%@)", status.joined(separator: ", ")) : status.joined(separator: ", ")
}
func isHomeViewSectionVisible(section: HomeViewSection) -> Bool {
database.isHomeViewSectionVisible(section: section)
}
func setHomeViewSectionVisible(section: HomeViewSection, visible: Bool) {
database.setHomeViewSectionVisible(section: section, visible: visible)
}
var recycleBinGroup: (any SwiftGroupModelInterface)? {
database.recycleBinGroup
}
func emptyRecycleBin() async {
await actions.emptyRecycleBin()
}
var shouldShowYubiKeySettingsOption: Bool {
database.format == .keePass4 && database.ckfs.yubiKeyCR != nil
}
var appleWatchEnabled: Bool {
database.appleWatchEnabled
}
var watchIsPairedAndInstalled: Bool {
actions.watchIsPairedAndInstalled
}
}