Commit 9073748013

90737480134a7038f10e7a3be3d30468eb3f8cde

parent: 23c126fd8a

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-20 05:31 UTC

Add the Mac App Store orgstar+ and tips UI

- StoreForm: status, plans with per-period prices, savings and renewal
  terms, Restore, Manage Subscription, Terms of Use and Privacy Policy
- StoreTipSection: tips in the orgstar+ settings tab
- StorePaywallSheet: the paywall, without tips
- Emacs Lisp wording for the Run and tangle feature in the App Store build
- ProStore.yearlySavings shared with the iOS screen, with tests

Layout: unified · split

Sources/OrgApp/ProStore.swift +9
@@ -24,6 +24,15 @@ public final class ProStore {
2424 #endif
2525 }()
2626
27 /// How much a yearly price saves against twelve monthly ones, in whole percent, or nil when
28 /// it saves nothing.
29 public nonisolated static func yearlySavings(yearly: Decimal, monthly: Decimal) -> Int? {
30 guard monthly > 0 else { return nil }
31 let ratio = NSDecimalNumber(decimal: yearly / (monthly * 12)).doubleValue
32 let percent = Int((1 - ratio) * 100)
33 return percent > 0 ? percent : nil
34 }
35
2736 public private(set) var products: [Product] = []
2837 public private(set) var activeProductID: String?
2938 public private(set) var busy = false
Sources/Orgstar/StoreViews.swift +166 −23
@@ -3,41 +3,180 @@ import OrgApp
33import StoreKit
44import SwiftUI
55
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.
108struct 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
1113 private var store: ProStore { .shared }
1214 private var access: ProAccess { .shared }
1315
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
1420 var body: some View {
1521 Form {
1622 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)
2535 }
36 Text(status).foregroundStyle(.secondary)
2637 }
27 Button("Restore Purchases") { Task { await store.restore() } }
28 .disabled(store.busy)
29 if let message = store.message { Text(message).foregroundStyle(.secondary) }
3038 }
3139 }
32 Section("Included in orgstar+") {
40 Section {
3341 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)
3574 }
3675 }
76 if showsTips {
77 StoreTipSection()
78 }
3779 }
3880 .formStyle(.grouped)
3981 .task { await store.loadProducts() }
4082 }
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 }
41180}
42181
43182/// Shown when a Pro feature is used without Pro.
@@ -46,19 +185,23 @@ struct StorePaywallSheet: View {
46185 @Environment(\.dismiss) private var dismiss
47186
48187 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)
54190 HStack {
55191 Spacer()
56192 Button("Done") { dismiss() }.keyboardShortcut(.defaultAction)
57193 }
58194 .padding([.horizontal, .bottom], 20)
59195 }
60 .frame(width: 480, height: 520)
196 .frame(width: 520, height: 600)
61197 .onChange(of: ProAccess.shared.isUnlocked) { _, unlocked in if unlocked { dismiss() } }
62198 }
63199}
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}
64207#endif
Sources/OrgstarMobile/ProScreen.swift +2 −4
@@ -140,11 +140,9 @@ struct ProScreen: View {
140140 /// How much the yearly plan saves against twelve months, in whole percent.
141141 private func savings(_ product: Product) -> Int? {
142142 guard product.id == ProStore.yearly,
143 let monthly = store.products.first(where: { $0.id == ProStore.monthly }), monthly.price > 0
143 let monthly = store.products.first(where: { $0.id == ProStore.monthly })
144144 else { return nil }
145 let ratio = NSDecimalNumber(decimal: product.price / (monthly.price * 12)).doubleValue
146 let percent = Int((1 - ratio) * 100)
147 return percent > 0 ? percent : nil
145 return ProStore.yearlySavings(yearly: product.price, monthly: monthly.price)
148146 }
149147
150148 private var status: LocalizedStringKey {
Tests/OrgAppTests/TipTests.swift +8
@@ -1,3 +1,4 @@
1import Foundation
12import Testing
23@testable import OrgApp
34
@@ -35,5 +36,12 @@ struct ProStoreTests {
3536 #expect(!ProStore.grandfathers)
3637 #endif
3738 }
39
40 @Test func yearlySavingsAreWholePercentAgainstTwelveMonths() {
41 #expect(ProStore.yearlySavings(yearly: Decimal(string: "39.99")!, monthly: Decimal(string: "4.99")!) == 33)
42 #expect(ProStore.yearlySavings(yearly: 60, monthly: 5) == nil)
43 #expect(ProStore.yearlySavings(yearly: 70, monthly: 5) == nil)
44 #expect(ProStore.yearlySavings(yearly: 10, monthly: 0) == nil)
45 }
3846}
3947#endif