From ef91ad5e2e9174897f6c2caeb687539fbdd9b5b0 Mon Sep 17 00:00:00 2001 From: Michal Smaga Date: Thu, 21 Nov 2024 16:04:55 +0100 Subject: [PATCH] Expose current storefront region --- Sources/Subscription/Managers/StorePurchaseManager.swift | 4 ++++ Sources/Subscription/StoreSubscriptionConfiguration.swift | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Sources/Subscription/Managers/StorePurchaseManager.swift b/Sources/Subscription/Managers/StorePurchaseManager.swift index 0a4818077..457302808 100644 --- a/Sources/Subscription/Managers/StorePurchaseManager.swift +++ b/Sources/Subscription/Managers/StorePurchaseManager.swift @@ -41,6 +41,7 @@ public protocol StorePurchaseManager { var purchasedProductIDs: [String] { get } var purchaseQueue: [String] { get } var areProductsAvailable: Bool { get } + var currentStorefrontRegion: SubscriptionRegion { get } @MainActor func syncAppleIDAccount() async throws @MainActor func updateAvailableProducts() async @@ -64,6 +65,7 @@ public final class DefaultStorePurchaseManager: ObservableObject, StorePurchaseM @Published public private(set) var purchaseQueue: [String] = [] public var areProductsAvailable: Bool { !availableProducts.isEmpty } + public private(set) var currentStorefrontRegion: SubscriptionRegion = .usa private var transactionUpdates: Task? private var storefrontChanges: Task? @@ -137,6 +139,8 @@ public final class DefaultStorePurchaseManager: ObservableObject, StorePurchaseM do { let currentStorefrontCountryCode = await Storefront.current?.countryCode ?? "" + self.currentStorefrontRegion = SubscriptionRegion.matchingRegion(for: currentStorefrontCountryCode) ?? .usa + let applicableProductIdentifiers = storeSubscriptionConfiguration.subscriptionIdentifiers(for: currentStorefrontCountryCode) let availableProducts = try await Product.products(for: applicableProductIdentifiers) diff --git a/Sources/Subscription/StoreSubscriptionConfiguration.swift b/Sources/Subscription/StoreSubscriptionConfiguration.swift index 79fd30d16..5c48ec7b6 100644 --- a/Sources/Subscription/StoreSubscriptionConfiguration.swift +++ b/Sources/Subscription/StoreSubscriptionConfiguration.swift @@ -89,7 +89,7 @@ struct StoreSubscriptionDefinition { var name: String var appIdentifier: String var environment: SubscriptionEnvironment.ServiceEnvironment - var identifiersByCountries: [CountryCodeSet: [String]] + var identifiersByCountries: [SubscriptionRegion: [String]] func allIdentifiers() -> [String] { identifiersByCountries.values.flatMap { $0 } @@ -100,7 +100,7 @@ struct StoreSubscriptionDefinition { } } -enum CountryCodeSet { +public enum SubscriptionRegion: CaseIterable { case usa case restOfWorld @@ -116,4 +116,8 @@ enum CountryCodeSet { func contains(_ country: String) -> Bool { countryCodes.contains(country.uppercased()) } + + static func matchingRegion(for countryCode: String) -> Self? { + Self.allCases.first { $0.countryCodes.contains(countryCode) } + } }