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:
Millian Lamiaux
2026-04-21 23:00:45 +02:00
parent 877f836f19
commit 9f15ae2d79
3 changed files with 15 additions and 1 deletions

View File

@@ -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") {}

View File

@@ -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 {