Sources/OrgstarMobile/ProScreen.swift

58f3b73e8ff337e5a15111fbb32c0d57cf2346da
orgstar/Sources/OrgstarMobile/ProScreen.swift history · blame · raw

178 lines · 7360 bytes

7 symbols in this file
  1#if os(iOS)
  2import OrgApp
  3import StoreKit
  4import SwiftUI
  5import UIKit
  6
  7/// orgstar+'s gold, for its banner in Settings and its screen.
  8extension ShapeStyle where Self == LinearGradient {
  9    static var plusGold: LinearGradient {
 10        LinearGradient(
 11            colors: [Color(red: 1.0, green: 0.84, blue: 0.36), Color(red: 0.86, green: 0.6, blue: 0.12)],
 12            startPoint: .topLeading, endPoint: .bottomTrailing)
 13    }
 14}
 15
 16/// The gold star that marks orgstar+.
 17struct PlusBadge: View {
 18    var size: CGFloat = 46
 19
 20    var body: some View {
 21        Image(systemName: "star.fill")
 22            .font(.system(size: size * 0.48, weight: .semibold))
 23            .foregroundStyle(.white)
 24            .frame(width: size, height: size)
 25            .background(.plusGold, in: .rect(cornerRadius: size * 0.24))
 26            .accessibilityHidden(true)
 27    }
 28}
 29
 30/// What orgstar+ includes, its prices, and buying, restoring or managing it.
 31struct ProScreen: View {
 32    /// The feature that led here, if any.
 33    var feature: ProFeature?
 34    @Environment(\.dismiss) private var dismiss
 35    @State private var managing = false
 36    private var store: ProStore { .shared }
 37    private var access: ProAccess { .shared }
 38
 39    var body: some View {
 40        Form {
 41            Section {
 42                VStack(spacing: 10) {
 43                    PlusBadge(size: 76)
 44                        .shadow(color: .orange.opacity(0.35), radius: 14, y: 6)
 45                    Text(verbatim: "orgstar+").font(.largeTitle.bold())
 46                    if let feature, !access.isUnlocked {
 47                        Text("\(feature.title) needs orgstar+.").font(.headline)
 48                    }
 49                    Text(status).foregroundStyle(.secondary)
 50                }
 51                .multilineTextAlignment(.center)
 52                .frame(maxWidth: .infinity)
 53                .padding(.vertical, 6)
 54            }
 55            .listRowBackground(Color.clear)
 56            Section {
 57                ForEach(ProFeature.allCases) { feature in
 58                    Label {
 59                        Text(feature.title)
 60                    } icon: {
 61                        Image(systemName: feature.systemImage).foregroundStyle(.plusGold)
 62                    }
 63                }
 64            } header: {
 65                Text("Included in orgstar+")
 66            } footer: {
 67                Text("Editing, the agenda, capture, clocking, tables and search stay free.")
 68            }
 69            if access.grant != .grandfathered, store.activeProductID != ProStore.lifetime {
 70                Section {
 71                    if store.products.isEmpty {
 72                        ProgressView().frame(maxWidth: .infinity)
 73                    }
 74                    ForEach(store.products) { product in
 75                        Button { Task { await store.buy(product) } } label: { plan(product) }
 76                            .tint(.primary)
 77                            .disabled(store.busy || product.id == store.activeProductID)
 78                    }
 79                } header: {
 80                    Text("Choose a plan")
 81                } footer: {
 82                    Text("Subscriptions renew automatically until cancelled in your Apple Account settings, at least 24 hours before the period ends.")
 83                }
 84            }
 85            Section {
 86                Button("Restore Purchases") { Task { await store.restore() } }.disabled(store.busy)
 87                if store.activeProductID == ProStore.monthly || store.activeProductID == ProStore.yearly {
 88                    Button("Manage Subscription") { managing = true }
 89                }
 90                Link("Terms of Use", destination: URL(string: "https://www.apple.com/legal/internet-services/itunes/dev/stdeula/")!)
 91                Link("Privacy Policy", destination: URL(string: "https://krz.sh/privacy/orgstar/")!)
 92            }
 93            if let message = store.message {
 94                Section { Text(message).foregroundStyle(.secondary) }
 95            }
 96        }
 97        .navigationTitle(Text(verbatim: "orgstar+"))
 98        .navigationBarTitleDisplayMode(.inline)
 99        .manageSubscriptionsSheet(isPresented: $managing)
100        .task { await store.loadProducts() }
101    }
102
103    /// One plan: its name, an optional savings tag, what it bills, and its price.
104    private func plan(_ product: Product) -> some View {
105        HStack(alignment: .firstTextBaseline) {
106            VStack(alignment: .leading, spacing: 3) {
107                HStack(spacing: 6) {
108                    Text(product.displayName).font(.headline)
109                    if let percent = savings(product) {
110                        Text("Save \(percent)%")
111                            .font(.caption.bold())
112                            .foregroundStyle(.white)
113                            .padding(.horizontal, 7)
114                            .padding(.vertical, 2)
115                            .background(.plusGold, in: .capsule)
116                    }
117                }
118                detail(product).font(.subheadline).foregroundStyle(.secondary)
119            }
120            Spacer()
121            Text(product.displayPrice + period(product))
122                .font(.headline)
123                .monospacedDigit()
124                .foregroundStyle(Color.accentColor)
125        }
126        .padding(.vertical, 4)
127    }
128
129    private func detail(_ product: Product) -> Text {
130        if product.id == store.activeProductID { return Text("Current plan") }
131        switch product.id {
132        case ProStore.monthly: return Text("Billed monthly")
133        case ProStore.yearly:
134            let perMonth = (product.price / 12).formatted(product.priceFormatStyle)
135            return Text("\(perMonth) a month, billed yearly")
136        default: return Text("One payment, no subscription")
137        }
138    }
139
140    /// How much the yearly plan saves against twelve months, in whole percent.
141    private func savings(_ product: Product) -> Int? {
142        guard product.id == ProStore.yearly,
143              let monthly = store.products.first(where: { $0.id == ProStore.monthly })
144        else { return nil }
145        return ProStore.yearlySavings(yearly: product.price, monthly: monthly.price)
146    }
147
148    private var status: LocalizedStringKey {
149        switch access.grant {
150        case .grandfathered: "Included, because you downloaded orgstar before orgstar+ existed. Thank you."
151        case .purchase: store.activeProductID == ProStore.lifetime ? "You own orgstar+. Thank you for supporting orgstar." : "Your orgstar+ subscription is active. Thank you for supporting orgstar."
152        case .license, nil: "Power features and Apple integrations, on top of everything that stays free."
153        }
154    }
155
156    private func period(_ product: Product) -> String {
157        switch product.subscription?.subscriptionPeriod.unit {
158        case .month: " / month"
159        case .year: " / year"
160        default: ""
161        }
162    }
163}
164
165/// The paywall as a sheet, with a close button.
166struct ProSheet: View {
167    let feature: ProFeature
168    @Environment(\.dismiss) private var dismiss
169
170    var body: some View {
171        NavigationStack {
172            ProScreen(feature: feature)
173                .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Close") { dismiss() } } }
174        }
175        .onChange(of: ProAccess.shared.isUnlocked) { _, unlocked in if unlocked { dismiss() } }
176    }
177}
178#endif