From c79810833f6c4717e6ac073ddd6eea719db8dd86 Mon Sep 17 00:00:00 2001 From: Brad Slayter Date: Wed, 18 Oct 2023 09:38:55 -0500 Subject: [PATCH 1/6] Productionize DBP web ui code --- DuckDuckGo/DBP/DBPHomeViewController.swift | 1 - .../DataBrokerProtectionViewController.swift | 166 +----------------- 2 files changed, 7 insertions(+), 160 deletions(-) diff --git a/DuckDuckGo/DBP/DBPHomeViewController.swift b/DuckDuckGo/DBP/DBPHomeViewController.swift index 9e8c7d23c5..38b581509a 100644 --- a/DuckDuckGo/DBP/DBPHomeViewController.swift +++ b/DuckDuckGo/DBP/DBPHomeViewController.swift @@ -51,7 +51,6 @@ final class DBPHomeViewController: NSViewController { return DataBrokerProtectionViewController(scheduler: dataBrokerProtectionManager.scheduler, dataManager: dataBrokerProtectionManager.dataManager, - notificationCenter: NotificationCenter.default, privacyConfig: privacyConfigurationManager, prefs: prefs) }() diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift index 579808cb57..d909ff017c 100644 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift +++ b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift @@ -23,165 +23,22 @@ import WebKit import Combine final public class DataBrokerProtectionViewController: NSViewController { - private let navigationViewModel: ContainerNavigationViewModel - private let profileViewModel: ProfileViewModel private let dataManager: DataBrokerProtectionDataManaging - private let resultsViewModel: ResultsViewModel - private let containerViewModel: ContainerViewModel private let scheduler: DataBrokerProtectionScheduler - private let notificationCenter: NotificationCenter private var webView: WKWebView? private let webUIViewModel: DBPUIViewModel - private let debugPage: String = """ - - - - - - Document - - -
- - - - - - - - -
- -

- - - - - """ - public init(scheduler: DataBrokerProtectionScheduler, dataManager: DataBrokerProtectionDataManaging, - notificationCenter: NotificationCenter = .default, - privacyConfig: PrivacyConfigurationManaging? = nil, prefs: ContentScopeProperties? = nil) { + privacyConfig: PrivacyConfigurationManaging? = nil, + prefs: ContentScopeProperties? = nil) { self.scheduler = scheduler self.dataManager = dataManager - self.notificationCenter = notificationCenter self.webUIViewModel = DBPUIViewModel(dataManager: dataManager, scheduler: scheduler, privacyConfig: privacyConfig, prefs: prefs, webView: webView) - navigationViewModel = ContainerNavigationViewModel(dataManager: dataManager) - profileViewModel = ProfileViewModel(dataManager: dataManager) - - resultsViewModel = ResultsViewModel(dataManager: dataManager, - notificationCenter: notificationCenter) - - containerViewModel = ContainerViewModel(scheduler: scheduler, - dataManager: dataManager) - - dataManager.fetchProfile(ignoresCache: true) + _ = dataManager.fetchProfile(ignoresCache: true) super.init(nibName: nil, bundle: nil) } @@ -191,25 +48,16 @@ final public class DataBrokerProtectionViewController: NSViewController { } override public func loadView() { - let containerView = DataBrokerProtectionContainerView( - containerViewModel: containerViewModel, - navigationViewModel: navigationViewModel, - profileViewModel: profileViewModel, - resultsViewModel: resultsViewModel) + guard let configuration = webUIViewModel.setupCommunicationLayer() else { return } - let hostingController = NSHostingController(rootView: containerView) - view = hostingController.view - -// guard let configuration = webUIViewModel.setupCommunicationLayer() else { return } -// -// webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 1024, height: 768), configuration: configuration) -// view = webView! + webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 1024, height: 768), configuration: configuration) + view = webView! // FOR LOCAL WEB UI DEVELOPMENT: // Comment this line 👇 // webView?.loadHTMLString(debugPage, baseURL: nil) // Uncomment this line and add your dev URL 👇 -// webView?.load(URL(string: "https://")!) + webView?.load(URL(string: "https://bhall.duckduckgo.com/data-broker-protection")!) } } From 5c481e6bc0f0617c6a2b1faa510fc42cc4303c32 Mon Sep 17 00:00:00 2001 From: Brad Slayter Date: Wed, 18 Oct 2023 14:09:30 -0500 Subject: [PATCH 2/6] Change dev url --- .../UI/DataBrokerProtectionViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift index d909ff017c..a9a734d0e7 100644 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift +++ b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift @@ -57,7 +57,7 @@ final public class DataBrokerProtectionViewController: NSViewController { // Comment this line 👇 // webView?.loadHTMLString(debugPage, baseURL: nil) // Uncomment this line and add your dev URL 👇 - webView?.load(URL(string: "https://bhall.duckduckgo.com/data-broker-protection")!) + webView?.load(URL(string: "https://bhall.duckduckgo.com/dbp")!) } } From 0cdb9fff4446a444ab5edc609e8550957af18280 Mon Sep 17 00:00:00 2001 From: Brad Slayter Date: Thu, 19 Oct 2023 13:04:45 -0500 Subject: [PATCH 3/6] Support open in new tab for DBP web ui --- DuckDuckGo/DBP/DBPHomeViewController.swift | 5 ++++- .../UI/DataBrokerProtectionViewController.swift | 13 ++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/DuckDuckGo/DBP/DBPHomeViewController.swift b/DuckDuckGo/DBP/DBPHomeViewController.swift index 38b581509a..7fa79d6e51 100644 --- a/DuckDuckGo/DBP/DBPHomeViewController.swift +++ b/DuckDuckGo/DBP/DBPHomeViewController.swift @@ -52,7 +52,10 @@ final class DBPHomeViewController: NSViewController { return DataBrokerProtectionViewController(scheduler: dataBrokerProtectionManager.scheduler, dataManager: dataBrokerProtectionManager.dataManager, privacyConfig: privacyConfigurationManager, - prefs: prefs) + prefs: prefs, + openURLHandler: { url in + WindowControllersManager.shared.show(url: url, newTab: true) + }) }() init(dataBrokerProtectionManager: DataBrokerProtectionManager) { diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift index a9a734d0e7..93237e6a3b 100644 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift +++ b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift @@ -29,12 +29,16 @@ final public class DataBrokerProtectionViewController: NSViewController { private let webUIViewModel: DBPUIViewModel + private let openURLHandler: (URL?) -> Void + public init(scheduler: DataBrokerProtectionScheduler, dataManager: DataBrokerProtectionDataManaging, privacyConfig: PrivacyConfigurationManaging? = nil, - prefs: ContentScopeProperties? = nil) { + prefs: ContentScopeProperties? = nil, + openURLHandler: @escaping (URL?) -> Void) { self.scheduler = scheduler self.dataManager = dataManager + self.openURLHandler = openURLHandler self.webUIViewModel = DBPUIViewModel(dataManager: dataManager, scheduler: scheduler, privacyConfig: privacyConfig, prefs: prefs, webView: webView) @@ -61,3 +65,10 @@ final public class DataBrokerProtectionViewController: NSViewController { } } + +extension DataBrokerProtectionViewController: WKUIDelegate { + public func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? { + openURLHandler(navigationAction.request.url) + return nil + } +} From 0f1f18eef57f7fffb300a9ac7ce1a3a948afef3f Mon Sep 17 00:00:00 2001 From: Brad Slayter Date: Thu, 19 Oct 2023 13:13:46 -0500 Subject: [PATCH 4/6] Set the delegate --- .../UI/DataBrokerProtectionViewController.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift index 93237e6a3b..caa0b8f31b 100644 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift +++ b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift @@ -55,6 +55,7 @@ final public class DataBrokerProtectionViewController: NSViewController { guard let configuration = webUIViewModel.setupCommunicationLayer() else { return } webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 1024, height: 768), configuration: configuration) + webView?.uiDelegate = self view = webView! // FOR LOCAL WEB UI DEVELOPMENT: From 4c2f777289ea28e7320df29ba1f8e3eaf706b644 Mon Sep 17 00:00:00 2001 From: Brad Slayter Date: Thu, 19 Oct 2023 13:30:18 -0500 Subject: [PATCH 5/6] lint --- .../UI/DataBrokerProtectionViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift index caa0b8f31b..5dfe2cc01f 100644 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift +++ b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift @@ -33,7 +33,7 @@ final public class DataBrokerProtectionViewController: NSViewController { public init(scheduler: DataBrokerProtectionScheduler, dataManager: DataBrokerProtectionDataManaging, - privacyConfig: PrivacyConfigurationManaging? = nil, + privacyConfig: PrivacyConfigurationManaging? = nil, prefs: ContentScopeProperties? = nil, openURLHandler: @escaping (URL?) -> Void) { self.scheduler = scheduler From c3f24b7896a9d7a91200b8a3c614a7decb9a5b59 Mon Sep 17 00:00:00 2001 From: Brad Slayter Date: Fri, 20 Oct 2023 14:06:40 -0500 Subject: [PATCH 6/6] PR comments --- .../UI/DataBrokerProtectionViewController.swift | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift index 5dfe2cc01f..70a4fa6bdb 100644 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift +++ b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift @@ -23,6 +23,11 @@ import WebKit import Combine final public class DataBrokerProtectionViewController: NSViewController { + + private enum Constants { + static let dbpUiUrl = "https://duckduckgo.com/dbp" + } + private let dataManager: DataBrokerProtectionDataManaging private let scheduler: DataBrokerProtectionScheduler private var webView: WKWebView? @@ -42,7 +47,9 @@ final public class DataBrokerProtectionViewController: NSViewController { self.webUIViewModel = DBPUIViewModel(dataManager: dataManager, scheduler: scheduler, privacyConfig: privacyConfig, prefs: prefs, webView: webView) - _ = dataManager.fetchProfile(ignoresCache: true) + Task { + _ = dataManager.fetchProfile(ignoresCache: true) + } super.init(nibName: nil, bundle: nil) } @@ -58,11 +65,7 @@ final public class DataBrokerProtectionViewController: NSViewController { webView?.uiDelegate = self view = webView! - // FOR LOCAL WEB UI DEVELOPMENT: - // Comment this line 👇 -// webView?.loadHTMLString(debugPage, baseURL: nil) - // Uncomment this line and add your dev URL 👇 - webView?.load(URL(string: "https://bhall.duckduckgo.com/dbp")!) + webView?.load(URL(string: Constants.dbpUiUrl)!) } }