import Foundation /// The Protomota "support this free app" rules, shared with Jotley and Glimmer (Standard Settings): /// - every successful core action (a review, recategorize, budget, Ask answer) is counted; /// - App Store builds ask for a rating once, after five core actions; /// - the direct-download Mac app shows the support card after five, then at most once every seven /// days, until every one of its four actions has been clicked; /// - "Later" hides the card until the next week-long wait ends, "Don't show again" hides it for good. public enum SupportPrompt { public enum Action: String, CaseIterable, Identifiable, Sendable { case feedback, share, support, apps public var id: Self { self } } public static let actionsKey = "bucksley.community.supportActions" public static let presentedKey = "bucksley.community.supportPresentedAt" public static let countKey = "bucksley.engagement.count" public static let reviewRequestedKey = "bucksley.engagement.reviewRequested" public static let neverShowKey = "bucksley.community.supportNeverShow" public static let milestone = 5 public static let interval: TimeInterval = 7 * 24 * 60 * 60 public enum Links { public static let website = URL(string: "https://bucksley.ai/")! public static let protomotaApps = URL(string: "https://projectatom.ai/apps")! public static let feedback = URL(string: "https://projectatom.ai/apps?app=bucksley#feedback")! public static let support = URL(string: "https://bucksley.ai/help/")! } public static func clicked(_ values: String) -> Set { Set(values.split(separator: ",").compactMap { Action(rawValue: String($0)) }) } public static func clicked(in defaults: UserDefaults = .standard) -> Set { clicked(defaults.string(forKey: actionsKey) ?? "") } /// Records an opened action and says whether every action has now been opened. @discardableResult public static func record(_ action: Action, in defaults: UserDefaults = .standard) -> Bool { var actions = clicked(in: defaults) actions.insert(action) defaults.set(actions.map(\.rawValue).sorted().joined(separator: ","), forKey: actionsKey) return actions.count == Action.allCases.count } public static func recordCoreAction(in defaults: UserDefaults = .standard) { defaults.set(defaults.integer(forKey: countKey) + 1, forKey: countKey) } public static func coreActions(in defaults: UserDefaults = .standard) -> Int { defaults.integer(forKey: countKey) } /// Whether the Mac card should show now. Showing it starts the next week-long wait. public static func claim(now: Date = .now, in defaults: UserDefaults = .standard) -> Bool { guard !isDismissedForGood(in: defaults), coreActions(in: defaults) >= milestone, clicked(in: defaults).count < Action.allCases.count else { return false } let last = defaults.double(forKey: presentedKey) guard last == 0 || now.timeIntervalSince1970 - last >= interval else { return false } defaults.set(now.timeIntervalSince1970, forKey: presentedKey) return true } /// "Don't show again": the card never comes back on its own. Settings still lists every action. public static func dismissForGood(in defaults: UserDefaults = .standard) { defaults.set(true, forKey: neverShowKey) } public static func isDismissedForGood(in defaults: UserDefaults = .standard) -> Bool { defaults.bool(forKey: neverShowKey) } /// App Store builds: ask for a rating once, after the milestone. The caller also checks that the /// build is a production App Store install (never TestFlight or Xcode). public static func claimReview(in defaults: UserDefaults = .standard) -> Bool { guard coreActions(in: defaults) >= milestone, !defaults.bool(forKey: reviewRequestedKey) else { return false } defaults.set(true, forKey: reviewRequestedKey) return true } }