From 6468912fbe0aed8e604fa266d1a2f0a15c9e1933 Mon Sep 17 00:00:00 2001 From: Jorge Bernal Date: Mon, 7 Nov 2022 12:49:54 +0100 Subject: [PATCH 1/2] Disable IAP purchase while purchasing --- .../ViewRelated/InAppPurchases/InAppPurchasesDebugView.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/WooCommerce/Classes/ViewRelated/InAppPurchases/InAppPurchasesDebugView.swift b/WooCommerce/Classes/ViewRelated/InAppPurchases/InAppPurchasesDebugView.swift index 5cf43277f30..12b8f7e303a 100644 --- a/WooCommerce/Classes/ViewRelated/InAppPurchases/InAppPurchasesDebugView.swift +++ b/WooCommerce/Classes/ViewRelated/InAppPurchases/InAppPurchasesDebugView.swift @@ -9,6 +9,7 @@ struct InAppPurchasesDebugView: View { @State var products: [WPComPlanProduct] = [] @State var entitledProductIDs: [String] = [] @State var inAppPurchasesAreSupported = true + @State var isPurchasing = false var body: some View { List { @@ -22,11 +23,15 @@ struct InAppPurchasesDebugView: View { Section("Products") { if products.isEmpty { Text("No products") + } else if isPurchasing { + ActivityIndicator(isAnimating: .constant(true), style: .medium) } else { ForEach(products, id: \.id) { product in Button(entitledProductIDs.contains(product.id) ? "Entitled: \(product.description)" : product.description) { Task { + isPurchasing = true try? await inAppPurchasesForWPComPlansManager.purchaseProduct(with: product.id, for: siteID) + isPurchasing = false } } } From 982a1ac65e0879dd2a69527a2b8945e5171ea857 Mon Sep 17 00:00:00 2001 From: Jorge Bernal Date: Mon, 7 Nov 2022 12:50:13 +0100 Subject: [PATCH 2/2] Reload entitlements after purchase and coming from background --- .../InAppPurchases/InAppPurchasesDebugView.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/WooCommerce/Classes/ViewRelated/InAppPurchases/InAppPurchasesDebugView.swift b/WooCommerce/Classes/ViewRelated/InAppPurchases/InAppPurchasesDebugView.swift index 12b8f7e303a..ce9e20f0cb7 100644 --- a/WooCommerce/Classes/ViewRelated/InAppPurchases/InAppPurchasesDebugView.swift +++ b/WooCommerce/Classes/ViewRelated/InAppPurchases/InAppPurchasesDebugView.swift @@ -7,7 +7,7 @@ struct InAppPurchasesDebugView: View { let siteID: Int64 private let inAppPurchasesForWPComPlansManager = InAppPurchasesForWPComPlansManager() @State var products: [WPComPlanProduct] = [] - @State var entitledProductIDs: [String] = [] + @State var entitledProductIDs: Set = [] @State var inAppPurchasesAreSupported = true @State var isPurchasing = false @@ -31,6 +31,7 @@ struct InAppPurchasesDebugView: View { Task { isPurchasing = true try? await inAppPurchasesForWPComPlansManager.purchaseProduct(with: product.id, for: siteID) + await loadUserEntitlements() isPurchasing = false } } @@ -55,6 +56,11 @@ struct InAppPurchasesDebugView: View { .task { await loadProducts() } + .onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in + Task { + await loadUserEntitlements() + } + } } private func loadProducts() async { @@ -76,7 +82,9 @@ struct InAppPurchasesDebugView: View { do { for product in self.products { if try await inAppPurchasesForWPComPlansManager.userIsEntitledToProduct(with: product.id) { - self.entitledProductIDs.append(product.id) + self.entitledProductIDs.insert(product.id) + } else { + self.entitledProductIDs.remove(product.id) } } }