diff --git a/PadelClubData/Subscriptions/Guard.swift b/PadelClubData/Subscriptions/Guard.swift index aa77c06..2ff17e7 100644 --- a/PadelClubData/Subscriptions/Guard.swift +++ b/PadelClubData/Subscriptions/Guard.swift @@ -30,11 +30,7 @@ import Combine self.updateListenerTask = self.listenForTransactions() Task { - do { - try await self.refreshPurchasedAppleProducts() - } catch { - Logger.error(error) - } + await self.refreshPurchases() Logger.log("plan = \(String(describing: currentBestPurchase?.productId))") } @@ -66,19 +62,46 @@ import Combine 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. for await verificationResult in Transaction.currentEntitlements { - 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) + 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) } - 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 { return Task(priority: .background) { //Iterate through any transactions which didn't come from a direct call to `purchase()`.