Sources/Orgstar/StoreViews.swift
64 lines · 2370 bytes
1#if APPSTORE
2import OrgApp
3import StoreKit
4import SwiftUI
5
6// TODO(M1.6): the full Mac StoreKit UI (plans with per-period prices and renewal terms,
7// Manage Subscription, Terms and Privacy, Tips) replaces these two views, keeping their names.
8
9/// orgstar+ from the App Store: status, plans, and restoring.
10struct StoreForm: View {
11 private var store: ProStore { .shared }
12 private var access: ProAccess { .shared }
13
14 var body: some View {
15 Form {
16 Section {
17 LabeledContent("orgstar+", value: access.isUnlocked ? String(localized: "Unlocked") : String(localized: "Not purchased"))
18 }
19 if !access.isUnlocked {
20 Section {
21 ForEach(store.products, id: \.id) { product in
22 LabeledContent(product.displayName) {
23 Button(product.displayPrice) { Task { await store.buy(product) } }
24 .disabled(store.busy)
25 }
26 }
27 Button("Restore Purchases") { Task { await store.restore() } }
28 .disabled(store.busy)
29 if let message = store.message { Text(message).foregroundStyle(.secondary) }
30 }
31 }
32 Section("Included in orgstar+") {
33 ForEach(ProFeature.macFeatures) { feature in
34 Label(feature.title, systemImage: feature.systemImage)
35 }
36 }
37 }
38 .formStyle(.grouped)
39 .task { await store.loadProducts() }
40 }
41}
42
43/// Shown when a Pro feature is used without Pro.
44struct StorePaywallSheet: View {
45 let feature: ProFeature
46 @Environment(\.dismiss) private var dismiss
47
48 var body: some View {
49 VStack(alignment: .leading, spacing: 0) {
50 Label("\(feature.title) needs orgstar+", systemImage: feature.systemImage)
51 .font(.headline)
52 .padding([.horizontal, .top], 20)
53 StoreForm()
54 HStack {
55 Spacer()
56 Button("Done") { dismiss() }.keyboardShortcut(.defaultAction)
57 }
58 .padding([.horizontal, .bottom], 20)
59 }
60 .frame(width: 480, height: 520)
61 .onChange(of: ProAccess.shared.isUnlocked) { _, unlocked in if unlocked { dismiss() } }
62 }
63}
64#endif