Sources/OrgstarMobile/TipStore.swift
102 lines · 3815 bytes
1#if os(iOS)
2import OrgApp
3import StoreKit
4import SwiftUI
5
6/// Tips from the App Store. `ProStore`'s transaction listener finishes any that arrive later.
7@MainActor @Observable
8final class TipStore {
9 private(set) var products: [Product] = []
10 private(set) var isLoading = false
11 private(set) var purchasingProductID: String?
12 /// A failure, or a tip waiting for approval, shown in the section.
13 private(set) var message: String?
14 /// Set after a tip goes through; the thank-you alert clears it.
15 var thanked = false
16
17 func loadProducts() async {
18 guard !isLoading else { return }
19 isLoading = true
20 message = nil
21 defer { isLoading = false }
22 do {
23 let fetched = try await Product.products(for: Tip.productIDs)
24 products = Tip.productIDs.compactMap { id in fetched.first { $0.id == id } }
25 if products.count < Tip.productIDs.count {
26 message = String(localized: "Some tips aren't available from the App Store right now.")
27 }
28 } catch {
29 products = []
30 message = String(localized: "The App Store can't be reached right now.")
31 }
32 }
33
34 func purchase(_ product: Product) async {
35 guard purchasingProductID == nil else { return }
36 purchasingProductID = product.id
37 message = nil
38 defer { purchasingProductID = nil }
39 do {
40 switch try await product.purchase() {
41 case .success(.verified(let transaction)):
42 await transaction.finish()
43 thanked = true
44 case .success(.unverified):
45 message = String(localized: "The App Store couldn't verify the purchase.")
46 case .pending:
47 message = String(localized: "The tip is waiting for approval.")
48 case .userCancelled:
49 break
50 @unknown default:
51 break
52 }
53 } catch {
54 message = String(localized: "The purchase didn't go through. \(error.localizedDescription)")
55 }
56 }
57}
58
59/// Tips in About, for everyone, with or without orgstar+.
60struct TipSection: View {
61 @State private var store = TipStore()
62
63 var body: some View {
64 Section {
65 if store.isLoading && store.products.isEmpty {
66 ProgressView().frame(maxWidth: .infinity)
67 }
68 ForEach(store.products) { product in
69 Button { Task { await store.purchase(product) } } label: {
70 HStack {
71 Text(product.displayName)
72 Spacer()
73 if store.purchasingProductID == product.id {
74 ProgressView().controlSize(.small)
75 } else {
76 Text(product.displayPrice).monospacedDigit().foregroundStyle(.secondary)
77 }
78 }
79 }
80 .tint(.primary)
81 .disabled(store.purchasingProductID != nil)
82 }
83 if let message = store.message {
84 Text(message).foregroundStyle(.secondary)
85 if store.products.count < Tip.productIDs.count {
86 Button("Try Again") { Task { await store.loadProducts() } }.disabled(store.isLoading)
87 }
88 }
89 } header: {
90 Text("Support Development")
91 } footer: {
92 Text("Tips are one-time purchases that help pay for orgstar's development. They don't unlock anything.")
93 }
94 .task { await store.loadProducts() }
95 .alert("Thank You", isPresented: $store.thanked) {
96 Button("OK") {}
97 } message: {
98 Text("Your tip helps keep orgstar going.")
99 }
100 }
101}
102#endif