Sources/Orgstar/ProLicense.swift

942b1aeb78c1317dea1c036aeef6d2a71eff4364
orgstar/Sources/Orgstar/ProLicense.swift

114 lines · 4469 bytes

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