fix: dismiss paywall and sync premium state after successful purchase
- Add purchaseSucceeded flag to PurchaseViewModel, set on purchase success - PaywallView observes the flag and dismisses itself automatically - ProfileTab.syncSubscription() writes PurchaseService.currentPlan back to UserProfile.subscriptionRaw via SwiftData on sheet dismiss
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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") {}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user