struct MainView: View {
@Environment(Checkout.self) var checkout
@State private var showingPaywall = false
@State private var showingLogin = false
var body: some View {
NavigationStack {
Group {
if !checkout.isInitialized {
ProgressView("Loading...")
} else if checkout.hasAccess(to: "prod_xxxxxxxxxxxxxx") {
PremiumContentView()
} else {
FreeContentView(onUpgrade: { showingPaywall = true })
}
}
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Menu {
if let userId = checkout.appUserId {
Text("Signed in as \(userId)")
Button("Sign Out", action: signOut)
} else {
Button("Sign In") { showingLogin = true }
}
Divider()
if checkout.hasAccess(to: "prod_xxxxxxxxxxxxxx") {
Button("Manage Subscription") {
// Show subscription management
}
}
} label: {
Image(systemName: "person.circle")
}
}
}
.sheet(isPresented: $showingPaywall) {
PaywallView()
}
.sheet(isPresented: $showingLogin) {
LoginView { userId in
Task {
try? await checkout.logIn(appUserId: userId)
showingLogin = false
}
}
}
}
}
func signOut() {
checkout.logOut()
}
}
struct FreeContentView: View {
let onUpgrade: () -> Void
var body: some View {
VStack(spacing: 20) {
Text("Free Content")
.font(.title)
Text("Upgrade to access premium features")
.foregroundStyle(.secondary)
Button("Upgrade Now", action: onUpgrade)
.buttonStyle(.borderedProminent)
}
}
}
struct PremiumContentView: View {
var body: some View {
VStack {
Image(systemName: "crown.fill")
.font(.largeTitle)
.foregroundStyle(.yellow)
Text("Premium Content")
.font(.title)
Text("Thanks for subscribing!")
}
}
}