Sources/OrgApp/ProAccess.swift
144 lines · 5766 bytes
10 symbols in this file
1import Foundation
2import Observation
3
4/// Features that need orgstar+.
5public enum ProFeature: String, CaseIterable, Sendable, Identifiable {
6 case babel, export, encryption, roam
7 case widgets, watch, liveActivity, calendar, shortcuts
8
9 public var id: String { rawValue }
10
11 public var title: String {
12 switch self {
13 case .babel: "Run and tangle source blocks"
14 case .export: "PDF, Word and other export, and publishing"
15 case .encryption: "Encrypted entries"
16 case .roam: "Org-roam nodes and backlinks"
17 case .widgets: "Widgets"
18 case .watch: "Apple Watch app"
19 case .liveActivity: "Clock Live Activity"
20 case .calendar: "Calendar and Reminders"
21 case .shortcuts: "Shortcuts actions"
22 }
23 }
24
25 public var systemImage: String {
26 switch self {
27 case .babel: "play.rectangle"
28 case .export: "square.and.arrow.up"
29 case .encryption: "lock"
30 case .roam: "point.3.connected.trianglepath.dotted"
31 case .widgets: "square.grid.2x2"
32 case .watch: "applewatch"
33 case .liveActivity: "timer"
34 case .calendar: "calendar"
35 case .shortcuts: "square.stack.3d.up"
36 }
37 }
38}
39
40extension ProFeature {
41 /// Export formats that stay free.
42 static let freeExports: Set<String> = ["html", "html-open", "markdown"]
43
44 /// The feature an export format needs, if any.
45 public static func required(byExport format: String) -> ProFeature? {
46 freeExports.contains(format) ? nil : .export
47 }
48
49 /// The feature an app command (`app.…`, from a key, menu or the palette) needs, if any.
50 /// Decrypting stays free so no one is locked out of their own text.
51 public static func required(byCommand id: String) -> ProFeature? {
52 switch id {
53 // The print panel saves PDFs. The agenda prints without orgstar+.
54 case "app.copy-rich-text", "app.print", "app.print-subtree", "app.print-selection": .export
55 case let id where id.hasPrefix("app.export."): required(byExport: String(id.dropFirst("app.export.".count)))
56 case let id where id.hasPrefix("app.publish"): .export
57 case "app.crypt.encrypt-entry", "app.crypt.encrypt-entries": .encryption
58 case let id where id.hasPrefix("app.dailies.") || id.hasPrefix("app.node."): .roam
59 case "app.toggle-backlinks", "app.backlinks": .roam
60 default: nil
61 }
62 }
63}
64
65/// Why Pro is unlocked.
66public enum ProGrant: String, Codable, Sendable {
67 /// Installed before Pro existed.
68 case grandfathered
69 /// An App Store purchase or subscription.
70 case purchase
71 /// A Mac license key.
72 case license
73}
74
75/// Whether Pro is unlocked. The app writes it; the app group's defaults carry it to the widgets,
76/// share extension and intents, which only read it.
77@MainActor @Observable
78public final class ProAccess {
79 public static let shared = ProAccess(defaults: ProAccess.groupDefaults)
80
81 nonisolated static let grantKey = "pro.grant"
82
83 /// The app group's defaults, or the standard ones when the app isn't in the group (the Mac app).
84 public nonisolated static var groupDefaults: UserDefaults {
85 UserDefaults(suiteName: CaptureInbox.group) ?? .standard
86 }
87
88 /// For code outside the main actor (widgets, intents): reads the stored grant.
89 public nonisolated static var isUnlockedNow: Bool {
90 groupDefaults.string(forKey: grantKey).flatMap(ProGrant.init(rawValue:)) != nil
91 }
92
93 private let defaults: UserDefaults
94 public private(set) var grant: ProGrant?
95 /// The feature whose paywall should show, if any.
96 public var paywall: ProFeature?
97
98 public init(defaults: UserDefaults) {
99 self.defaults = defaults
100 grant = defaults.string(forKey: Self.grantKey).flatMap(ProGrant.init(rawValue:))
101 }
102
103 public var isUnlocked: Bool { grant != nil }
104
105 public func set(_ grant: ProGrant?) {
106 self.grant = grant
107 defaults.set(grant?.rawValue, forKey: Self.grantKey)
108 }
109
110 /// True when `feature` may be used; otherwise shows its paywall and returns false.
111 @discardableResult
112 public func allows(_ feature: ProFeature) -> Bool {
113 if isUnlocked { return true }
114 paywall = feature
115 return false
116 }
117}
118
119/// Who installed before Pro and keeps it free.
120public enum Grandfathering {
121 /// The first iOS build (`CFBundleVersion`, the commit count) that sells Pro: one more than
122 /// main's count before Pro merged. An App Store `originalAppVersion` below it means the app was
123 /// first downloaded while it was free.
124 public static let firstPaidIOSBuild = 569
125
126 /// Whether an App Store original build number predates Pro.
127 public static func predatesPro(originalBuild: String, firstPaidBuild: Int = firstPaidIOSBuild) -> Bool {
128 guard let build = Int(originalBuild.trimmingCharacters(in: .whitespaces)) else { return false }
129 return build < firstPaidBuild
130 }
131
132 static let macDecidedKey = "pro.macGrandfatherDecided"
133
134 /// Decides once, on the first launch of a Pro build of the Mac app, whether this Mac ran an
135 /// earlier free version: Sparkle marks its first launch, and `marker` (the workspace's list of
136 /// folders) exists once folders were added. Later launches return the first answer.
137 public static func macPredatesPro(defaults: UserDefaults = .standard, marker: URL?) -> Bool {
138 if let decided = defaults.object(forKey: macDecidedKey) as? Bool { return decided }
139 let ranBefore = defaults.object(forKey: "SUHasLaunchedBefore") != nil
140 || marker.map { FileManager.default.fileExists(atPath: $0.path) } == true
141 defaults.set(ranBefore, forKey: macDecidedKey)
142 return ranBefore
143 }
144}