Sources/Orgstar/ProLicense.swift
138 lines · 5611 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).")
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("License key", text: $key, prompt: Text("ORGSTAR1-…"), axis: .vertical)
61 .lineLimit(2...4)
62 .font(.body.monospaced())
63 .onSubmit(activate)
64 if let error { Text(error).foregroundStyle(.red) }
65 HStack {
66 Link("Sponsor for orgstar+…", destination: MacPro.buyURL)
67 Link("Get Your Key…", destination: MacPro.keyURL)
68 Spacer()
69 Button("Activate", action: activate).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.")
73 }
74 } else {
75 Section {
76 Button("Remove License from This Mac", role: .destructive) {
77 LicenseStore.remove()
78 license = nil
79 MacPro.resolve()
80 }
81 }
82 }
83 Section("Included in orgstar+") {
84 ForEach(ProFeature.macFeatures) { feature in
85 Label(feature.title, systemImage: feature.systemImage)
86 }
87 }
88 }
89 .formStyle(.grouped)
90 }
91
92 private func activate() {
93 do {
94 license = try LicenseStore.activate(key)
95 key = ""
96 error = nil
97 ProAccess.shared.set(.license)
98 } catch .badSignature {
99 error = "This key isn't valid for orgstar. Paste the whole key from the license page."
100 } catch .expired {
101 error = "This key has expired. Sign in on the license page for a current one."
102 } catch {
103 self.error = "That doesn't look like an orgstar license key."
104 }
105 }
106}
107
108/// Shown when a Pro feature is used without Pro.
109struct ProPaywallSheet: View {
110 let feature: ProFeature
111 @Environment(\.dismiss) private var dismiss
112
113 var body: some View {
114 VStack(alignment: .leading, spacing: 0) {
115 VStack(alignment: .leading, spacing: 6) {
116 Label("\(feature.title) needs orgstar+", systemImage: feature.systemImage).font(.headline)
117 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.")
118 .foregroundStyle(.secondary)
119 }
120 .padding([.horizontal, .top], 20)
121 LicenseForm()
122 HStack {
123 Spacer()
124 Button("Done") { dismiss() }.keyboardShortcut(.defaultAction)
125 }
126 .padding([.horizontal, .bottom], 20)
127 }
128 .frame(width: 480, height: 520)
129 .onChange(of: ProAccess.shared.isUnlocked) { _, unlocked in if unlocked { dismiss() } }
130 }
131}
132
133#endif
134
135extension ProFeature {
136 /// The Pro features the Mac app has.
137 static let macFeatures: [ProFeature] = [.babel, .export, .encryption, .roam, .widgets, .calendar, .shortcuts]
138}