From c7ff61eb24d3afdf2b6977173508ce396d8ef809 Mon Sep 17 00:00:00 2001 From: Michal Smaga Date: Fri, 22 Mar 2024 22:26:08 +0100 Subject: [PATCH] Rework subscription feature availability to avodi refering appDelegate --- DuckDuckGo/Application/AppDelegate.swift | 5 ----- .../DBP/DataBrokerProtectionFeatureVisibility.swift | 3 ++- .../NavigationBar/View/AddressBarTextField.swift | 3 ++- DuckDuckGo/NavigationBar/View/MoreOptionsMenu.swift | 12 ++++++++---- .../View/NavigationBarViewController.swift | 2 +- .../NetworkProtection+ConvenienceInitializers.swift | 8 ++++---- .../Preferences/Model/PreferencesSection.swift | 2 +- DuckDuckGo/Tab/UserScripts/UserScripts.swift | 3 ++- .../Views/WaitlistThankYouPromptPresenter.swift | 3 ++- .../Views/WaitlistViewControllerPresenter.swift | 3 ++- 10 files changed, 24 insertions(+), 20 deletions(-) diff --git a/DuckDuckGo/Application/AppDelegate.swift b/DuckDuckGo/Application/AppDelegate.swift index e2b524f541..e90e95bb07 100644 --- a/DuckDuckGo/Application/AppDelegate.swift +++ b/DuckDuckGo/Application/AppDelegate.swift @@ -81,10 +81,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate, FileDownloadManagerDel let bookmarksManager = LocalBookmarkManager.shared var privacyDashboardWindow: NSWindow? -#if SUBSCRIPTION - let subscriptionFeatureAvailability: SubscriptionFeatureAvailability -#endif - #if NETWORK_PROTECTION && SUBSCRIPTION // Needs to be lazy as indirectly depends on AppDelegate private lazy var networkProtectionSubscriptionEventHandler = NetworkProtectionSubscriptionEventHandler() @@ -193,7 +189,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate, FileDownloadManagerDel #else SubscriptionPurchaseEnvironment.current = .stripe #endif - subscriptionFeatureAvailability = DefaultSubscriptionFeatureAvailability() #endif } diff --git a/DuckDuckGo/DBP/DataBrokerProtectionFeatureVisibility.swift b/DuckDuckGo/DBP/DataBrokerProtectionFeatureVisibility.swift index 46de122bd1..be0224fb0e 100644 --- a/DuckDuckGo/DBP/DataBrokerProtectionFeatureVisibility.swift +++ b/DuckDuckGo/DBP/DataBrokerProtectionFeatureVisibility.swift @@ -22,6 +22,7 @@ import Foundation import BrowserServicesKit import Common import DataBrokerProtection +import Subscription protocol DataBrokerProtectionFeatureVisibility { func isFeatureVisible() -> Bool @@ -98,7 +99,7 @@ struct DefaultDataBrokerProtectionFeatureVisibility: DataBrokerProtectionFeature func isPrivacyProEnabled() -> Bool { #if SUBSCRIPTION - return NSApp.delegateTyped.subscriptionFeatureAvailability.isFeatureAvailable + return DefaultSubscriptionFeatureAvailability().isFeatureAvailable #else return false #endif diff --git a/DuckDuckGo/NavigationBar/View/AddressBarTextField.swift b/DuckDuckGo/NavigationBar/View/AddressBarTextField.swift index bad6b6ab63..84fbb30576 100644 --- a/DuckDuckGo/NavigationBar/View/AddressBarTextField.swift +++ b/DuckDuckGo/NavigationBar/View/AddressBarTextField.swift @@ -21,6 +21,7 @@ import Carbon.HIToolbox import Combine import Common import Suggestions +import Subscription final class AddressBarTextField: NSTextField { @@ -347,7 +348,7 @@ final class AddressBarTextField: NSTextField { #endif #if SUBSCRIPTION - if NSApp.delegateTyped.subscriptionFeatureAvailability.isFeatureAvailable { + if DefaultSubscriptionFeatureAvailability().isFeatureAvailable { if providedUrl.isChild(of: URL.subscriptionBaseURL) || providedUrl.isChild(of: URL.identityTheftRestoration) { self.updateValue(selectedTabViewModel: nil, addressBarString: nil) // reset self.window?.makeFirstResponder(nil) diff --git a/DuckDuckGo/NavigationBar/View/MoreOptionsMenu.swift b/DuckDuckGo/NavigationBar/View/MoreOptionsMenu.swift index 321b592833..0ab306100c 100644 --- a/DuckDuckGo/NavigationBar/View/MoreOptionsMenu.swift +++ b/DuckDuckGo/NavigationBar/View/MoreOptionsMenu.swift @@ -317,7 +317,7 @@ final class MoreOptionsMenu: NSMenu { var items: [NSMenuItem] = [] #if SUBSCRIPTION - if NSApp.delegateTyped.subscriptionFeatureAvailability.isFeatureAvailable && !AccountManager().isUserAuthenticated { + if DefaultSubscriptionFeatureAvailability().isFeatureAvailable && !AccountManager().isUserAuthenticated { items.append(contentsOf: makeInactiveSubscriptionItems()) } else { items.append(contentsOf: makeActiveSubscriptionItems()) // this adds NETP and DBP only if conditionally enabled @@ -336,6 +336,10 @@ final class MoreOptionsMenu: NSMenu { private func makeActiveSubscriptionItems() -> [NSMenuItem] { var items: [NSMenuItem] = [] +#if SUBSCRIPTION + let subscriptionFeatureAvailability = DefaultSubscriptionFeatureAvailability() +#endif + #if NETWORK_PROTECTION if networkProtectionFeatureVisibility.isNetworkProtectionVisible() { let networkProtectionItem: NSMenuItem @@ -344,7 +348,7 @@ final class MoreOptionsMenu: NSMenu { items.append(networkProtectionItem) #if SUBSCRIPTION - if NSApp.delegateTyped.subscriptionFeatureAvailability.isFeatureAvailable && AccountManager().isUserAuthenticated { + if subscriptionFeatureAvailability.isFeatureAvailable && AccountManager().isUserAuthenticated { Task { let isMenuItemEnabled: Bool @@ -377,7 +381,7 @@ final class MoreOptionsMenu: NSMenu { items.append(dataBrokerProtectionItem) #if SUBSCRIPTION - if NSApp.delegateTyped.subscriptionFeatureAvailability.isFeatureAvailable && AccountManager().isUserAuthenticated { + if subscriptionFeatureAvailability.isFeatureAvailable && AccountManager().isUserAuthenticated { Task { let isMenuItemEnabled: Bool @@ -409,7 +413,7 @@ final class MoreOptionsMenu: NSMenu { .withImage(.itrIcon) items.append(identityTheftRestorationItem) - if NSApp.delegateTyped.subscriptionFeatureAvailability.isFeatureAvailable && AccountManager().isUserAuthenticated { + if subscriptionFeatureAvailability.isFeatureAvailable && AccountManager().isUserAuthenticated { Task { let isMenuItemEnabled: Bool diff --git a/DuckDuckGo/NavigationBar/View/NavigationBarViewController.swift b/DuckDuckGo/NavigationBar/View/NavigationBarViewController.swift index 98b8498896..d392da9c79 100644 --- a/DuckDuckGo/NavigationBar/View/NavigationBarViewController.swift +++ b/DuckDuckGo/NavigationBar/View/NavigationBarViewController.swift @@ -327,7 +327,7 @@ final class NavigationBarViewController: NSViewController { } #if SUBSCRIPTION - if NSApp.delegateTyped.subscriptionFeatureAvailability.isFeatureAvailable { + if DefaultSubscriptionFeatureAvailability().isFeatureAvailable { let accountManager = AccountManager() let networkProtectionTokenStorage = NetworkProtectionKeychainTokenStore() diff --git a/DuckDuckGo/NetworkProtection/AppTargets/BothAppTargets/NetworkProtection+ConvenienceInitializers.swift b/DuckDuckGo/NetworkProtection/AppTargets/BothAppTargets/NetworkProtection+ConvenienceInitializers.swift index 113f737b58..b88189354c 100644 --- a/DuckDuckGo/NetworkProtection/AppTargets/BothAppTargets/NetworkProtection+ConvenienceInitializers.swift +++ b/DuckDuckGo/NetworkProtection/AppTargets/BothAppTargets/NetworkProtection+ConvenienceInitializers.swift @@ -37,7 +37,7 @@ extension NetworkProtectionDeviceManager { tokenStore: tokenStore, keyStore: keyStore, errorEvents: .networkProtectionAppDebugEvents, - isSubscriptionEnabled: NSApp.delegateTyped.subscriptionFeatureAvailability.isFeatureAvailable) + isSubscriptionEnabled: DefaultSubscriptionFeatureAvailability().isFeatureAvailable) } } @@ -47,13 +47,13 @@ extension NetworkProtectionCodeRedemptionCoordinator { self.init(environment: settings.selectedEnvironment, tokenStore: NetworkProtectionKeychainTokenStore(), errorEvents: .networkProtectionAppDebugEvents, - isSubscriptionEnabled: NSApp.delegateTyped.subscriptionFeatureAvailability.isFeatureAvailable) + isSubscriptionEnabled: DefaultSubscriptionFeatureAvailability().isFeatureAvailable) } } extension NetworkProtectionKeychainTokenStore { convenience init() { - self.init(isSubscriptionEnabled: NSApp.delegateTyped.subscriptionFeatureAvailability.isFeatureAvailable) + self.init(isSubscriptionEnabled: DefaultSubscriptionFeatureAvailability().isFeatureAvailable) } convenience init(isSubscriptionEnabled: Bool) { @@ -83,7 +83,7 @@ extension NetworkProtectionLocationListCompositeRepository { environment: settings.selectedEnvironment, tokenStore: NetworkProtectionKeychainTokenStore(), errorEvents: .networkProtectionAppDebugEvents, - isSubscriptionEnabled: NSApp.delegateTyped.subscriptionFeatureAvailability.isFeatureAvailable + isSubscriptionEnabled: DefaultSubscriptionFeatureAvailability().isFeatureAvailable ) } } diff --git a/DuckDuckGo/Preferences/Model/PreferencesSection.swift b/DuckDuckGo/Preferences/Model/PreferencesSection.swift index b05bcc4dd9..9274a723dd 100644 --- a/DuckDuckGo/Preferences/Model/PreferencesSection.swift +++ b/DuckDuckGo/Preferences/Model/PreferencesSection.swift @@ -60,7 +60,7 @@ struct PreferencesSection: Hashable, Identifiable { ] #if SUBSCRIPTION - if NSApp.delegateTyped.subscriptionFeatureAvailability.isFeatureAvailable { + if DefaultSubscriptionFeatureAvailability().isFeatureAvailable { let subscriptionPanes: [PreferencePaneIdentifier] = [.subscription] sections.insert(.init(id: .privacyPro, panes: subscriptionPanes), at: 1) } diff --git a/DuckDuckGo/Tab/UserScripts/UserScripts.swift b/DuckDuckGo/Tab/UserScripts/UserScripts.swift index d4cb62cdce..017c0c6716 100644 --- a/DuckDuckGo/Tab/UserScripts/UserScripts.swift +++ b/DuckDuckGo/Tab/UserScripts/UserScripts.swift @@ -20,6 +20,7 @@ import Foundation import BrowserServicesKit import UserScript import WebKit +import Subscription @MainActor final class UserScripts: UserScriptsProvider { @@ -87,7 +88,7 @@ final class UserScripts: UserScriptsProvider { } #if SUBSCRIPTION - if NSApp.delegateTyped.subscriptionFeatureAvailability.isFeatureAvailable { + if DefaultSubscriptionFeatureAvailability().isFeatureAvailable { subscriptionPagesUserScript.registerSubfeature(delegate: SubscriptionPagesUseSubscriptionFeature()) userScripts.append(subscriptionPagesUserScript) diff --git a/DuckDuckGo/Waitlist/Views/WaitlistThankYouPromptPresenter.swift b/DuckDuckGo/Waitlist/Views/WaitlistThankYouPromptPresenter.swift index 07155802a3..2237c32222 100644 --- a/DuckDuckGo/Waitlist/Views/WaitlistThankYouPromptPresenter.swift +++ b/DuckDuckGo/Waitlist/Views/WaitlistThankYouPromptPresenter.swift @@ -18,6 +18,7 @@ import AppKit import Foundation +import Subscription final class WaitlistThankYouPromptPresenter { @@ -53,7 +54,7 @@ final class WaitlistThankYouPromptPresenter { func presentThankYouPromptIfNecessary(in window: NSWindow) { // Wiring this here since it's mostly useful for rolling out PrivacyPro, and should // go away once PPro is fully rolled out. - if NSApp.delegateTyped.subscriptionFeatureAvailability.isFeatureAvailable { + if DefaultSubscriptionFeatureAvailability().isFeatureAvailable { DailyPixel.fire(pixel: .privacyProFeatureEnabled, frequency: .dailyOnly) } diff --git a/DuckDuckGo/Waitlist/Views/WaitlistViewControllerPresenter.swift b/DuckDuckGo/Waitlist/Views/WaitlistViewControllerPresenter.swift index f51b0b695c..22fcf9bbdb 100644 --- a/DuckDuckGo/Waitlist/Views/WaitlistViewControllerPresenter.swift +++ b/DuckDuckGo/Waitlist/Views/WaitlistViewControllerPresenter.swift @@ -18,6 +18,7 @@ import Foundation import UserNotifications +import Subscription #if NETWORK_PROTECTION || DBP @@ -82,7 +83,7 @@ struct DataBrokerProtectionWaitlistViewControllerPresenter: WaitlistViewControll static func shouldPresentWaitlist() -> Bool { #if SUBSCRIPTION - if NSApp.delegateTyped.subscriptionFeatureAvailability.isFeatureAvailable { + if DefaultSubscriptionFeatureAvailability().isFeatureAvailable { return false } #endif