Sources/OrgstarMobile/ProScreen.swift

1e2be994af63ee234fff2fe60543c62ca3f6905e
orgstar/Sources/OrgstarMobile/ProScreen.swift

185 lines · 7979 bytes

  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 showsPlans {
 70                Section {
 71                    if store.isLoading && store.products.isEmpty {
 72                        ProgressView().frame(maxWidth: .infinity)
 73                    } else if store.products.count < ProStore.productIDs.count, let message = store.message {
 74                        Text(message).foregroundStyle(.secondary)
 75                        Button("Try Again") { Task { await store.loadProducts() } }
 76                    }
 77                    ForEach(store.products) { product in
 78                        Button { Task { await store.buy(product) } } label: { plan(product) }
 79                            .tint(.primary)
 80                            .disabled(store.busy || product.id == store.activeProductID)
 81                    }
 82                } header: {
 83                    Text("Choose a Plan")
 84                } footer: {
 85                    Text("Subscriptions renew automatically until cancelled in your Apple Account settings, at least 24 hours before the period ends.")
 86                }
 87            }
 88            Section {
 89                Button("Restore Purchases") { Task { await store.restore() } }.disabled(store.busy)
 90                if store.activeProductID == ProStore.monthly || store.activeProductID == ProStore.yearly {
 91                    Button("Manage Subscription") { managing = true }
 92                }
 93                Link("Terms of Use", destination: URL(string: "https://www.apple.com/legal/internet-services/itunes/dev/stdeula/")!)
 94                Link("Privacy Policy", destination: URL(string: "https://krz.sh/privacy/orgstar/")!)
 95            }
 96            // A purchase's or restore's message; a failed load shows under the plans.
 97            if let message = store.message, !showsPlans || store.products.count == ProStore.productIDs.count {
 98                Section { Text(message).foregroundStyle(.secondary) }
 99            }
100        }
101        .navigationTitle(Text(verbatim: "orgstar+"))
102        .navigationBarTitleDisplayMode(.inline)
103        .manageSubscriptionsSheet(isPresented: $managing)
104        .task { await store.loadProducts() }
105    }
106
107    private var showsPlans: Bool { access.grant != .grandfathered && store.activeProductID != ProStore.lifetime }
108
109    /// One plan: its name, an optional savings tag, what it bills, and its price.
110    private func plan(_ product: Product) -> some View {
111        HStack(alignment: .firstTextBaseline) {
112            VStack(alignment: .leading, spacing: 3) {
113                HStack(spacing: 6) {
114                    Text(product.displayName).font(.headline)
115                    if let percent = savings(product) {
116                        Text("Save \(percent)%")
117                            .font(.caption.bold())
118                            .foregroundStyle(.white)
119                            .padding(.horizontal, 7)
120                            .padding(.vertical, 2)
121                            .background(.plusGold, in: .capsule)
122                    }
123                }
124                detail(product).font(.subheadline).foregroundStyle(.secondary)
125            }
126            Spacer()
127            Text(price(product))
128                .font(.headline)
129                .monospacedDigit()
130                .foregroundStyle(Color.accentColor)
131        }
132        .padding(.vertical, 4)
133    }
134
135    private func detail(_ product: Product) -> Text {
136        if product.id == store.activeProductID { return Text("Current plan") }
137        switch product.id {
138        case ProStore.monthly: return Text("Billed monthly")
139        case ProStore.yearly:
140            let perMonth = (product.price / 12).formatted(product.priceFormatStyle)
141            return Text("\(perMonth) a month, billed yearly")
142        default: return Text("One payment, no subscription")
143        }
144    }
145
146    /// How much the yearly plan saves against twelve months, in whole percent.
147    private func savings(_ product: Product) -> Int? {
148        guard product.id == ProStore.yearly,
149              let monthly = store.products.first(where: { $0.id == ProStore.monthly })
150        else { return nil }
151        return ProStore.yearlySavings(yearly: product.price, monthly: monthly.price)
152    }
153
154    private var status: LocalizedStringKey {
155        switch access.grant {
156        case .grandfathered: "Included, because you downloaded orgstar before orgstar+ existed. Thank you."
157        case .purchase: store.activeProductID == ProStore.lifetime ? "You own orgstar+. Thank you for supporting orgstar." : "Your orgstar+ subscription is active. Thank you for supporting orgstar."
158        case .license, nil: "Power features and Apple integrations, on top of everything that stays free."
159        }
160    }
161
162    /// The price, and the period a subscription bills for.
163    private func price(_ product: Product) -> String {
164        switch product.subscription?.subscriptionPeriod.unit {
165        case .month: String(localized: "\(product.displayPrice) / month")
166        case .year: String(localized: "\(product.displayPrice) / year")
167        default: product.displayPrice
168        }
169    }
170}
171
172/// The paywall as a sheet, with a close button.
173struct ProSheet: View {
174    let feature: ProFeature
175    @Environment(\.dismiss) private var dismiss
176
177    var body: some View {
178        NavigationStack {
179            ProScreen(feature: feature)
180                .toolbar { ToolbarItem(placement: .confirmationAction) { Button("Done") { dismiss() } } }
181        }
182        .onChange(of: ProAccess.shared.isUnlocked) { _, unlocked in if unlocked { dismiss() } }
183    }
184}
185#endif
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form