From 738d90f27f2ba87866e14d6306d99976d2aec76d Mon Sep 17 00:00:00 2001 From: lumaa-dev Date: Fri, 27 Sep 2024 15:07:12 +0200 Subject: [PATCH] Better handling ShopView --- Bubble.xcodeproj/project.pbxproj | 12 +++--- Bubble/Views/Settings/ShopView.swift | 55 +++++++++++++++------------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/Bubble.xcodeproj/project.pbxproj b/Bubble.xcodeproj/project.pbxproj index d03e86f..ab44ad9 100644 --- a/Bubble.xcodeproj/project.pbxproj +++ b/Bubble.xcodeproj/project.pbxproj @@ -1129,7 +1129,7 @@ ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground; CODE_SIGN_ENTITLEMENTS = BubbleWidgets/BubbleWidgetsExtension.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 305; + CURRENT_PROJECT_VERSION = 307; DEVELOPMENT_TEAM = HB5P3BML86; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = BubbleWidgets/Info.plist; @@ -1164,7 +1164,7 @@ ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground; CODE_SIGN_ENTITLEMENTS = BubbleWidgets/BubbleWidgetsExtension.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 305; + CURRENT_PROJECT_VERSION = 307; DEVELOPMENT_TEAM = HB5P3BML86; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = BubbleWidgets/Info.plist; @@ -1327,7 +1327,7 @@ ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = YES; CODE_SIGN_ENTITLEMENTS = Bubble/Bubble.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 305; + CURRENT_PROJECT_VERSION = 307; DEVELOPMENT_ASSET_PATHS = ""; DEVELOPMENT_TEAM = HB5P3BML86; ENABLE_MODULE_VERIFIER = NO; @@ -1370,7 +1370,7 @@ ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = YES; CODE_SIGN_ENTITLEMENTS = Bubble/Bubble.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 305; + CURRENT_PROJECT_VERSION = 307; DEVELOPMENT_ASSET_PATHS = ""; DEVELOPMENT_TEAM = HB5P3BML86; ENABLE_MODULE_VERIFIER = NO; @@ -1407,7 +1407,7 @@ isa = XCBuildConfiguration; buildSettings = { CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 305; + CURRENT_PROJECT_VERSION = 307; DEVELOPMENT_TEAM = HB5P3BML86; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = AuthService/Info.plist; @@ -1437,7 +1437,7 @@ isa = XCBuildConfiguration; buildSettings = { CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 305; + CURRENT_PROJECT_VERSION = 307; DEVELOPMENT_TEAM = HB5P3BML86; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = AuthService/Info.plist; diff --git a/Bubble/Views/Settings/ShopView.swift b/Bubble/Views/Settings/ShopView.swift index 8c83d73..2ba4aa1 100644 --- a/Bubble/Views/Settings/ShopView.swift +++ b/Bubble/Views/Settings/ShopView.swift @@ -13,9 +13,7 @@ public struct ShopView: View { @State private var purchaseError: Bool = false @State private var hasSub: Bool = false - private var canPay: Bool { - return true - } + @State private var canPay: Bool = true public var body: some View { VStack { @@ -59,7 +57,7 @@ public struct ShopView: View { Button { // showLifetime.toggle() - purchase(entitlement: .lifetime) + PremiumFeature.purchase(entitlement: .lifetime) } label: { Text("shop.bubble-plus.lifetime") } @@ -115,6 +113,13 @@ public struct ShopView: View { } } .task { + Purchases.shared.getOfferings { offerings, err in + if let err { + self.canPay = false + dismiss() + } + } + AppDelegate.hasPlus { subscribed in self.hasSub = subscribed } @@ -271,7 +276,7 @@ extension ShopView { Spacer() Button { - purchase(entitlement: selectedPlan.getEntitlement()) + PremiumFeature.purchase(entitlement: selectedPlan.getEntitlement()) } label: { Text("shop.bubble-plus.subscribe") } @@ -425,6 +430,26 @@ extension ShopView { self.description = description self.systemImage = systemImage } + + static func hasActuallyPlus(customerInfo: CustomerInfo?) -> Bool { + return customerInfo?.entitlements[PlusEntitlements.lifetime.getEntitlementId()]?.isActive == true || customerInfo?.entitlements[PlusEntitlements.monthly.getEntitlementId()]?.isActive == true || customerInfo?.entitlements[PlusEntitlements.yearly.getEntitlementId()]?.isActive == true + } + + static func purchase(entitlement: PlusEntitlements) { + Purchases.shared.getOfferings { (offerings, error) in + if let product = entitlement.toPackage(offerings: offerings) { + Purchases.shared.purchase(package: product) { (transaction, customerInfo, error, userCancelled) in + if hasActuallyPlus(customerInfo: customerInfo) { + print("BOUGHT PLUS") + AppDelegate.premium = true + } + } + } + if let e = error { + print(e) + } + } + } } } @@ -466,23 +491,3 @@ enum PlusEntitlements: String { } } } - -private func hasActuallyPlus(customerInfo: CustomerInfo?) -> Bool { - return customerInfo?.entitlements[PlusEntitlements.lifetime.getEntitlementId()]?.isActive == true || customerInfo?.entitlements[PlusEntitlements.monthly.getEntitlementId()]?.isActive == true || customerInfo?.entitlements[PlusEntitlements.yearly.getEntitlementId()]?.isActive == true -} - -private func purchase(entitlement: PlusEntitlements) { - Purchases.shared.getOfferings { (offerings, error) in - if let product = entitlement.toPackage(offerings: offerings) { - Purchases.shared.purchase(package: product) { (transaction, customerInfo, error, userCancelled) in - if hasActuallyPlus(customerInfo: customerInfo) { - print("BOUGHT PLUS") - AppDelegate.premium = true - } - } - } - if let e = error { - print(e) - } - } -}