Skip to content

Commit

Permalink
Remove leftover .isFeatureAvailable calls
Browse files Browse the repository at this point in the history
  • Loading branch information
miasma13 committed Jan 8, 2025
1 parent 903c778 commit 03e96f7
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/duckduckgo/BrowserServicesKit",
"state" : {
"branch" : "michal/fix-expired-subscription-refresh-loop",
"revision" : "57f5cd3c5bdd99b330b4e6c3cef89ca6e12f2774"
"branch" : "michal/international-pp-cleanup",
"revision" : "84d2f7e4206707f5198a1ba669290d8ff86da22b"
}
},
{
Expand Down
15 changes: 0 additions & 15 deletions DuckDuckGo/Menus/MainMenuActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -909,21 +909,6 @@ extension MainViewController {
guard let internalUserDecider = NSApp.delegateTyped.internalUserDecider as? DefaultInternalUserDecider else { return }
let state = internalUserDecider.isInternalUser
internalUserDecider.debugSetInternalUserState(!state)

if !DefaultSubscriptionFeatureAvailability().isFeatureAvailable {
// We only clear PPro state when it's not available, as otherwise
// there should be no state to clear. Clearing PPro state can
// trigger notifications which we want to avoid unless
// necessary.
clearPrivacyProState()
}
}

/// Clears the PrivacyPro state to make testing easier.
///
private func clearPrivacyProState() {
Application.appDelegate.subscriptionManager.accountManager.signOut()
UserDefaults.netP.networkProtectionEntitlementsExpired = false
}

@objc func resetDailyPixels(_ sender: Any?) {
Expand Down
15 changes: 7 additions & 8 deletions DuckDuckGo/NavigationBar/View/AddressBarTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -421,14 +421,13 @@ final class AddressBarTextField: NSTextField {
}
#endif

if DefaultSubscriptionFeatureAvailability().isFeatureAvailable {
let baseURL = Application.appDelegate.subscriptionManager.url(for: .baseURL)
let identityTheftRestorationURL = Application.appDelegate.subscriptionManager.url(for: .identityTheftRestoration)
if providedUrl.isChild(of: baseURL) || providedUrl.isChild(of: identityTheftRestorationURL) {
self.updateValue(selectedTabViewModel: nil, addressBarString: nil) // reset
self.window?.makeFirstResponder(nil)
return
}
// Prevent typing in subscription URLs directly in the address bar
let baseURL = Application.appDelegate.subscriptionManager.url(for: .baseURL)
let identityTheftRestorationURL = Application.appDelegate.subscriptionManager.url(for: .identityTheftRestoration)
if providedUrl.isChild(of: baseURL) || providedUrl.isChild(of: identityTheftRestorationURL) {
self.updateValue(selectedTabViewModel: nil, addressBarString: nil) // reset
self.window?.makeFirstResponder(nil)
return
}

self.window?.makeFirstResponder(nil)
Expand Down
4 changes: 1 addition & 3 deletions DuckDuckGo/NavigationBar/View/MoreOptionsMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,6 @@ final class MoreOptionsMenu: NSMenu, NSMenuDelegate {

@MainActor
private func addSubscriptionItems() {
guard subscriptionFeatureAvailability.isFeatureAvailable else { return }

func shouldHideDueToNoProduct() -> Bool {
let platform = subscriptionManager.currentEnvironment.purchasePlatform
return platform == .appStore && subscriptionManager.canPurchase == false
Expand Down Expand Up @@ -966,7 +964,7 @@ final class SubscriptionSubMenu: NSMenu, NSMenuDelegate {
}

private func refreshAvailabilityBasedOnEntitlements() {
guard subscriptionFeatureAvailability.isFeatureAvailable, subscriptionManager.accountManager.isUserAuthenticated else { return }
guard subscriptionManager.accountManager.isUserAuthenticated else { return }

@Sendable func hasEntitlement(for productName: Entitlement.ProductName) async -> Bool {
switch await self.subscriptionManager.accountManager.hasEntitlement(forProductName: productName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,7 @@ final class NavigationBarViewController: NSViewController {
}

private func toggleNetworkProtectionPopover() {
guard DefaultSubscriptionFeatureAvailability().isFeatureAvailable,
NetworkProtectionKeychainTokenStore().isFeatureActivated else {
guard NetworkProtectionKeychainTokenStore().isFeatureActivated else {
return
}

Expand Down
26 changes: 12 additions & 14 deletions DuckDuckGo/Preferences/Model/PreferencesSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,22 @@ struct PreferencesSection: Hashable, Identifiable {
.init(id: .about, panes: otherPanes)
]

if DefaultSubscriptionFeatureAvailability().isFeatureAvailable {
let subscriptionManager = Application.appDelegate.subscriptionManager
let platform = subscriptionManager.currentEnvironment.purchasePlatform
var shouldHidePrivacyProDueToNoProducts = platform == .appStore && subscriptionManager.canPurchase == false
let subscriptionManager = Application.appDelegate.subscriptionManager
let platform = subscriptionManager.currentEnvironment.purchasePlatform
var shouldHidePrivacyProDueToNoProducts = platform == .appStore && subscriptionManager.canPurchase == false

if subscriptionManager.accountManager.isUserAuthenticated {
shouldHidePrivacyProDueToNoProducts = false
}

if !shouldHidePrivacyProDueToNoProducts {
var subscriptionPanes: [PreferencePaneIdentifier] = [.subscription]
if subscriptionManager.accountManager.isUserAuthenticated {
shouldHidePrivacyProDueToNoProducts = false
}

if includingVPN {
subscriptionPanes.append(.vpn)
}
if !shouldHidePrivacyProDueToNoProducts {
var subscriptionPanes: [PreferencePaneIdentifier] = [.subscription]

sections.insert(.init(id: .privacyPro, panes: subscriptionPanes), at: 1)
if includingVPN {
subscriptionPanes.append(.vpn)
}

sections.insert(.init(id: .privacyPro, panes: subscriptionPanes), at: 1)
}

return sections
Expand Down
8 changes: 2 additions & 6 deletions DuckDuckGo/Subscription/SubscriptionRedirectManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@ protocol SubscriptionRedirectManager: AnyObject {

final class PrivacyProSubscriptionRedirectManager: SubscriptionRedirectManager {

private let featureAvailabiltyProvider: () -> Bool
private let subscriptionEnvironment: SubscriptionEnvironment
private let canPurchase: () -> Bool
private let baseURL: URL

init(featureAvailabiltyProvider: @escaping @autoclosure () -> Bool = DefaultSubscriptionFeatureAvailability().isFeatureAvailable,
subscriptionEnvironment: SubscriptionEnvironment,
init(subscriptionEnvironment: SubscriptionEnvironment,
baseURL: URL,
canPurchase: @escaping () -> Bool) {
self.featureAvailabiltyProvider = featureAvailabiltyProvider
self.subscriptionEnvironment = subscriptionEnvironment
self.canPurchase = canPurchase
self.baseURL = baseURL
Expand All @@ -45,9 +42,8 @@ final class PrivacyProSubscriptionRedirectManager: SubscriptionRedirectManager {
guard url.isPart(ofDomain: "duckduckgo.com") else { return nil }

if url.pathComponents == URL.privacyPro.pathComponents {
let isFeatureAvailable = featureAvailabiltyProvider()
let shouldHidePrivacyProDueToNoProducts = subscriptionEnvironment.purchasePlatform == .appStore && canPurchase() == false
let isPurchasePageRedirectActive = isFeatureAvailable && !shouldHidePrivacyProDueToNoProducts
let isPurchasePageRedirectActive = !shouldHidePrivacyProDueToNoProducts
// Redirect the `/pro` URL to `/subscriptions` URL. If there are any query items in the original URL it appends to the `/subscriptions` URL.
return isPurchasePageRedirectActive ? baseURL.addingQueryItems(from: url) : nil
}
Expand Down
16 changes: 0 additions & 16 deletions DuckDuckGo/Tab/Navigation/RedirectNavigationResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,4 @@ struct RedirectNavigationResponder: NavigationResponder {
navigator.load(request)
}
}

// private func redirectURL(for url: URL) -> URL? {
// guard url.isPart(ofDomain: "duckduckgo.com") else { return nil }
//
// if url.pathComponents == URL.privacyPro.pathComponents {
// let isFeatureAvailable = DefaultSubscriptionFeatureAvailability().isFeatureAvailable
// let subscriptionManager = Application.appDelegate.subscriptionManager
// let platform = subscriptionManager.currentEnvironment.purchasePlatform
// let shouldHidePrivacyProDueToNoProducts = platform == .appStore && subscriptionManager.canPurchase == false
// let isPurchasePageRedirectActive = isFeatureAvailable && !shouldHidePrivacyProDueToNoProducts
// let url = SubscriptionURL.baseURL.subscriptionURL(environment: subscriptionManager.currentEnvironment.serviceEnvironment)
// return isPurchasePageRedirectActive ? url : nil
// }
//
// return nil
// }
}
30 changes: 14 additions & 16 deletions DuckDuckGo/Tab/UserScripts/UserScripts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,20 @@ final class UserScripts: UserScriptsProvider {
userScripts.append(specialPages)
}

if DefaultSubscriptionFeatureAvailability().isFeatureAvailable {
let subscriptionManager = Application.appDelegate.subscriptionManager
let stripePurchaseFlow = DefaultStripePurchaseFlow(subscriptionEndpointService: subscriptionManager.subscriptionEndpointService,
authEndpointService: subscriptionManager.authEndpointService,
accountManager: subscriptionManager.accountManager)
let freemiumDBPPixelExperimentManager = FreemiumDBPPixelExperimentManager(subscriptionManager: subscriptionManager)
let delegate = SubscriptionPagesUseSubscriptionFeature(subscriptionManager: subscriptionManager,
stripePurchaseFlow: stripePurchaseFlow,
uiHandler: Application.appDelegate.subscriptionUIHandler,
freemiumDBPPixelExperimentManager: freemiumDBPPixelExperimentManager)
subscriptionPagesUserScript.registerSubfeature(delegate: delegate)
userScripts.append(subscriptionPagesUserScript)

identityTheftRestorationPagesUserScript.registerSubfeature(delegate: IdentityTheftRestorationPagesFeature())
userScripts.append(identityTheftRestorationPagesUserScript)
}
let subscriptionManager = Application.appDelegate.subscriptionManager
let stripePurchaseFlow = DefaultStripePurchaseFlow(subscriptionEndpointService: subscriptionManager.subscriptionEndpointService,
authEndpointService: subscriptionManager.authEndpointService,
accountManager: subscriptionManager.accountManager)
let freemiumDBPPixelExperimentManager = FreemiumDBPPixelExperimentManager(subscriptionManager: subscriptionManager)
let delegate = SubscriptionPagesUseSubscriptionFeature(subscriptionManager: subscriptionManager,
stripePurchaseFlow: stripePurchaseFlow,
uiHandler: Application.appDelegate.subscriptionUIHandler,
freemiumDBPPixelExperimentManager: freemiumDBPPixelExperimentManager)
subscriptionPagesUserScript.registerSubfeature(delegate: delegate)
userScripts.append(subscriptionPagesUserScript)

identityTheftRestorationPagesUserScript.registerSubfeature(delegate: IdentityTheftRestorationPagesFeature())
userScripts.append(identityTheftRestorationPagesUserScript)
}

lazy var userScripts: [UserScript] = [
Expand Down
17 changes: 2 additions & 15 deletions DuckDuckGo/Waitlist/VPNFeatureGatekeeper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ struct DefaultVPNFeatureGatekeeper: VPNFeatureGatekeeper {
/// For subscription users this means they have entitlements.
///
func canStartVPN() async throws -> Bool {
guard subscriptionFeatureAvailability.isFeatureAvailable else {
return false
}

switch await subscriptionManager.accountManager.hasEntitlement(forProductName: .networkProtection) {
case .success(let hasEntitlement):
return hasEntitlement
Expand All @@ -83,22 +79,13 @@ struct DefaultVPNFeatureGatekeeper: VPNFeatureGatekeeper {
/// For subscription users this means they are authenticated.
///
func isVPNVisible() -> Bool {
guard subscriptionFeatureAvailability.isFeatureAvailable else {
return false
}
return subscriptionManager.accountManager.isUserAuthenticated
}

/// We've had to add this method because accessing the singleton in app delegate is crashing the integration tests.
///
var subscriptionFeatureAvailability: DefaultSubscriptionFeatureAvailability {
DefaultSubscriptionFeatureAvailability()
subscriptionManager.accountManager.isUserAuthenticated
}

/// Returns whether the VPN should be uninstalled automatically.
/// This is only true when the user is not an Easter Egg user, the waitlist test has ended, and the user is onboarded.
func shouldUninstallAutomatically() -> Bool {
return subscriptionFeatureAvailability.isFeatureAvailable && !subscriptionManager.accountManager.isUserAuthenticated && LoginItem.vpnMenu.status.isInstalled
!subscriptionManager.accountManager.isUserAuthenticated && LoginItem.vpnMenu.status.isInstalled
}

/// Whether the user is fully onboarded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ private class MockFeatureAvailability: SubscriptionFeatureAvailability {
var mockSubscriptionPurchaseAllowed: Bool = false
var mockUsesUnifiedFeedbackForm: Bool = false

var isFeatureAvailable: Bool { mockFeatureAvailable }
var isSubscriptionPurchaseAllowed: Bool { mockSubscriptionPurchaseAllowed }
var usesUnifiedFeedbackForm: Bool { mockUsesUnifiedFeedbackForm }

Expand Down
3 changes: 1 addition & 2 deletions UnitTests/Menus/MoreOptionsMenuTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ final class MoreOptionsMenuTests: XCTestCase {
moreOptionsMenu = MoreOptionsMenu(tabCollectionViewModel: tabCollectionViewModel,
passwordManagerCoordinator: passwordManagerCoordinator,
vpnFeatureGatekeeper: networkProtectionVisibilityMock,
subscriptionFeatureAvailability: SubscriptionFeatureAvailabilityMock(isFeatureAvailable: true,
isSubscriptionPurchaseAllowed: true,
subscriptionFeatureAvailability: SubscriptionFeatureAvailabilityMock(isSubscriptionPurchaseAllowed: true,
usesUnifiedFeedbackForm: false),
sharingMenu: NSMenu(),
internalUserDecider: internalUserDecider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ import Subscription
import BrowserServicesKit

public final class SubscriptionFeatureAvailabilityMock: SubscriptionFeatureAvailability {
public var isFeatureAvailable: Bool
public var isSubscriptionPurchaseAllowed: Bool
public var usesUnifiedFeedbackForm: Bool

public init(isFeatureAvailable: Bool, isSubscriptionPurchaseAllowed: Bool, usesUnifiedFeedbackForm: Bool) {
self.isFeatureAvailable = isFeatureAvailable
public init(isSubscriptionPurchaseAllowed: Bool, usesUnifiedFeedbackForm: Bool) {
self.isSubscriptionPurchaseAllowed = isSubscriptionPurchaseAllowed
self.usesUnifiedFeedbackForm = usesUnifiedFeedbackForm
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ final class SubscriptionPagesUseSubscriptionFeatureTests: XCTestCase {

subscriptionAttributionPixelHandler = PrivacyProSubscriptionAttributionPixelHandler()

subscriptionFeatureAvailability = SubscriptionFeatureAvailabilityMock(isFeatureAvailable: true,
isSubscriptionPurchaseAllowed: true,
subscriptionFeatureAvailability = SubscriptionFeatureAvailabilityMock(isSubscriptionPurchaseAllowed: true,
usesUnifiedFeedbackForm: false)

// Real SubscriptionManager
Expand All @@ -180,8 +179,7 @@ final class SubscriptionPagesUseSubscriptionFeatureTests: XCTestCase {
subscriptionEndpointService: subscriptionService,
authEndpointService: authService,
subscriptionFeatureMappingCache: subscriptionFeatureMappingCache,
subscriptionEnvironment: subscriptionEnvironment,
subscriptionFeatureFlagger: subscriptionFeatureFlagger)
subscriptionEnvironment: subscriptionEnvironment)

mockFreemiumDBPExperimentManager = MockFreemiumDBPExperimentManager()
mockPixelHandler = MockFreemiumDBPExperimentPixelHandler()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ final class SubscriptionPagesUseSubscriptionFeatureTestsForStripe: XCTestCase {
authEndpointService: authService,
accountManager: accountManager)

subscriptionFeatureAvailability = SubscriptionFeatureAvailabilityMock(isFeatureAvailable: true,
isSubscriptionPurchaseAllowed: true,
subscriptionFeatureAvailability = SubscriptionFeatureAvailabilityMock(isSubscriptionPurchaseAllowed: true,
usesUnifiedFeedbackForm: false)

// Real SubscriptionManager
Expand All @@ -184,8 +183,7 @@ final class SubscriptionPagesUseSubscriptionFeatureTestsForStripe: XCTestCase {
subscriptionEndpointService: subscriptionService,
authEndpointService: authService,
subscriptionFeatureMappingCache: subscriptionFeatureMappingCache,
subscriptionEnvironment: subscriptionEnvironment,
subscriptionFeatureFlagger: subscriptionFeatureFlagger)
subscriptionEnvironment: subscriptionEnvironment)

mockFreemiumDBPExperimentManager = MockFreemiumDBPExperimentManager()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ final class SubscriptionRedirectManagerTests: XCTestCase {
override func setUpWithError() throws {
try super.setUpWithError()

sut = PrivacyProSubscriptionRedirectManager(featureAvailabiltyProvider: true,
subscriptionEnvironment: Constants.environment,
sut = PrivacyProSubscriptionRedirectManager(subscriptionEnvironment: Constants.environment,
baseURL: Constants.redirectURL,
canPurchase: { [self] in canPurchase })
}
Expand Down

0 comments on commit 03e96f7

Please sign in to comment.