Skip to content

Commit

Permalink
Add option to override and fix App Store storefront
Browse files Browse the repository at this point in the history
  • Loading branch information
miasma13 committed Nov 28, 2024
1 parent b0ac95f commit 380a471
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"location" : "https://github.com/duckduckgo/BrowserServicesKit",
"state" : {
"branch" : "michal/non-us-subscriptions",
"revision" : "c28608cff5bc3369d0277d3d186fa59bfc4b83dc"
"revision" : "8d6eb6619eb7615cc765b21d1ab6bed99836a2ed"
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion DuckDuckGo/Menus/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,8 @@ final class MainMenu: NSMenu {
updatePurchasingPlatform: updatePurchasingPlatform,
currentViewController: { WindowControllersManager.shared.lastKeyMainWindowController?.mainViewController },
openSubscriptionTab: { WindowControllersManager.shared.showTab(with: .subscription($0)) },
subscriptionManager: Application.appDelegate.subscriptionManager)
subscriptionManager: Application.appDelegate.subscriptionManager,
subscriptionUserDefaults: subscriptionUserDefaults)

NSMenuItem(title: "Logging").submenu(setupLoggingMenu())
NSMenuItem(title: "AI Chat").submenu(AIChatDebugMenu())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,28 @@ extension DefaultSubscriptionManager {
let subscriptionFeatureFlagger: FeatureFlaggerMapping<SubscriptionFeatureFlags> = FeatureFlaggerMapping { feature in
guard let featureFlagger else {
// With no featureFlagger provided there is no gating of features
return true
return feature.defaultState
}

switch feature {
case .isLaunchedROW:
return featureFlagger.isFeatureOn(.isPrivacyProLaunchedROW)
case .isLaunchedROWOverride:
return featureFlagger.isFeatureOn(.isPrivacyProLaunchedROWOverride)
case .usePrivacyProUSARegionOverride:
return (featureFlagger.internalUserDecider.isInternalUser &&
subscriptionEnvironment.serviceEnvironment == .staging &&
subscriptionUserDefaults.storefrontRegionOverride == .usa)
case .usePrivacyProROWRegionOverride:
return (featureFlagger.internalUserDecider.isInternalUser &&
subscriptionEnvironment.serviceEnvironment == .staging &&
subscriptionUserDefaults.storefrontRegionOverride == .restOfWorld)
}
}

if #available(macOS 12.0, *) {
let storePurchaseManager = DefaultStorePurchaseManager(subscriptionFeatureMappingCache: subscriptionFeatureMappingCache)
let storePurchaseManager = DefaultStorePurchaseManager(subscriptionFeatureMappingCache: subscriptionFeatureMappingCache,
subscriptionFeatureFlagger: subscriptionFeatureFlagger)
self.init(storePurchaseManager: storePurchaseManager,
accountManager: accountManager,
subscriptionEndpointService: subscriptionEndpointService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ public final class SubscriptionDebugMenu: NSMenuItem {
var openSubscriptionTab: (URL) -> Void

private var purchasePlatformItem: NSMenuItem?
private var regionOverrideItem: NSMenuItem?

var currentViewController: () -> NSViewController?
let subscriptionManager: SubscriptionManager
let subscriptionUserDefaults: UserDefaults
var accountManager: AccountManager {
subscriptionManager.accountManager
}
Expand All @@ -38,7 +40,8 @@ public final class SubscriptionDebugMenu: NSMenuItem {
@available(macOS 12.0, *)
fileprivate var purchaseManager: DefaultStorePurchaseManager {
if _purchaseManager == nil {
_purchaseManager = DefaultStorePurchaseManager(subscriptionFeatureMappingCache: subscriptionManager.subscriptionFeatureMappingCache)
_purchaseManager = DefaultStorePurchaseManager(subscriptionFeatureMappingCache: subscriptionManager.subscriptionFeatureMappingCache,
subscriptionFeatureFlagger: nil)
}
// swiftlint:disable:next force_cast
return _purchaseManager as! DefaultStorePurchaseManager
Expand All @@ -53,13 +56,15 @@ public final class SubscriptionDebugMenu: NSMenuItem {
updatePurchasingPlatform: @escaping (SubscriptionEnvironment.PurchasePlatform) -> Void,
currentViewController: @escaping () -> NSViewController?,
openSubscriptionTab: @escaping (URL) -> Void,
subscriptionManager: SubscriptionManager) {
subscriptionManager: SubscriptionManager,
subscriptionUserDefaults: UserDefaults) {
self.currentEnvironment = currentEnvironment
self.updateServiceEnvironment = updateServiceEnvironment
self.updatePurchasingPlatform = updatePurchasingPlatform
self.currentViewController = currentViewController
self.openSubscriptionTab = openSubscriptionTab
self.subscriptionManager = subscriptionManager
self.subscriptionUserDefaults = subscriptionUserDefaults
super.init(title: "Subscription", action: nil, keyEquivalent: "")
self.submenu = makeSubmenu()
}
Expand Down Expand Up @@ -92,6 +97,10 @@ public final class SubscriptionDebugMenu: NSMenuItem {
environmentItem.submenu = makeEnvironmentSubmenu()
menu.addItem(environmentItem)

let regionOverrideItem = NSMenuItem(title: "Region override for App Store Sandbox", action: nil, target: nil)
menu.addItem(regionOverrideItem)
self.regionOverrideItem = regionOverrideItem

menu.delegate = self

return menu
Expand Down Expand Up @@ -154,6 +163,37 @@ public final class SubscriptionDebugMenu: NSMenuItem {
return menu
}

private func makeRegionOverrideItemSubmenu() -> NSMenu {
let menu = NSMenu(title: "")

let currentRegionOverride = subscriptionUserDefaults.storefrontRegionOverride

let usaItem = NSMenuItem(title: "USA", action: #selector(setRegionOverrideToUSA), target: self)
if currentRegionOverride == .usa {
usaItem.state = .on
usaItem.isEnabled = false
usaItem.action = nil
usaItem.target = nil
}
menu.addItem(usaItem)

let rowItem = NSMenuItem(title: "Rest of World", action: #selector(setRegionOverrideToROW), target: self)
if currentRegionOverride == .restOfWorld {
rowItem.state = .on
rowItem.isEnabled = false
rowItem.action = nil
rowItem.target = nil
}
menu.addItem(rowItem)

menu.addItem(.separator())

let clearItem = NSMenuItem(title: "Clear storefront region override", action: #selector(clearRegionOverride), target: self)
menu.addItem(clearItem)

return menu
}

private func refreshSubmenu() {
self.submenu = makeSubmenu()
}
Expand Down Expand Up @@ -232,7 +272,8 @@ public final class SubscriptionDebugMenu: NSMenuItem {

@IBAction func showPurchaseView(_ sender: Any?) {
if #available(macOS 12.0, *) {
let storePurchaseManager = DefaultStorePurchaseManager(subscriptionFeatureMappingCache: subscriptionManager.subscriptionFeatureMappingCache)
let storePurchaseManager = DefaultStorePurchaseManager(subscriptionFeatureMappingCache: subscriptionManager.subscriptionFeatureMappingCache,
subscriptionFeatureFlagger: nil)
let appStoreRestoreFlow = DefaultAppStoreRestoreFlow(accountManager: subscriptionManager.accountManager,
storePurchaseManager: subscriptionManager.storePurchaseManager(),
subscriptionEndpointService: subscriptionManager.subscriptionEndpointService,
Expand Down Expand Up @@ -295,6 +336,30 @@ public final class SubscriptionDebugMenu: NSMenuItem {
NSApp.terminate(self)
}

// MARK: - Region override

@IBAction func clearRegionOverride(_ sender: Any?) {
updateRegionOverride(to: nil)
}

@IBAction func setRegionOverrideToUSA(_ sender: Any?) {
updateRegionOverride(to: .usa)
}

@IBAction func setRegionOverrideToROW(_ sender: Any?) {
updateRegionOverride(to: .restOfWorld)
}

private func updateRegionOverride(to region: SubscriptionRegion?) {
self.subscriptionUserDefaults.storefrontRegionOverride = region

if #available(macOS 12.0, *) {
Task {
await subscriptionManager.storePurchaseManager().updateAvailableProducts()
}
}
}

// MARK: -

@objc
Expand Down Expand Up @@ -345,5 +410,6 @@ extension SubscriptionDebugMenu: NSMenuDelegate {

public func menuWillOpen(_ menu: NSMenu) {
purchasePlatformItem?.submenu = makePurchasePlatformSubmenu()
regionOverrideItem?.submenu = makeRegionOverrideItemSubmenu()
}
}

0 comments on commit 380a471

Please sign in to comment.