This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 441
/
BVC+ProductNotification.swift
149 lines (119 loc) · 4.93 KB
/
BVC+ProductNotification.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
// Copyright 2020 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
import Preferences
import BraveUI
import Shared
import Data
import UIKit
import Onboarding
// MARK: - ProductNotification
extension BrowserViewController {
// MARK: Internal
@objc func updateShieldNotifications() {
// Adding slight delay here for 2 reasons
// First the content Blocker stats will be updated in current tab
// after receiving notification from Global Stats
// Second the popover notification will be shown after page loaded
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
guard let self = self else { return }
self.presentOnboardingAdblockNotifications()
self.presentEducationalProductNotifications()
}
}
private func presentOnboardingAdblockNotifications() {
if Preferences.DebugFlag.skipEduPopups == true { return }
var isAboutHomeUrl = false
if let selectedTab = tabManager.selectedTab,
let url = selectedTab.url,
let internalURL = InternalURL(url) {
isAboutHomeUrl = internalURL.isAboutHomeURL
}
guard let selectedTab = tabManager.selectedTab,
!Preferences.General.onboardingAdblockPopoverShown.value,
!benchmarkNotificationPresented,
!Preferences.AppState.backgroundedCleanly.value,
Preferences.Onboarding.isNewRetentionUser.value == true,
!topToolbar.inOverlayMode,
!isTabTrayActive,
selectedTab.webView?.scrollView.isDragging == false,
isAboutHomeUrl == false
else {
return
}
let blockedRequestURLs = selectedTab.contentBlocker.blockedRequests
if !blockedRequestURLs.isEmpty, let url = selectedTab.url {
let domain = url.baseDomain ?? url.host ?? url.schemelessAbsoluteString
guard currentBenchmarkWebsite != domain else {
return
}
currentBenchmarkWebsite = domain
guard let trackersDetail = BlockedTrackerParser.parse(
blockedRequestURLs: blockedRequestURLs,
selectedTabURL: url) else {
return
}
notifyTrackersBlocked(
domain: domain,
displayTrackers: trackersDetail.displayTrackers,
trackerCount: trackersDetail.trackerCount)
Preferences.General.onboardingAdblockPopoverShown.value = true
}
}
private func presentEducationalProductNotifications() {
if Preferences.DebugFlag.skipEduPopups == true { return }
var isAboutHomeUrl = false
if let selectedTab = tabManager.selectedTab,
let url = selectedTab.url,
let internalURL = InternalURL(url) {
isAboutHomeUrl = internalURL.isAboutHomeURL
}
guard let selectedTab = tabManager.selectedTab,
presentedViewController == nil,
!benchmarkNotificationPresented,
!isOnboardingOrFullScreenCalloutPresented,
!Preferences.AppState.backgroundedCleanly.value,
!topToolbar.inOverlayMode,
!isTabTrayActive,
selectedTab.webView?.scrollView.isDragging == false,
isAboutHomeUrl == false
else {
return
}
// Data Saved Pop-Over only exist in JP locale
if Locale.current.regionCode == "JP" {
if !benchmarkNotificationPresented,
!Preferences.ProductNotificationBenchmarks.showingSpecificDataSavedEnabled.value {
guard let currentURL = selectedTab.url,
DataSaved.get(with: currentURL.absoluteString) == nil,
let domainFetchedSiteSavings = benchmarkBlockingDataSource?.fetchDomainFetchedSiteSavings(currentURL)
else {
return
}
notifyDomainSpecificDataSaved(domainFetchedSiteSavings)
DataSaved.insert(
savedUrl: currentURL.absoluteString,
amount: domainFetchedSiteSavings)
return
}
}
}
private func notifyDomainSpecificDataSaved(_ dataSaved: String) {
let shareTrackersViewController = ShareTrackersController(trackingType: .domainSpecificDataSaved(dataSaved: dataSaved))
dismiss(animated: true)
shareTrackersViewController.actionHandler = { [weak self] action in
guard let self = self, action == .dontShowAgainTapped else { return }
Preferences.ProductNotificationBenchmarks.showingSpecificDataSavedEnabled.value = true
self.dismiss(animated: true)
}
showBenchmarkNotificationPopover(controller: shareTrackersViewController)
}
private func showBenchmarkNotificationPopover(controller: (UIViewController & PopoverContentComponent)) {
benchmarkNotificationPresented = true
let popover = PopoverController(contentController: controller)
popover.addsConvenientDismissalMargins = false
popover.previewForOrigin = .init(view: topToolbar.shieldsButton)
popover.present(from: topToolbar.shieldsButton, on: self)
}
}