minor improvements to Guard

sync3
Laurent 2 months ago
parent 9ed8e56c2e
commit e3b3c6456c
  1. 45
      PadelClubData/Subscriptions/Guard.swift

@ -30,11 +30,7 @@ import Combine
self.updateListenerTask = self.listenForTransactions() self.updateListenerTask = self.listenForTransactions()
Task { Task {
do { await self.refreshPurchases()
try await self.refreshPurchasedAppleProducts()
} catch {
Logger.error(error)
}
Logger.log("plan = \(String(describing: currentBestPurchase?.productId))") Logger.log("plan = \(String(describing: currentBestPurchase?.productId))")
} }
@ -66,19 +62,46 @@ import Combine
return productIds return productIds
} }
public func refreshPurchasedAppleProducts() async throws { public func refreshPurchases() async {
await _refreshUnfinishedTransactions()
await _refreshPurchasedAppleProducts()
}
fileprivate func _refreshPurchasedAppleProducts() async {
// Iterate through the user's purchased products. // Iterate through the user's purchased products.
for await verificationResult in Transaction.currentEntitlements { for await verificationResult in Transaction.currentEntitlements {
let transaction = try await self.processTransactionResult(verificationResult) do {
print("processs product id = \(transaction.productID)") let transaction = try await self.processTransactionResult(verificationResult)
DispatchQueue.main.async { print("processs product id = \(transaction.productID)")
NotificationCenter.default.post(name: Notification.Name.StoreEventHappened, object: nil) DispatchQueue.main.async {
NotificationCenter.default.post(name: Notification.Name.StoreEventHappened, object: nil)
}
await transaction.finish()
} catch {
Logger.error(error)
} }
await transaction.finish()
} }
} }
public func _refreshUnfinishedTransactions() async {
// Iterate through the user's purchased products.
for await verificationResult in Transaction.unfinished {
do {
let transaction = try await self.processTransactionResult(verificationResult)
print("processs product id = \(transaction.productID)")
DispatchQueue.main.async {
NotificationCenter.default.post(name: Notification.Name.StoreEventHappened, object: nil)
}
await transaction.finish()
} catch {
Logger.error(error)
}
}
}
func listenForTransactions() -> Task<Void, Never> { func listenForTransactions() -> Task<Void, Never> {
return Task(priority: .background) { return Task(priority: .background) {
//Iterate through any transactions which didn't come from a direct call to `purchase()`. //Iterate through any transactions which didn't come from a direct call to `purchase()`.

Loading…
Cancel
Save