Sources/Orgstar/ProLicense.swift

design-themes-widgets
orgstar/Sources/Orgstar/ProLicense.swift history · blame · raw

140 lines · 5941 bytes

  1import OrgApp
  2import SwiftUI
  3
  4#if !APPSTORE
  5/// Pro on the Mac: a license key for sponsoring on GitHub, or an install that predates Pro.
  6enum MacPro {
  7    static let buyURL = URL(string: "https://krz.sh/apps/orgstar/buy")!
  8    static let keyURL = URL(string: "https://krz.sh/apps/orgstar/license")!
  9
 10    /// Sets `ProAccess.shared` from the stored license or an earlier free install.
 11    @MainActor static func resolve() {
 12        if LicenseStore.current() != nil {
 13            ProAccess.shared.set(.license)
 14        } else if Grandfathering.macPredatesPro(marker: WorkspaceModel.defaultDirectory.appendingPathComponent("roots.json")) {
 15            ProAccess.shared.set(.grandfathered)
 16        } else {
 17            ProAccess.shared.set(nil)
 18        }
 19    }
 20
 21    /// Renews a monthly sponsor's key now and then daily, and re-resolves, so an ended
 22    /// sponsorship locks orgstar+ once its key expires.
 23    @MainActor static func renewInBackground() {
 24        Task {
 25            while true {
 26                _ = await LicenseStore.renewIfDue()
 27                resolve()
 28                try? await Task.sleep(for: .seconds(86400))
 29            }
 30        }
 31    }
 32}
 33
 34/// Status, buying, and entering or removing a license key.
 35struct LicenseForm: View {
 36    @State private var key = ""
 37    @State private var error: String?
 38    @State private var license = LicenseStore.current()
 39    private var access: ProAccess { ProAccess.shared }
 40
 41    var body: some View {
 42        Form {
 43            Section {
 44                switch access.grant {
 45                case .license:
 46                    LabeledContent("orgstar+", value: license.map { "Licensed to \($0.holder)" } ?? "Licensed")
 47                    if let expires = license?.expires {
 48                        let date = Date(timeIntervalSince1970: TimeInterval(expires)).formatted(date: .abbreviated, time: .omitted)
 49                        Text("Renews while you sponsor monthly. This key is valid until \(date).", tableName: "DeveloperID")
 50                            .foregroundStyle(.secondary)
 51                    }
 52                case .grandfathered:
 53                    LabeledContent("orgstar+", value: "Included: you used orgstar before orgstar+")
 54                case .purchase, nil:
 55                    LabeledContent("orgstar+", value: "Not licensed")
 56                }
 57            }
 58            if access.grant != .license {
 59                Section {
 60                    TextField(text: $key, prompt: Text("ORGSTAR1-…", tableName: "DeveloperID"), axis: .vertical) { Text("License key", tableName: "DeveloperID") }
 61                        .lineLimit(2...4)
 62                        .font(.body.monospaced())
 63                        .onSubmit(activate)
 64                    if let error { Text(error).foregroundStyle(.red) }
 65                    HStack {
 66                        Link(destination: MacPro.buyURL) { Text("Sponsor for orgstar+…", tableName: "DeveloperID") }
 67                        Link(destination: MacPro.keyURL) { Text("Get Your Key…", tableName: "DeveloperID") }
 68                        Spacer()
 69                        Button(action: activate) { Text("Activate", tableName: "DeveloperID") }.disabled(key.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
 70                    }
 71                } footer: {
 72                    Text("Sponsors get their key by signing in with GitHub. It is checked on this Mac; a monthly sponsor's key is renewed with krz.sh.", tableName: "DeveloperID")
 73                }
 74            } else {
 75                Section {
 76                    Button(role: .destructive) {
 77                        LicenseStore.remove()
 78                        license = nil
 79                        MacPro.resolve()
 80                    } label: {
 81                        Text("Remove License from This Mac", tableName: "DeveloperID")
 82                    }
 83                }
 84            }
 85            Section("Included in orgstar+") {
 86                ForEach(ProFeature.macFeatures) { feature in
 87                    Label(feature.title, systemImage: feature.systemImage)
 88                }
 89            }
 90        }
 91        .formStyle(.grouped)
 92    }
 93
 94    private func activate() {
 95        do {
 96            license = try LicenseStore.activate(key)
 97            key = ""
 98            error = nil
 99            ProAccess.shared.set(.license)
100        } catch .badSignature {
101            error = "This key isn't valid for orgstar. Paste the whole key from the license page."
102        } catch .expired {
103            error = "This key has expired. Sign in on the license page for a current one."
104        } catch {
105            self.error = "That doesn't look like an orgstar license key."
106        }
107    }
108}
109
110/// Shown when a Pro feature is used without Pro.
111struct ProPaywallSheet: View {
112    let feature: ProFeature
113    @Environment(\.dismiss) private var dismiss
114
115    var body: some View {
116        VStack(alignment: .leading, spacing: 0) {
117            VStack(alignment: .leading, spacing: 6) {
118                Label("\(feature.title) needs orgstar+", systemImage: feature.systemImage).font(.headline)
119                Text("orgstar+ for Mac comes with sponsoring orgstar on GitHub: $60 once, or $5 a month. Editing, the agenda, capture, clocking and search stay free.", tableName: "DeveloperID")
120                    .foregroundStyle(.secondary)
121            }
122            .padding([.horizontal, .top], 20)
123            LicenseForm()
124            HStack {
125                Spacer()
126                Button("Done") { dismiss() }.keyboardShortcut(.defaultAction)
127            }
128            .padding([.horizontal, .bottom], 20)
129        }
130        .frame(width: 480, height: 520)
131        .onChange(of: ProAccess.shared.isUnlocked) { _, unlocked in if unlocked { dismiss() } }
132    }
133}
134
135#endif
136
137extension ProFeature {
138    /// The Pro features the Mac app has.
139    static let macFeatures: [ProFeature] = [.babel, .export, .encryption, .roam, .widgets, .calendar, .shortcuts]
140}