Branchwell

Inkspring / app · inkspring.ai / native-mac / Sources / Inkspring / InkspringCommunity.swift

Raw 4.7 KB on 3299148fb8af57afc73c4a39f59b66f2ac2bca32
1import Foundation
2import StoreKit
3import SwiftUI
4#if os(macOS)
5import AppKit
6#endif
7
8enum InkspringCommunity {
9 static let website = URL(string: "https://inkspring.ai/")!
10 static let apps = URL(string: "https://projectatom.ai/apps")!
11 static let support = URL(string: "https://inkspring.ai/help")!
12 static let websiteHelp = URL(string: "https://inkspring.ai/help")!
13 static let feedback = URL(string: "https://projectatom.ai/apps?app=inkspring#feedback")!
14 static let appStoreReview = URL(string: "https://apps.apple.com/app/id6834006000?action=write-review")!
15 static let community = URL(string: "https://projectatom.ai/glimmer#trade-communities")!
16}
17
18#if os(macOS)
19@MainActor
20enum InkspringSupportReminder {
21 enum Action: String, CaseIterable, Identifiable {
22 case newsletter, feedback, share, support, apps
23 var id: Self { self }
24 }
25
26 static let actionsKey = "inkspring.community.supportActions"
27 static let presentedKey = "inkspring.community.supportPresentedAt"
28 /// Set by "Don't show again". The reminder never returns once it is set.
29 static let dismissedKey = "inkspring.community.supportDismissed"
30 static let interval: TimeInterval = 7 * 24 * 60 * 60
31
32 static func clicked(_ values: String) -> Set<Action> {
33 Set(values.split(separator: ",").compactMap { Action(rawValue: String($0)) })
34 }
35
36 @discardableResult
37 static func record(_ action: Action, in defaults: UserDefaults = .standard) -> Bool {
38 var actions = clicked(defaults.string(forKey: actionsKey) ?? "")
39 actions.insert(action)
40 defaults.set(actions.map(\.rawValue).sorted().joined(separator: ","), forKey: actionsKey)
41 return actions.count == Action.allCases.count
42 }
43
44 static func dismissPermanently(in defaults: UserDefaults = .standard) {
45 defaults.set(true, forKey: dismissedKey)
46 }
47
48 static func claim(now: Date = .now, in defaults: UserDefaults = .standard) -> Bool {
49 guard !defaults.bool(forKey: dismissedKey),
50 defaults.integer(forKey: InkspringReview.activityKey) >= InkspringReview.milestone,
51 clicked(defaults.string(forKey: actionsKey) ?? "").count < Action.allCases.count else { return false }
52 let last = defaults.double(forKey: presentedKey)
53 guard last == 0 || now.timeIntervalSince1970 - last >= interval else { return false }
54 defaults.set(now.timeIntervalSince1970, forKey: presentedKey)
55 return true
56 }
57}
58#endif
59
60@MainActor
61enum InkspringReview {
62 static let milestone = 5
63 static let activityKey = "inkspring.community.completedWriting"
64 #if os(macOS)
65 // The old Mac flag tracked a web feedback dialog, not an App Store request.
66 static let requestedKey = "inkspring.community.appStoreReviewRequested"
67 #else
68 static let requestedKey = "inkspring.community.reviewRequested"
69 #endif
70
71 static func recordActivity(in defaults: UserDefaults = .standard) {
72 let count = defaults.integer(forKey: activityKey)
73 guard count < milestone else { return }
74 defaults.set(count + 1, forKey: activityKey)
75 }
76
77 static func claimRequest(in defaults: UserDefaults = .standard) -> Bool {
78 guard defaults.integer(forKey: activityKey) >= milestone,
79 !defaults.bool(forKey: requestedKey) else { return false }
80 defaults.set(true, forKey: requestedKey)
81 return true
82 }
83}
84
85struct InkspringRatingPrompt: ViewModifier {
86 let isIdle: Bool
87 @Environment(\.scenePhase) private var scenePhase
88 @Environment(\.requestReview) private var requestReview
89 @AppStorage(InkspringReview.activityKey) private var completedActivities = 0
90 @AppStorage(InkspringReview.requestedKey) private var requested = false
91
92 private var canRequest: Bool {
93 isIdle && scenePhase == .active && completedActivities >= InkspringReview.milestone && !requested
94 }
95
96 func body(content: Content) -> some View {
97 content.task(id: canRequest) {
98 guard canRequest else { return }
99 do { try await Task.sleep(for: .seconds(2)) } catch { return }
100 #if !DEBUG
101 // The native sheet rates an App Store app. TestFlight and direct Mac
102 // downloads should not consume the prompt for a later Store install.
103 guard case .verified(let transaction) = try? await AppTransaction.shared,
104 transaction.environment == .production else { return }
105 #endif
106 #if os(macOS)
107 guard NSApp.isActive, let window = NSApp.keyWindow, !window.isSheet,
108 window.attachedSheet == nil else { return }
109 #endif
110 guard !Task.isCancelled, canRequest, InkspringReview.claimRequest() else { return }
111 requestReview()
112 }
113 }
114}