Skip to content

Commit

Permalink
Expose current storefront region
Browse files Browse the repository at this point in the history
  • Loading branch information
miasma13 committed Nov 21, 2024
1 parent bc7be9e commit ef91ad5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Sources/Subscription/Managers/StorePurchaseManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Void, Never>?
private var storefrontChanges: Task<Void, Never>?

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions Sources/Subscription/StoreSubscriptionConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -100,7 +100,7 @@ struct StoreSubscriptionDefinition {
}
}

enum CountryCodeSet {
public enum SubscriptionRegion: CaseIterable {
case usa
case restOfWorld

Expand All @@ -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) }
}
}

0 comments on commit ef91ad5

Please sign in to comment.