Branchwell

Bucksley / app / Sources / BucksleyKit / SupportPrompt.swift

1import Foundation
2
3/// The Protomota "support this free app" rules, shared with Jotley and Glimmer (Standard Settings):
4/// - every successful core action (a review, recategorize, budget, Ask answer) is counted;
5/// - App Store builds ask for a rating once, after five core actions;
6/// - the direct-download Mac app shows the support card after five, then at most once every seven
7/// days, until every one of its four actions has been clicked;
8/// - "Later" hides the card until the next week-long wait ends, "Don't show again" hides it for good.
9public enum SupportPrompt {
10 public enum Action: String, CaseIterable, Identifiable, Sendable {
11 case feedback, share, support, apps
12 public var id: Self { self }
13 }
14
15 public static let actionsKey = "bucksley.community.supportActions"
16 public static let presentedKey = "bucksley.community.supportPresentedAt"
17 public static let countKey = "bucksley.engagement.count"
18 public static let reviewRequestedKey = "bucksley.engagement.reviewRequested"
19 public static let neverShowKey = "bucksley.community.supportNeverShow"
20 public static let milestone = 5
21 public static let interval: TimeInterval = 7 * 24 * 60 * 60
22
23 public enum Links {
24 public static let website = URL(string: "https://bucksley.ai/")!
25 public static let protomotaApps = URL(string: "https://projectatom.ai/apps")!
26 public static let feedback = URL(string: "https://projectatom.ai/apps?app=bucksley#feedback")!
27 public static let support = URL(string: "https://bucksley.ai/help/")!
28 }
29
30 public static func clicked(_ values: String) -> Set<Action> {
31 Set(values.split(separator: ",").compactMap { Action(rawValue: String($0)) })
32 }
33
34 public static func clicked(in defaults: UserDefaults = .standard) -> Set<Action> { clicked(defaults.string(forKey: actionsKey) ?? "") }
35
36 /// Records an opened action and says whether every action has now been opened.
37 @discardableResult
38 public static func record(_ action: Action, in defaults: UserDefaults = .standard) -> Bool {
39 var actions = clicked(in: defaults)
40 actions.insert(action)
41 defaults.set(actions.map(\.rawValue).sorted().joined(separator: ","), forKey: actionsKey)
42 return actions.count == Action.allCases.count
43 }
44
45 public static func recordCoreAction(in defaults: UserDefaults = .standard) {
46 defaults.set(defaults.integer(forKey: countKey) + 1, forKey: countKey)
47 }
48
49 public static func coreActions(in defaults: UserDefaults = .standard) -> Int { defaults.integer(forKey: countKey) }
50
51 /// Whether the Mac card should show now. Showing it starts the next week-long wait.
52 public static func claim(now: Date = .now, in defaults: UserDefaults = .standard) -> Bool {
53 guard !isDismissedForGood(in: defaults), coreActions(in: defaults) >= milestone,
54 clicked(in: defaults).count < Action.allCases.count else { return false }
55 let last = defaults.double(forKey: presentedKey)
56 guard last == 0 || now.timeIntervalSince1970 - last >= interval else { return false }
57 defaults.set(now.timeIntervalSince1970, forKey: presentedKey)
58 return true
59 }
60
61 /// "Don't show again": the card never comes back on its own. Settings still lists every action.
62 public static func dismissForGood(in defaults: UserDefaults = .standard) { defaults.set(true, forKey: neverShowKey) }
63
64 public static func isDismissedForGood(in defaults: UserDefaults = .standard) -> Bool { defaults.bool(forKey: neverShowKey) }
65
66 /// App Store builds: ask for a rating once, after the milestone. The caller also checks that the
67 /// build is a production App Store install (never TestFlight or Xcode).
68 public static func claimReview(in defaults: UserDefaults = .standard) -> Bool {
69 guard coreActions(in: defaults) >= milestone, !defaults.bool(forKey: reviewRequestedKey) else { return false }
70 defaults.set(true, forKey: reviewRequestedKey)
71 return true
72 }
73}