DomainDig/FeatureAccessService.swift
279 lines · 10113 bytes
22 symbols in this file
FeatureTierFeatureCapabilityFeatureEntitlementsUpgradePromptContextFeatureAccessServiceentitlementshasAccesscanAddTrackedDomaincanAddTrackedDomaintrackedDomainLimitMessagecanCreateWorkflowcanRunBatchupgradeMessageworkflowLimitMessagebatchLimitMessageworkflowAllowanceSummarybatchAllowanceSummaryupgradePromptForTrackedDomainsupgradePromptForWorkflowsupgradePromptForBatchupgradePromptenabledFeatureLabels
1import Foundation
2
3enum FeatureTier: String, Codable, CaseIterable, Identifiable {
4 case free
5 case pro
6 case proPlus = "dataPlus"
7
8 var id: String { rawValue }
9
10 var title: String {
11 switch self {
12 case .free:
13 return "Free"
14 case .pro:
15 return "Pro"
16 case .proPlus:
17 return "Pro+"
18 }
19 }
20}
21
22enum FeatureCapability: String, CaseIterable, Identifiable {
23 case singleLookup
24 case basicHistory
25 case limitedTracking
26 case workflows
27 case batchOperations
28 case automatedMonitoring
29 case localAlerts
30 case advancedExports
31 case extendedSubdomains
32 case sharedPortfolios
33 case auditDepth
34 case intelligenceDetail
35 case bucketExport
36 case accentTheme
37
38 var id: String { rawValue }
39
40 var title: String {
41 switch self {
42 case .singleLookup:
43 return "Single lookup"
44 case .basicHistory:
45 return "Basic history"
46 case .limitedTracking:
47 return "Limited tracking"
48 case .workflows:
49 return "Workflows"
50 case .batchOperations:
51 return "Batch operations"
52 case .automatedMonitoring:
53 return "Background monitoring"
54 case .localAlerts:
55 return "Local alerts"
56 case .advancedExports:
57 return "Advanced exports"
58 case .extendedSubdomains:
59 return "Extended subdomains"
60 case .sharedPortfolios:
61 return "Shared portfolios"
62 case .auditDepth:
63 return "Unlimited audits"
64 case .intelligenceDetail:
65 return "Intelligence detail"
66 case .bucketExport:
67 return "Export to your own bucket"
68 case .accentTheme:
69 return "Accent themes"
70 }
71 }
72}
73
74struct FeatureEntitlements: Equatable {
75 let tier: FeatureTier
76 let capabilities: Set<FeatureCapability>
77 let trackedDomainLimit: Int
78 let workflowLimit: Int?
79 let batchSizeLimit: Int?
80 /// Concurrently open audit sessions; nil is unlimited.
81 let activeAuditSessionLimit: Int?
82 /// Snapshots kept per install, the retention dial D1 settled on. Not
83 /// optional: every tier has a ceiling, and the store is not a place to
84 /// accumulate without one.
85 let historyRetentionLimit: Int
86}
87
88struct UpgradePromptContext: Identifiable, Equatable {
89 let id = UUID()
90 let title: String
91 let message: String
92 let capability: FeatureCapability
93}
94
95enum FeatureAccessService {
96 private static let effectivelyUnlimitedTrackedDomains = 5_000
97
98 /// Reads the observable property rather than `PurchaseService.cachedTier`,
99 /// the static over UserDefaults. SwiftUI records a dependency for any
100 /// observable property read while a body evaluates, however deep in the
101 /// call tree, so every `hasAccess` call site in a view now updates when a
102 /// purchase lands. The nine `let _ = purchaseService.currentTier` discards
103 /// that used to manufacture that dependency are gone with this.
104 static var currentTier: FeatureTier {
105 PurchaseService.shared.currentTier
106 }
107
108 static var entitlements: FeatureEntitlements {
109 entitlements(for: currentTier)
110 }
111
112 static func entitlements(for tier: FeatureTier) -> FeatureEntitlements {
113 switch tier {
114 case .free:
115 return FeatureEntitlements(
116 tier: .free,
117 capabilities: [.singleLookup, .basicHistory, .limitedTracking, .workflows, .batchOperations],
118 trackedDomainLimit: 5,
119 workflowLimit: 1,
120 batchSizeLimit: 10,
121 activeAuditSessionLimit: 1,
122 historyRetentionLimit: 50
123 )
124 case .pro:
125 return FeatureEntitlements(
126 tier: .pro,
127 capabilities: [
128 .singleLookup,
129 .basicHistory,
130 .limitedTracking,
131 .workflows,
132 .batchOperations,
133 .automatedMonitoring,
134 .localAlerts,
135 .advancedExports,
136 .extendedSubdomains
137 ],
138 trackedDomainLimit: effectivelyUnlimitedTrackedDomains,
139 workflowLimit: nil,
140 batchSizeLimit: nil,
141 activeAuditSessionLimit: nil,
142 historyRetentionLimit: 250
143 )
144 case .proPlus:
145 return FeatureEntitlements(
146 tier: .proPlus,
147 capabilities: Set(FeatureCapability.allCases),
148 trackedDomainLimit: effectivelyUnlimitedTrackedDomains,
149 workflowLimit: nil,
150 batchSizeLimit: nil,
151 activeAuditSessionLimit: nil,
152 historyRetentionLimit: 2_000
153 )
154 }
155 }
156
157 /// Snapshots this tier keeps. A downgrade lowers what the store may grow
158 /// to; it never prunes what is already there (Review Focus #6).
159 static var historyRetentionLimit: Int {
160 entitlements.historyRetentionLimit
161 }
162
163 static func hasAccess(to capability: FeatureCapability) -> Bool {
164 entitlements.capabilities.contains(capability)
165 }
166
167 static func canAddTrackedDomain(currentCount: Int) -> Bool {
168 canAddTrackedDomain(currentCount: currentCount, tier: currentTier)
169 }
170
171 static func canAddTrackedDomain(currentCount: Int, tier: FeatureTier) -> Bool {
172 currentCount < entitlements(for: tier).trackedDomainLimit
173 }
174
175 static func trackedDomainLimitMessage(currentCount: Int) -> String? {
176 guard currentTier == .free else { return nil }
177 if currentCount >= entitlements.trackedDomainLimit {
178 return "Free includes up to \(entitlements.trackedDomainLimit) tracked domains. Available in Pro."
179 }
180 return "Free includes up to \(entitlements.trackedDomainLimit) tracked domains."
181 }
182
183 static func canCreateWorkflow(currentCount: Int) -> Bool {
184 guard hasAccess(to: .workflows) else { return false }
185 guard let limit = entitlements.workflowLimit else { return true }
186 return currentCount < limit
187 }
188
189 static func canRunBatch(domainCount: Int) -> Bool {
190 guard hasAccess(to: .batchOperations) else { return false }
191 guard let limit = entitlements.batchSizeLimit else { return true }
192 return domainCount <= limit
193 }
194
195 static func upgradeMessage(for capability: FeatureCapability) -> String {
196 switch capability {
197 case .workflows, .batchOperations, .automatedMonitoring, .localAlerts, .advancedExports,
198 .extendedSubdomains:
199 return "Available in Pro"
200 case .sharedPortfolios, .auditDepth, .intelligenceDetail, .bucketExport, .accentTheme:
201 return "Available in Pro+"
202 case .limitedTracking:
203 return "Tracking is limited on Free"
204 case .singleLookup, .basicHistory:
205 return "Included in Free"
206 }
207 }
208
209 static func workflowLimitMessage(currentCount: Int) -> String? {
210 guard let limit = entitlements.workflowLimit, currentCount >= limit else { return nil }
211 return "Free includes up to \(limit) workflow."
212 }
213
214 static func batchLimitMessage(domainCount: Int) -> String? {
215 guard let limit = entitlements.batchSizeLimit, domainCount > limit else { return nil }
216 return "Free runs batches up to \(limit) domains."
217 }
218
219 static func workflowAllowanceSummary(currentCount: Int) -> String? {
220 guard currentTier == .free, let limit = entitlements.workflowLimit else { return nil }
221 if currentCount >= limit {
222 return "Free includes up to \(limit) workflow. Available in Pro."
223 }
224 return "Free includes up to \(limit) workflow."
225 }
226
227 static func batchAllowanceSummary() -> String? {
228 guard currentTier == .free, let limit = entitlements.batchSizeLimit else { return nil }
229 return "Free runs batches up to \(limit) domains."
230 }
231
232 static func upgradePromptForTrackedDomains(currentCount: Int) -> UpgradePromptContext? {
233 guard currentCount >= entitlements.trackedDomainLimit else { return nil }
234 return UpgradePromptContext(
235 title: "Available in Pro",
236 message: "Free includes up to \(entitlements.trackedDomainLimit) tracked domains. You can keep your current watchlist, but adding more requires Pro.",
237 capability: .limitedTracking
238 )
239 }
240
241 static func upgradePromptForWorkflows(currentCount: Int) -> UpgradePromptContext? {
242 guard let limit = entitlements.workflowLimit, currentCount >= limit else { return nil }
243 return UpgradePromptContext(
244 title: "Available in Pro",
245 message: "Free includes up to \(limit) workflow. You can keep your current workflows, but creating more requires Pro.",
246 capability: .workflows
247 )
248 }
249
250 static func upgradePromptForBatch(domainCount: Int) -> UpgradePromptContext? {
251 guard let limit = entitlements.batchSizeLimit, domainCount > limit else { return nil }
252 return UpgradePromptContext(
253 title: "Available in Pro",
254 message: "Free runs batches up to \(limit) domains at a time. You can continue with smaller batches or upgrade to Pro.",
255 capability: .batchOperations
256 )
257 }
258
259 static func upgradePrompt(for capability: FeatureCapability) -> UpgradePromptContext {
260 let title: String
261 switch capability {
262 case .sharedPortfolios, .auditDepth, .intelligenceDetail, .bucketExport, .accentTheme:
263 title = "Available in Pro+"
264 default:
265 title = "Available in Pro"
266 }
267 return UpgradePromptContext(
268 title: title,
269 message: upgradeMessage(for: capability),
270 capability: capability
271 )
272 }
273
274 static func enabledFeatureLabels() -> [String] {
275 FeatureCapability.allCases
276 .filter { hasAccess(to: $0) }
277 .map(\.title)
278 }
279}