From c3af223664ceb75c0f0bed915a6bbf48dcf9ceca Mon Sep 17 00:00:00 2001 From: Juan Manuel Pereira Date: Fri, 22 Dec 2023 13:19:02 -0300 Subject: [PATCH 1/2] Implement sign-out flow for DBP --- .../DBP/DataBrokerProtectionFeatureDisabler.swift | 6 ++++++ .../DataBrokerProtectionSubscriptionEventHandler.swift | 9 ++++++--- DuckDuckGo/Tab/View/BrowserTabViewController.swift | 9 +++++++++ .../TabBar/ViewModel/TabCollectionViewModel.swift | 10 ++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/DuckDuckGo/DBP/DataBrokerProtectionFeatureDisabler.swift b/DuckDuckGo/DBP/DataBrokerProtectionFeatureDisabler.swift index e0df3b67d7..54f99bad09 100644 --- a/DuckDuckGo/DBP/DataBrokerProtectionFeatureDisabler.swift +++ b/DuckDuckGo/DBP/DataBrokerProtectionFeatureDisabler.swift @@ -19,6 +19,10 @@ import Foundation import DataBrokerProtection +public extension Notification.Name { + static let dbpWasDisabled = Notification.Name("com.duckduckgo.DBP.DBPWasDisabled") +} + protocol DataBrokerProtectionFeatureDisabling { func disableAndDelete() } @@ -40,6 +44,8 @@ struct DataBrokerProtectionFeatureDisabler: DataBrokerProtectionFeatureDisabling scheduler.disableLoginItem() dataManager.removeAllData() + + NotificationCenter.default.post(name: .dbpWasDisabled, object: nil) } } } diff --git a/DuckDuckGo/DBP/DataBrokerProtectionSubscriptionEventHandler.swift b/DuckDuckGo/DBP/DataBrokerProtectionSubscriptionEventHandler.swift index d95e292334..fbe20fcdeb 100644 --- a/DuckDuckGo/DBP/DataBrokerProtectionSubscriptionEventHandler.swift +++ b/DuckDuckGo/DBP/DataBrokerProtectionSubscriptionEventHandler.swift @@ -25,11 +25,14 @@ final class DataBrokerProtectionSubscriptionEventHandler { private let accountManager: Subscription.AccountManaging private let authRepository: AuthenticationRepository + private let featureDisabler: DataBrokerProtectionFeatureDisabling - init(accountManager: Subscription.AccountManaging = Subscription.AccountManager(), - authRepository: AuthenticationRepository = KeychainAuthenticationData()) { + init(accountManager: Subscription.AccountManaging = AccountManager(), + authRepository: AuthenticationRepository = KeychainAuthenticationData(), + featureDisabler: DataBrokerProtectionFeatureDisabling = DataBrokerProtectionFeatureDisabler()) { self.accountManager = accountManager self.authRepository = authRepository + self.featureDisabler = featureDisabler } func registerForSubscriptionAccountManagerEvents() { @@ -48,7 +51,7 @@ final class DataBrokerProtectionSubscriptionEventHandler { } @objc private func handleAccountDidSignOut() { - // Going to be defined here: https://app.asana.com/0/1204006570077678/1206206961074916/f + featureDisabler.disableAndDelete() } } diff --git a/DuckDuckGo/Tab/View/BrowserTabViewController.swift b/DuckDuckGo/Tab/View/BrowserTabViewController.swift index 2b44563da5..987d080074 100644 --- a/DuckDuckGo/Tab/View/BrowserTabViewController.swift +++ b/DuckDuckGo/Tab/View/BrowserTabViewController.swift @@ -114,6 +114,10 @@ final class BrowserTabViewController: NSViewController { object: nil) #if DBP + NotificationCenter.default.addObserver(self, + selector: #selector(onDBPFeatureDisabled), + name: .dbpWasDisabled, + object: nil) NotificationCenter.default.addObserver(self, selector: #selector(onCloseDataBrokerProtection), name: .dbpDidClose, @@ -165,6 +169,11 @@ final class BrowserTabViewController: NSViewController { } #if DBP + @objc + private func onDBPFeatureDisabled(_ notification: Notification) { + tabCollectionViewModel.removeAll(with: .dataBrokerProtection) + } + @objc private func onCloseDataBrokerProtection(_ notification: Notification) { guard let activeTab = tabCollectionViewModel.selectedTabViewModel?.tab, diff --git a/DuckDuckGo/TabBar/ViewModel/TabCollectionViewModel.swift b/DuckDuckGo/TabBar/ViewModel/TabCollectionViewModel.swift index cf0c70d754..25e76760dc 100644 --- a/DuckDuckGo/TabBar/ViewModel/TabCollectionViewModel.swift +++ b/DuckDuckGo/TabBar/ViewModel/TabCollectionViewModel.swift @@ -361,6 +361,16 @@ final class TabCollectionViewModel: NSObject { // MARK: - Removal + func removeAll(with content: Tab.TabContent) { + let tabs = tabCollection.tabs.filter { $0.content == content } + + for tab in tabs { + if let index = indexInAllTabs(of: tab) { + remove(at: index) + } + } + } + func remove(at index: TabIndex, published: Bool = true, forceChange: Bool = false) { switch index { case .unpinned(let i): From 5ed8467329ab341b2cc48bd589fd22e4332cb247 Mon Sep 17 00:00:00 2001 From: Juan Manuel Pereira Date: Fri, 5 Jan 2024 14:22:36 -0300 Subject: [PATCH 2/2] Disable type_body_length rule --- DuckDuckGo/TabBar/ViewModel/TabCollectionViewModel.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/DuckDuckGo/TabBar/ViewModel/TabCollectionViewModel.swift b/DuckDuckGo/TabBar/ViewModel/TabCollectionViewModel.swift index 25e76760dc..18bb06de99 100644 --- a/DuckDuckGo/TabBar/ViewModel/TabCollectionViewModel.swift +++ b/DuckDuckGo/TabBar/ViewModel/TabCollectionViewModel.swift @@ -37,6 +37,7 @@ protocol TabCollectionViewModelDelegate: AnyObject { } @MainActor +// swiftlint:disable:next type_body_length final class TabCollectionViewModel: NSObject { weak var delegate: TabCollectionViewModelDelegate?