This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDataImportStatusProviding.swift
99 lines (87 loc) · 3.85 KB
/
DataImportStatusProviding.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
//
// DataImportStatusProviding.swift
//
// Copyright © 2023 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import Bookmarks
import BrowserServicesKit
import PixelKit
protocol DataImportStatusProviding {
var didImport: Bool { get }
func showImportWindow(customTitle: String?, completion: (() -> Void)?)
}
final class BookmarksAndPasswordsImportStatusProvider: DataImportStatusProviding {
let secureVault: (any AutofillSecureVault)?
let bookmarkManager: BookmarkManager
init(secureVault: (any AutofillSecureVault)? = try? AutofillSecureVaultFactory.makeVault(reporter: SecureVaultReporter.shared),
bookmarkManager: BookmarkManager = LocalBookmarkManager.shared) {
self.secureVault = secureVault
self.bookmarkManager = bookmarkManager
}
@UserDefaultsWrapper(key: .homePageContinueSetUpImport, defaultValue: nil)
private var successfulImportHappened: Bool?
// The successfulImportHappened boolean covers only users who start importing after this code is live.
// To cover as much as possible users that have imported data in the past we try some hack to detect if they have imported bookmarks or passwords
var didImport: Bool {
if successfulImportHappened == nil {
successfulImportHappened = didImportBookmarks || didImportPasswords
}
return successfulImportHappened!
}
@MainActor
func showImportWindow(customTitle: String?, completion: (() -> Void)?) {
DataImportView(title: customTitle ?? UserText.importDataTitle).show(completion: completion)
}
// It only cover the case in which the user has imported bookmar AFTER already having some bookmarks
// There is no way to detect whether the user has imported bookmarks as first thing
private var didImportBookmarks: Bool {
guard let folders = bookmarkManager.list?.topLevelEntities else { return false }
for folder in folders.reversed() where folder.title.contains(UserText.bookmarkImportedFromFolder) {
return true
}
return false
}
// Checks if there are multiple passwords created at the same time which would indicate that they were created through import
private var didImportPasswords: Bool {
guard let secureVault else {
return false
}
var dates: [Date] = []
do {
let accountsDates = try secureVault.accounts().map(\.created)
dates.append(contentsOf: accountsDates)
let noteDates = try secureVault.notes().map(\.created)
dates.append(contentsOf: noteDates)
let cardDates = try secureVault.creditCards().map(\.created)
dates.append(contentsOf: cardDates)
let identitiesDates = try secureVault.identities().map(\.created)
dates.append(contentsOf: identitiesDates)
} catch {
PixelKit.fire(DebugEvent(GeneralPixel.secureVaultError(error: error)))
}
guard dates.count >= 2 else {
return false
}
let sortedDates = dates.sorted()
for ind in 1..<sortedDates.count {
let sameSecond = Calendar.current.isDate(sortedDates[ind], equalTo: sortedDates[ind - 1], toGranularity: .second)
if sameSecond {
return true
}
}
return false
}
}