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 pathHomePageContinueSetUpModel.swift
481 lines (408 loc) · 18.1 KB
/
HomePageContinueSetUpModel.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
//
// HomePageContinueSetUpModel.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 AppKit
import BrowserServicesKit
import Combine
import Common
import Foundation
import PixelKit
import Subscription
import FeatureFlags
import NetworkProtection
import NetworkProtectionUI
protocol ContinueSetUpModelTabOpening {
@MainActor
func openTab(_ tab: Tab)
}
struct TabCollectionViewModelTabOpener: ContinueSetUpModelTabOpening {
let tabCollectionViewModel: TabCollectionViewModel
@MainActor
func openTab(_ tab: Tab) {
tabCollectionViewModel.insertOrAppend(tab: tab, selected: true)
}
}
extension HomePage.Models {
static let newHomePageTabOpen = Notification.Name("newHomePageAppOpen")
final class ContinueSetUpModel: ObservableObject {
let itemWidth = FeaturesGridDimensions.itemWidth
let itemHeight = FeaturesGridDimensions.itemHeight
let horizontalSpacing = FeaturesGridDimensions.horizontalSpacing
let verticalSpacing = FeaturesGridDimensions.verticalSpacing
let itemsPerRow = HomePage.featuresPerRow
let itemsRowCountWhenCollapsed = HomePage.featureRowCountWhenCollapsed
let gridWidth = FeaturesGridDimensions.width
let deleteActionTitle = UserText.newTabSetUpRemoveItemAction
let privacyConfigurationManager: PrivacyConfigurationManaging
var duckPlayerURL: String {
let duckPlayerSettings = privacyConfigurationManager.privacyConfig.settings(for: .duckPlayer)
return duckPlayerSettings["tryDuckPlayerLink"] as? String ?? "https://www.youtube.com/watch?v=yKWIA-Pys4c"
}
private let defaultBrowserProvider: DefaultBrowserProvider
private let dockCustomizer: DockCustomization
private let dataImportProvider: DataImportStatusProviding
private let tabOpener: ContinueSetUpModelTabOpening
private let emailManager: EmailManager
private let duckPlayerPreferences: DuckPlayerPreferencesPersistor
private let subscriptionManager: SubscriptionManager
@UserDefaultsWrapper(key: .homePageShowAllFeatures, defaultValue: false)
var shouldShowAllFeatures: Bool {
didSet {
updateVisibleMatrix()
shouldShowAllFeaturesSubject.send(shouldShowAllFeatures)
}
}
private var cancellables: Set<AnyCancellable> = []
let shouldShowAllFeaturesPublisher: AnyPublisher<Bool, Never>
private let shouldShowAllFeaturesSubject = PassthroughSubject<Bool, Never>()
struct Settings {
@UserDefaultsWrapper(key: .homePageShowMakeDefault, defaultValue: true)
var shouldShowMakeDefaultSetting: Bool
@UserDefaultsWrapper(key: .homePageShowAddToDock, defaultValue: true)
var shouldShowAddToDockSetting: Bool
@UserDefaultsWrapper(key: .homePageShowImport, defaultValue: true)
var shouldShowImportSetting: Bool
@UserDefaultsWrapper(key: .homePageShowDuckPlayer, defaultValue: true)
var shouldShowDuckPlayerSetting: Bool
@UserDefaultsWrapper(key: .homePageShowEmailProtection, defaultValue: true)
var shouldShowEmailProtectionSetting: Bool
@UserDefaultsWrapper(key: .homePageIsFirstSession, defaultValue: true)
var isFirstSession: Bool
func clear() {
_shouldShowMakeDefaultSetting.clear()
_shouldShowAddToDockSetting.clear()
_shouldShowImportSetting.clear()
_shouldShowDuckPlayerSetting.clear()
_shouldShowEmailProtectionSetting.clear()
_isFirstSession.clear()
}
}
private let settings: Settings
var isMoreOrLessButtonNeeded: Bool {
return featuresMatrix.count > itemsRowCountWhenCollapsed
}
var hasContent: Bool {
return !featuresMatrix.isEmpty
}
lazy var listOfFeatures = settings.isFirstSession ? firstRunFeatures : randomisedFeatures
@Published var featuresMatrix: [[FeatureType]] = [[]] {
didSet {
updateVisibleMatrix()
}
}
@Published var visibleFeaturesMatrix: [[FeatureType]] = [[]]
init(defaultBrowserProvider: DefaultBrowserProvider = SystemDefaultBrowserProvider(),
dockCustomizer: DockCustomization = DockCustomizer(),
dataImportProvider: DataImportStatusProviding = BookmarksAndPasswordsImportStatusProvider(),
tabOpener: ContinueSetUpModelTabOpening,
emailManager: EmailManager = EmailManager(),
duckPlayerPreferences: DuckPlayerPreferencesPersistor = DuckPlayerPreferencesUserDefaultsPersistor(),
privacyConfigurationManager: PrivacyConfigurationManaging = AppPrivacyFeatures.shared.contentBlocking.privacyConfigurationManager,
subscriptionManager: SubscriptionManager = Application.appDelegate.subscriptionManager) {
self.defaultBrowserProvider = defaultBrowserProvider
self.dockCustomizer = dockCustomizer
self.dataImportProvider = dataImportProvider
self.tabOpener = tabOpener
self.emailManager = emailManager
self.duckPlayerPreferences = duckPlayerPreferences
self.privacyConfigurationManager = privacyConfigurationManager
self.subscriptionManager = subscriptionManager
self.settings = .init()
shouldShowAllFeaturesPublisher = shouldShowAllFeaturesSubject.removeDuplicates().eraseToAnyPublisher()
refreshFeaturesMatrix()
NotificationCenter.default.addObserver(self, selector: #selector(newTabOpenNotification(_:)), name: HomePage.Models.newHomePageTabOpen, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(windowDidBecomeKey(_:)), name: NSWindow.didBecomeKeyNotification, object: nil)
// HTML NTP doesn't refresh on appear so we have to connect to the appear signal
// (the notification in this case) to trigger a refresh.
NotificationCenter.default.addObserver(self, selector: #selector(refreshFeaturesForHTMLNewTabPage(_:)), name: .newTabPageWebViewDidAppear, object: nil)
// This is just temporarily here to run an A/A test to check the new experiment framework works as expected
guard let cohort = Application.appDelegate.featureFlagger.resolveCohort(for: FeatureFlag.testExperiment) as? FeatureFlag.TestExperimentCohort else { return }
switch cohort {
case .control:
print("COHORT A")
case .treatment:
print("COHORT B")
}
subscribeToTestExperimentFeatureFlagChanges()
}
private func subscribeToTestExperimentFeatureFlagChanges() {
guard let overridesHandler = Application.appDelegate.featureFlagger.localOverrides?.actionHandler as? FeatureFlagOverridesPublishingHandler<FeatureFlag> else {
return
}
overridesHandler.experimentFlagDidChangePublisher
.filter { $0.0 == .testExperiment }
.sink { (_, cohort) in
guard let newCohort = FeatureFlag.TestExperimentCohort.cohort(for: cohort) else { return }
switch newCohort {
case .control:
print("COHORT A")
case .treatment:
print("COHORT B")
}
}
.store(in: &cancellables)
}
@MainActor func performAction(for featureType: FeatureType) {
switch featureType {
case .defaultBrowser:
performDefaultBrowserAction()
case .dock:
performDockAction()
case .importBookmarksAndPasswords:
performImportBookmarksAndPasswordsAction()
case .duckplayer:
performDuckPlayerAction()
case .emailProtection:
performEmailProtectionAction()
}
}
private func performDefaultBrowserAction() {
do {
PixelKit.fire(GeneralPixel.defaultRequestedFromHomepageSetupView)
try defaultBrowserProvider.presentDefaultBrowserPrompt()
} catch {
defaultBrowserProvider.openSystemPreferences()
}
}
private func performImportBookmarksAndPasswordsAction() {
dataImportProvider.showImportWindow(customTitle: nil, completion: { self.refreshFeaturesMatrix() })
}
@MainActor
private func performDuckPlayerAction() {
if let videoUrl = URL(string: duckPlayerURL) {
let tab = Tab(content: .url(videoUrl, source: .link), shouldLoadInBackground: true)
tabOpener.openTab(tab)
}
}
@MainActor
private func performEmailProtectionAction() {
let tab = Tab(content: .url(EmailUrls().emailProtectionLink, source: .ui), shouldLoadInBackground: true)
tabOpener.openTab(tab)
}
func performDockAction() {
PixelKit.fire(GeneralPixel.userAddedToDockFromNewTabPageCard,
includeAppVersionParameter: false)
dockCustomizer.addToDock()
}
func removeItem(for featureType: FeatureType) {
switch featureType {
case .defaultBrowser:
settings.shouldShowMakeDefaultSetting = false
case .dock:
settings.shouldShowAddToDockSetting = false
case .importBookmarksAndPasswords:
settings.shouldShowImportSetting = false
case .duckplayer:
settings.shouldShowDuckPlayerSetting = false
case .emailProtection:
settings.shouldShowEmailProtectionSetting = false
}
refreshFeaturesMatrix()
}
func refreshFeaturesMatrix() {
var features: [FeatureType] = []
appendFeatureCards(&features)
if features.isEmpty {
AppearancePreferences.shared.continueSetUpCardsClosed = true
}
featuresMatrix = features.chunked(into: itemsPerRow)
}
private func appendFeatureCards(_ features: inout [FeatureType]) {
for feature in listOfFeatures where shouldAppendFeature(feature: feature) {
features.append(feature)
}
}
private func shouldAppendFeature(feature: FeatureType) -> Bool {
switch feature {
case .defaultBrowser:
return shouldMakeDefaultCardBeVisible
case .importBookmarksAndPasswords:
return shouldImportCardBeVisible
case .dock:
return shouldDockCardBeVisible
case .duckplayer:
return shouldDuckPlayerCardBeVisible
case .emailProtection:
return shouldEmailProtectionCardBeVisible
}
}
// Helper Functions
@MainActor
@objc private func newTabOpenNotification(_ notification: Notification) {
if !settings.isFirstSession {
listOfFeatures = randomisedFeatures
}
#if DEBUG
settings.isFirstSession = false
#endif
if OnboardingViewModel.isOnboardingFinished {
settings.isFirstSession = false
}
}
@objc private func windowDidBecomeKey(_ notification: Notification) {
// Async dispatch allows default browser setting to propagate
// after being changed in the system dialog
DispatchQueue.main.async {
self.refreshFeaturesMatrix()
}
}
@objc private func refreshFeaturesForHTMLNewTabPage(_ notification: Notification) {
refreshFeaturesMatrix()
}
var randomisedFeatures: [FeatureType] {
var features: [FeatureType] = [.defaultBrowser]
var shuffledFeatures = FeatureType.allCases.filter { $0 != .defaultBrowser }
shuffledFeatures.shuffle()
features.append(contentsOf: shuffledFeatures)
return features
}
var firstRunFeatures: [FeatureType] {
var features = FeatureType.allCases.filter { $0 != .duckplayer }
features.insert(.duckplayer, at: 0)
return features
}
private func updateVisibleMatrix() {
guard !featuresMatrix.isEmpty else {
visibleFeaturesMatrix = [[]]
return
}
visibleFeaturesMatrix = shouldShowAllFeatures ? featuresMatrix : [featuresMatrix[0]]
}
private var shouldMakeDefaultCardBeVisible: Bool {
settings.shouldShowMakeDefaultSetting && !defaultBrowserProvider.isDefault
}
private var shouldDockCardBeVisible: Bool {
#if !APPSTORE
settings.shouldShowAddToDockSetting && !dockCustomizer.isAddedToDock
#else
return false
#endif
}
private var shouldImportCardBeVisible: Bool {
settings.shouldShowImportSetting && !dataImportProvider.didImport
}
private var shouldDuckPlayerCardBeVisible: Bool {
settings.shouldShowDuckPlayerSetting && duckPlayerPreferences.duckPlayerModeBool == nil && !duckPlayerPreferences.youtubeOverlayAnyButtonPressed
}
private var shouldEmailProtectionCardBeVisible: Bool {
settings.shouldShowEmailProtectionSetting && !emailManager.isSignedIn
}
}
// MARK: Feature Type
enum FeatureType: CaseIterable, Equatable, Hashable {
// CaseIterable doesn't work with enums that have associated values, so we have to implement it manually.
// We ignore the `networkProtectionRemoteMessage` case here to avoid it getting accidentally included - it has special handling and will get
// included elsewhere.
static var allCases: [HomePage.Models.FeatureType] {
#if APPSTORE
[.duckplayer, .emailProtection, .defaultBrowser, .importBookmarksAndPasswords]
#else
[.duckplayer, .emailProtection, .defaultBrowser, .dock, .importBookmarksAndPasswords]
#endif
}
case duckplayer
case emailProtection
case defaultBrowser
case dock
case importBookmarksAndPasswords
var title: String {
switch self {
case .defaultBrowser:
return UserText.newTabSetUpDefaultBrowserCardTitle
case .dock:
return UserText.newTabSetUpDockCardTitle
case .importBookmarksAndPasswords:
return UserText.newTabSetUpImportCardTitle
case .duckplayer:
return UserText.newTabSetUpDuckPlayerCardTitle
case .emailProtection:
return UserText.newTabSetUpEmailProtectionCardTitle
}
}
var summary: String {
switch self {
case .defaultBrowser:
return UserText.newTabSetUpDefaultBrowserSummary
case .dock:
return UserText.newTabSetUpDockSummary
case .importBookmarksAndPasswords:
return UserText.newTabSetUpImportSummary
case .duckplayer:
return UserText.newTabSetUpDuckPlayerSummary
case .emailProtection:
return UserText.newTabSetUpEmailProtectionSummary
}
}
var action: String {
switch self {
case .defaultBrowser:
return UserText.newTabSetUpDefaultBrowserAction
case .dock:
return UserText.newTabSetUpDockAction
case .importBookmarksAndPasswords:
return UserText.newTabSetUpImportAction
case .duckplayer:
return UserText.newTabSetUpDuckPlayerAction
case .emailProtection:
return UserText.newTabSetUpEmailProtectionAction
}
}
var confirmation: String? {
switch self {
case .dock:
return UserText.newTabSetUpDockConfirmation
default:
return nil
}
}
var icon: NSImage {
let iconSize = NSSize(width: 64, height: 48)
switch self {
case .defaultBrowser:
return .defaultApp128.resized(to: iconSize)!
case .dock:
return .dock128.resized(to: iconSize)!
case .importBookmarksAndPasswords:
return .import128.resized(to: iconSize)!
case .duckplayer:
return .cleanTube128.resized(to: iconSize)!
case .emailProtection:
return .inbox128.resized(to: iconSize)!
}
}
}
enum FeaturesGridDimensions {
static let itemWidth: CGFloat = 240
static let itemHeight: CGFloat = 160
static let verticalSpacing: CGFloat = 16
static let horizontalSpacing: CGFloat = 24
static let width: CGFloat = (itemWidth + horizontalSpacing) * CGFloat(HomePage.featuresPerRow) - horizontalSpacing
static func height(for rowCount: Int) -> CGFloat {
(itemHeight + verticalSpacing) * CGFloat(rowCount) - verticalSpacing
}
}
}
// MARK: - Remote Messaging
extension AppVersion {
public var majorAndMinorOSVersion: String {
let components = osVersion.split(separator: ".")
guard components.count >= 2 else {
return majorVersionNumber
}
return "\(components[0]).\(components[1])"
}
}