| @@ -3,41 +3,180 @@ import OrgApp |
| 3 | 3 | import StoreKit |
| 4 | 4 | import SwiftUI |
| 5 | 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. |
| 6 | /// orgstar+ from the App Store: status, plans with their prices and renewal terms, restoring, |
| 7 | /// managing a subscription, and tips. |
| 10 | 8 | struct 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 |
| 11 | 13 | private var store: ProStore { .shared } |
| 12 | 14 | private var access: ProAccess { .shared } |
| 13 | 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 | |
| 14 | 20 | var body: some View { |
| 15 | 21 | Form { |
| 16 | 22 | 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) |
| 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) |
| 25 | 35 | } |
| 36 | Text(status).foregroundStyle(.secondary) |
| 26 | 37 | } |
| 27 | | Button("Restore Purchases") { Task { await store.restore() } } |
| 28 | | .disabled(store.busy) |
| 29 | | if let message = store.message { Text(message).foregroundStyle(.secondary) } |
| 30 | 38 | } |
| 31 | 39 | } |
| 32 | | Section("Included in orgstar+") { |
| 40 | Section { |
| 33 | 41 | ForEach(ProFeature.macFeatures) { feature in |
| 34 | | Label(feature.title, systemImage: feature.systemImage) |
| 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) |
| 35 | 74 | } |
| 36 | 75 | } |
| 76 | if showsTips { |
| 77 | StoreTipSection() |
| 78 | } |
| 37 | 79 | } |
| 38 | 80 | .formStyle(.grouped) |
| 39 | 81 | .task { await store.loadProducts() } |
| 40 | 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+. |
| 142 | struct 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 | } |
| 41 | 180 | } |
| 42 | 181 | |
| 43 | 182 | /// Shown when a Pro feature is used without Pro. |
| @@ -46,19 +185,23 @@ struct StorePaywallSheet: View { |
| 46 | 185 | @Environment(\.dismiss) private var dismiss |
| 47 | 186 | |
| 48 | 187 | 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() |
| 188 | VStack(spacing: 0) { |
| 189 | StoreForm(feature: feature, showsTips: false) |
| 54 | 190 | HStack { |
| 55 | 191 | Spacer() |
| 56 | 192 | Button("Done") { dismiss() }.keyboardShortcut(.defaultAction) |
| 57 | 193 | } |
| 58 | 194 | .padding([.horizontal, .bottom], 20) |
| 59 | 195 | } |
| 60 | | .frame(width: 480, height: 520) |
| 196 | .frame(width: 520, height: 600) |
| 61 | 197 | .onChange(of: ProAccess.shared.isUnlocked) { _, unlocked in if unlocked { dismiss() } } |
| 62 | 198 | } |
| 63 | 199 | } |
| 200 | |
| 201 | extension 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 | } |
| 64 | 207 | #endif |