Sources/Orgstar/StoreViews.swift
207 lines · 8522 bytes
1#if APPSTORE
2import OrgApp
3import StoreKit
4import SwiftUI
5
6/// orgstar+ from the App Store: status, plans with their prices and renewal terms, restoring,
7/// managing a subscription, and tips.
8struct StoreForm: View {
9 /// The feature that led here, if any.
10 var feature: ProFeature?
11 /// Whether to show the tips section, which the paywall leaves out.
12 var showsTips = true
13 private var store: ProStore { .shared }
14 private var access: ProAccess { .shared }
15
16 static let termsURL = URL(string: "https://www.apple.com/legal/internet-services/itunes/dev/stdeula/")!
17 static let privacyURL = URL(string: "https://krz.sh/privacy/orgstar/")!
18 static let subscriptionsURL = URL(string: "https://apps.apple.com/account/subscriptions")!
19
20 var body: some View {
21 Form {
22 Section {
23 HStack(spacing: 12) {
24 Image(systemName: "star.fill")
25 .font(.title2)
26 .foregroundStyle(.white)
27 .frame(width: 40, height: 40)
28 .background(.orange.gradient, in: .rect(cornerRadius: 9))
29 .accessibilityHidden(true)
30 VStack(alignment: .leading, spacing: 2) {
31 if let feature, !access.isUnlocked {
32 Text("\(feature.storeTitle) needs orgstar+.").font(.headline)
33 } else {
34 Text(verbatim: "orgstar+").font(.headline)
35 }
36 Text(status).foregroundStyle(.secondary)
37 }
38 }
39 }
40 Section {
41 ForEach(ProFeature.macFeatures) { feature in
42 Label(feature.storeTitle, systemImage: feature.systemImage)
43 }
44 } header: {
45 Text("Included in orgstar+")
46 } footer: {
47 Text("Editing, the agenda, capture, clocking, tables and search stay free.").settingsFooter()
48 }
49 if store.activeProductID != ProStore.lifetime {
50 Section {
51 if store.products.isEmpty {
52 ProgressView().frame(maxWidth: .infinity)
53 }
54 ForEach(store.products) { product in plan(product) }
55 } header: {
56 Text("Choose a plan")
57 } footer: {
58 Text("Subscriptions renew automatically until cancelled in your Apple Account settings, at least 24 hours before the period ends.")
59 .settingsFooter()
60 }
61 }
62 Section {
63 HStack {
64 Button("Restore Purchases") { Task { await store.restore() } }.disabled(store.busy)
65 if store.activeProductID == ProStore.monthly || store.activeProductID == ProStore.yearly {
66 Link("Manage Subscription…", destination: Self.subscriptionsURL)
67 }
68 Spacer()
69 Link("Terms of Use", destination: Self.termsURL)
70 Link("Privacy Policy", destination: Self.privacyURL)
71 }
72 if let message = store.message {
73 Text(message).foregroundStyle(.secondary)
74 }
75 }
76 if showsTips {
77 StoreTipSection()
78 }
79 }
80 .formStyle(.grouped)
81 .task { await store.loadProducts() }
82 }
83
84 /// One plan: its name, an optional savings tag, what it bills, and a button with its price.
85 private func plan(_ product: Product) -> some View {
86 LabeledContent {
87 Button(price(product)) { Task { await store.buy(product) } }
88 .monospacedDigit()
89 .disabled(store.busy || product.id == store.activeProductID)
90 } label: {
91 HStack(spacing: 6) {
92 Text(product.displayName)
93 if let percent = savings(product) {
94 Text("Save \(percent)%")
95 .font(.caption.bold())
96 .foregroundStyle(.white)
97 .padding(.horizontal, 6)
98 .padding(.vertical, 1)
99 .background(.orange.gradient, in: .capsule)
100 }
101 }
102 Text(detail(product))
103 }
104 }
105
106 private func detail(_ product: Product) -> String {
107 if product.id == store.activeProductID { return String(localized: "Current plan") }
108 switch product.id {
109 case ProStore.monthly: return String(localized: "Billed monthly")
110 case ProStore.yearly:
111 let perMonth = (product.price / 12).formatted(product.priceFormatStyle)
112 return String(localized: "\(perMonth) a month, billed yearly")
113 default: return String(localized: "One payment, no subscription")
114 }
115 }
116
117 /// The price with its billing period.
118 private func price(_ product: Product) -> String {
119 switch product.subscription?.subscriptionPeriod.unit {
120 case .month: String(localized: "\(product.displayPrice) / month")
121 case .year: String(localized: "\(product.displayPrice) / year")
122 default: product.displayPrice
123 }
124 }
125
126 private func savings(_ product: Product) -> Int? {
127 guard product.id == ProStore.yearly,
128 let monthly = store.products.first(where: { $0.id == ProStore.monthly })
129 else { return nil }
130 return ProStore.yearlySavings(yearly: product.price, monthly: monthly.price)
131 }
132
133 private var status: String {
134 guard access.isUnlocked else { return String(localized: "Power features and Apple integrations, on top of everything that stays free.") }
135 return store.activeProductID == ProStore.lifetime
136 ? String(localized: "You own orgstar+. Thank you for supporting orgstar.")
137 : String(localized: "Your orgstar+ subscription is active. Thank you for supporting orgstar.")
138 }
139}
140
141/// Tips, for everyone, with or without orgstar+.
142struct StoreTipSection: View {
143 @State private var store = TipStore()
144
145 var body: some View {
146 Section {
147 if store.isLoading && store.products.isEmpty {
148 ProgressView().frame(maxWidth: .infinity)
149 }
150 ForEach(store.products) { product in
151 LabeledContent(product.displayName) {
152 if store.purchasingProductID == product.id {
153 ProgressView().controlSize(.small)
154 } else {
155 Button(product.displayPrice) { Task { await store.purchase(product) } }
156 .monospacedDigit()
157 .disabled(store.purchasingProductID != nil)
158 }
159 }
160 }
161 if let message = store.message {
162 Text(message).foregroundStyle(.secondary)
163 if store.products.count < Tip.productIDs.count {
164 Button("Try Again") { Task { await store.loadProducts() } }.disabled(store.isLoading)
165 }
166 }
167 } header: {
168 Text("Support Development")
169 } footer: {
170 Text("Tips are one-time purchases that help pay for orgstar's development. They don't unlock anything.")
171 .settingsFooter()
172 }
173 .task { await store.loadProducts() }
174 .alert("Thank You", isPresented: $store.thanked) {
175 Button("OK") {}
176 } message: {
177 Text("Your tip helps keep orgstar going.")
178 }
179 }
180}
181
182/// Shown when a Pro feature is used without Pro.
183struct StorePaywallSheet: View {
184 let feature: ProFeature
185 @Environment(\.dismiss) private var dismiss
186
187 var body: some View {
188 VStack(spacing: 0) {
189 StoreForm(feature: feature, showsTips: false)
190 HStack {
191 Spacer()
192 Button("Done") { dismiss() }.keyboardShortcut(.defaultAction)
193 }
194 .padding([.horizontal, .bottom], 20)
195 }
196 .frame(width: 520, height: 600)
197 .onChange(of: ProAccess.shared.isUnlocked) { _, unlocked in if unlocked { dismiss() } }
198 }
199}
200
201extension ProFeature {
202 /// The title in the App Store build, which runs Emacs Lisp blocks natively.
203 var storeTitle: String {
204 self == .babel ? String(localized: "Run Emacs Lisp blocks and tangle") : title
205 }
206}
207#endif