|
|
@@ -11,6 +11,7 @@ enum PremiumProductID: String, CaseIterable {
|
|
|
@MainActor
|
|
|
final class PremiumStore: ObservableObject {
|
|
|
static let shared = PremiumStore()
|
|
|
+ private static let localPremiumUnlockKey = "premiumUnlockedLocal"
|
|
|
|
|
|
@Published private(set) var products: [Product] = []
|
|
|
@Published private(set) var isPremiumUnlocked = false
|
|
|
@@ -22,6 +23,8 @@ final class PremiumStore: ObservableObject {
|
|
|
private var transactionListener: Task<Void, Never>?
|
|
|
|
|
|
private init() {
|
|
|
+ // Keep UI in sync immediately after a successful purchase.
|
|
|
+ isPremiumUnlocked = UserDefaults.standard.bool(forKey: Self.localPremiumUnlockKey)
|
|
|
transactionListener = Task { await listenForTransactions() }
|
|
|
Task { await refreshEntitlements() }
|
|
|
}
|
|
|
@@ -76,6 +79,10 @@ final class PremiumStore: ObservableObject {
|
|
|
case .success(let verification):
|
|
|
switch verification {
|
|
|
case .verified(let transaction):
|
|
|
+ if PremiumProductID(rawValue: transaction.productID) != nil {
|
|
|
+ isPremiumUnlocked = true
|
|
|
+ UserDefaults.standard.set(true, forKey: Self.localPremiumUnlockKey)
|
|
|
+ }
|
|
|
await transaction.finish()
|
|
|
await refreshEntitlements()
|
|
|
return true
|
|
|
@@ -119,7 +126,12 @@ final class PremiumStore: ObservableObject {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- isPremiumUnlocked = unlocked
|
|
|
+ let cachedUnlock = UserDefaults.standard.bool(forKey: Self.localPremiumUnlockKey)
|
|
|
+ let finalUnlock = unlocked || cachedUnlock
|
|
|
+ isPremiumUnlocked = finalUnlock
|
|
|
+ if finalUnlock {
|
|
|
+ UserDefaults.standard.set(true, forKey: Self.localPremiumUnlockKey)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private func listenForTransactions() async {
|