diff --git a/tabatago-swift/TabataGo/ViewModels/PurchaseViewModel.swift b/tabatago-swift/TabataGo/ViewModels/PurchaseViewModel.swift index 7171dc0..40babc1 100644 --- a/tabatago-swift/TabataGo/ViewModels/PurchaseViewModel.swift +++ b/tabatago-swift/TabataGo/ViewModels/PurchaseViewModel.swift @@ -7,6 +7,7 @@ final class PurchaseViewModel: ObservableObject { @Published var offerings: Offerings? = nil @Published var selectedPackage: Package? = nil @Published var isPurchasing = false + @Published var purchaseSucceeded = false @Published var showError = false @Published var errorMessage: String? = nil @@ -27,6 +28,7 @@ final class PurchaseViewModel: ObservableObject { do { try await service.purchase(package: package) AnalyticsService.shared.subscriptionStarted(plan: package.identifier) + purchaseSucceeded = true } catch { errorMessage = error.localizedDescription showError = true diff --git a/tabatago-swift/TabataGo/Views/Paywall/PaywallView.swift b/tabatago-swift/TabataGo/Views/Paywall/PaywallView.swift index 63ba9d1..f26948a 100644 --- a/tabatago-swift/TabataGo/Views/Paywall/PaywallView.swift +++ b/tabatago-swift/TabataGo/Views/Paywall/PaywallView.swift @@ -112,6 +112,9 @@ struct PaywallView: View { } } .task { await vm.loadOfferings() } + .onChange(of: vm.purchaseSucceeded) { _, succeeded in + if succeeded { dismiss() } + } .onAppear { AnalyticsService.shared.paywallViewed(source: "paywall_sheet") } .alert("Error", isPresented: $vm.showError) { Button("OK") {} diff --git a/tabatago-swift/TabataGo/Views/Tabs/ProfileTab.swift b/tabatago-swift/TabataGo/Views/Tabs/ProfileTab.swift index 9fd9d1f..e455e84 100644 --- a/tabatago-swift/TabataGo/Views/Tabs/ProfileTab.swift +++ b/tabatago-swift/TabataGo/Views/Tabs/ProfileTab.swift @@ -8,6 +8,7 @@ struct ProfileTab: View { @State private var showingPaywall = false @Environment(\.modelContext) private var context @StateObject private var purchaseVM = PurchaseViewModel() + private let purchaseService = PurchaseService.shared private var profile: UserProfile? { profiles.first } @@ -101,11 +102,19 @@ struct ProfileTab: View { } .navigationTitle("Profile") .navigationBarTitleDisplayMode(.large) - .sheet(isPresented: $showingPaywall) { + .sheet(isPresented: $showingPaywall, onDismiss: syncSubscription) { PaywallView() } } } + + private func syncSubscription() { + guard let profile else { return } + let newPlan = purchaseService.currentPlan + guard profile.subscription != newPlan else { return } + profile.subscriptionRaw = newPlan.rawValue + try? context.save() + } } struct ProfileRow: View {