Sources/Orgstar/ProLicense.swift

135 lines · 5589 bytes

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