Glimmer / app · glimmertoday.ai / native / Glimmer / SupportReminder.swift
| 1 | import SwiftUI |
| 2 | #if os(macOS) |
| 3 | import AppKit |
| 4 | #endif |
| 5 | |
| 6 | enum StoreLinks { |
| 7 | static let website = URL(string: "https://glimmertoday.ai/")! |
| 8 | static let protomotaApps = URL(string: "https://projectatom.ai/apps")! |
| 9 | static let feedback = URL(string: "https://projectatom.ai/apps?app=glimmer#feedback")! |
| 10 | /// Help articles and personal support. |
| 11 | static let support = URL(string: "https://glimmertoday.ai/support")! |
| 12 | |
| 13 | static var review: URL? { |
| 14 | guard let id = Bundle.main.object(forInfoDictionaryKey: "GlimmerAppStoreID") as? String, |
| 15 | !id.isEmpty, id.utf8.allSatisfy({ (48...57).contains($0) }) else { return nil } |
| 16 | return URL(string: "https://apps.apple.com/app/id\(id)?action=write-review") |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | #if os(macOS) |
| 21 | /// The Mac "support Glimmer" card, shared with the other Protomota Mac apps: after five logged |
| 22 | /// moments it returns at most once a week until every action has been opened. Later hides it for a week; |
| 23 | /// Don't show again turns it off for good. |
| 24 | @MainActor |
| 25 | enum GlimmerSupportReminder { |
| 26 | enum Action: String, CaseIterable, Identifiable { |
| 27 | case feedback, share, support, apps |
| 28 | var id: Self { self } |
| 29 | } |
| 30 | |
| 31 | static let actionsKey = "glimmer.community.supportActions" |
| 32 | static let presentedKey = "glimmer.community.supportPresentedAt" |
| 33 | static let dismissedKey = "glimmer.community.supportDismissed" |
| 34 | static let milestone = 5 |
| 35 | static let interval: TimeInterval = 7 * 24 * 60 * 60 |
| 36 | |
| 37 | static func clicked(_ values: String) -> Set<Action> { |
| 38 | Set(values.split(separator: ",").compactMap { Action(rawValue: String($0)) }) |
| 39 | } |
| 40 | |
| 41 | /// Records an opened action and says whether every action has now been opened. |
| 42 | @discardableResult |
| 43 | static func record(_ action: Action, in defaults: UserDefaults = .standard) -> Bool { |
| 44 | var actions = clicked(defaults.string(forKey: actionsKey) ?? "") |
| 45 | actions.insert(action) |
| 46 | defaults.set(actions.map(\.rawValue).sorted().joined(separator: ","), forKey: actionsKey) |
| 47 | return actions.count == Action.allCases.count |
| 48 | } |
| 49 | |
| 50 | /// Turns the reminder off for good. Settings still opens the card on request. |
| 51 | static func dismissForever(in defaults: UserDefaults = .standard) { |
| 52 | defaults.set(true, forKey: dismissedKey) |
| 53 | } |
| 54 | |
| 55 | /// Whether the reminder should show now. Showing it starts the next week-long wait. |
| 56 | static func claim(activity: Int, now: Date = .now, in defaults: UserDefaults = .standard) -> Bool { |
| 57 | guard activity >= milestone, !defaults.bool(forKey: dismissedKey), |
| 58 | clicked(defaults.string(forKey: actionsKey) ?? "").count < Action.allCases.count else { return false } |
| 59 | let last = defaults.double(forKey: presentedKey) |
| 60 | guard last == 0 || now.timeIntervalSince1970 - last >= interval else { return false } |
| 61 | defaults.set(now.timeIntervalSince1970, forKey: presentedKey) |
| 62 | return true |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /// Feedback, share, support and more apps, each ticked once opened. |
| 67 | struct GlimmerSupportSheet: View { |
| 68 | let isReminder: Bool |
| 69 | @Environment(\.openURL) private var openURL |
| 70 | @Environment(\.dismiss) private var dismiss |
| 71 | @AppStorage(GlimmerSupportReminder.actionsKey) private var clicks = "" |
| 72 | @State private var sharePicker = SupportSharePicker() |
| 73 | |
| 74 | var body: some View { |
| 75 | VStack(alignment: .leading, spacing: 16) { |
| 76 | HStack(spacing: 16) { |
| 77 | GlimmerLogo(size: 58) |
| 78 | VStack(alignment: .leading, spacing: 4) { |
| 79 | Text("Free software.\nSupported by people.") |
| 80 | .font(GlimmerFont.display(.title2, weight: .bold)) |
| 81 | .accessibilityAddTraits(.isHeader) |
| 82 | Text("Help more people discover Glimmer and tell us what we can improve.") |
| 83 | .foregroundStyle(.secondary) |
| 84 | .fixedSize(horizontal: false, vertical: true) |
| 85 | } |
| 86 | } |
| 87 | VStack(spacing: 8) { |
| 88 | row("Send feedback", detail: "Tell us what’s working and what could be better", |
| 89 | symbol: "bubble.left.and.bubble.right.fill", tint: GlimmerColor.sage, action: .feedback) { |
| 90 | visit(.feedback, url: StoreLinks.feedback) |
| 91 | } |
| 92 | row("Share Glimmer", detail: "Send someone a link to the app", symbol: "square.and.arrow.up.fill", |
| 93 | tint: GlimmerColor.accent, action: .share) { |
| 94 | sharePicker.show { |
| 95 | if GlimmerSupportReminder.record(.share), isReminder { dismiss() } |
| 96 | } |
| 97 | } |
| 98 | .background(SupportShareAnchor(view: sharePicker.anchor).allowsHitTesting(false)) |
| 99 | row("Open a Support Ticket", detail: "Free help or follow replies for Mac, iPhone, and iPad", |
| 100 | symbol: "headphones", tint: GlimmerColor.dusk, action: .support) { |
| 101 | visit(.support, url: StoreLinks.support) |
| 102 | } |
| 103 | row("More Free Apps from Project ATOM", detail: "Explore more free apps from Project ATOM", |
| 104 | symbol: "square.grid.2x2.fill", tint: GlimmerColor.honey, action: .apps) { |
| 105 | visit(.apps, url: StoreLinks.protomotaApps) |
| 106 | } |
| 107 | } |
| 108 | .glassGroup(spacing: 8) |
| 109 | Text("Checkmarks show which buttons you’ve opened.") |
| 110 | .font(.caption).foregroundStyle(.secondary) |
| 111 | if isReminder { |
| 112 | HStack { |
| 113 | Button("Don’t show again") { |
| 114 | GlimmerSupportReminder.dismissForever() |
| 115 | dismiss() |
| 116 | } |
| 117 | .buttonStyle(.plain) |
| 118 | .font(.callout) |
| 119 | .foregroundStyle(.secondary) |
| 120 | .accessibilityIdentifier("support.dontShowAgain") |
| 121 | Spacer() |
| 122 | PrimaryAction(title: "Later", systemImage: "clock") { dismiss() } |
| 123 | .keyboardShortcut(.cancelAction) |
| 124 | .accessibilityIdentifier("support.later") |
| 125 | } |
| 126 | } else { |
| 127 | HStack { |
| 128 | Spacer() |
| 129 | PrimaryAction(title: "Done") { dismiss() } |
| 130 | .keyboardShortcut(.cancelAction) |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | .padding(24) |
| 135 | .frame(width: 520) |
| 136 | .accessibilityIdentifier("supportReminder") |
| 137 | } |
| 138 | |
| 139 | private func visit(_ action: GlimmerSupportReminder.Action, url: URL) { |
| 140 | let finished = GlimmerSupportReminder.record(action) |
| 141 | openURL(url) |
| 142 | if finished, isReminder { dismiss() } |
| 143 | } |
| 144 | |
| 145 | private func row(_ title: String, detail: String, symbol: String, tint: Color, |
| 146 | action: GlimmerSupportReminder.Action, perform: @escaping () -> Void) -> some View { |
| 147 | let clicked = GlimmerSupportReminder.clicked(clicks).contains(action) |
| 148 | return ActionRow(title: title, detail: detail, icon: symbol, tint: tint, |
| 149 | trailing: clicked ? "checkmark.circle.fill" : "arrow.up.right", selected: clicked, action: perform) |
| 150 | .accessibilityValue(clicked ? "Previously opened" : "") |
| 151 | .accessibilityIdentifier("support.\(action.rawValue)") |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private struct SupportShareAnchor: NSViewRepresentable { |
| 156 | let view: NSView |
| 157 | func makeNSView(context: Context) -> NSView { view } |
| 158 | func updateNSView(_ nsView: NSView, context: Context) {} |
| 159 | } |
| 160 | |
| 161 | @MainActor |
| 162 | private final class SupportSharePicker: NSObject, NSSharingServicePickerDelegate { |
| 163 | let anchor = NSView() |
| 164 | private var picker: NSSharingServicePicker? |
| 165 | private var finished: (() -> Void)? |
| 166 | |
| 167 | func show(finished: @escaping () -> Void) { |
| 168 | guard picker == nil, anchor.window != nil else { return } |
| 169 | let picker = NSSharingServicePicker(items: [StoreLinks.website]) |
| 170 | self.picker = picker |
| 171 | self.finished = finished |
| 172 | picker.delegate = self |
| 173 | picker.show(relativeTo: anchor.bounds, of: anchor, preferredEdge: .minY) |
| 174 | } |
| 175 | |
| 176 | nonisolated func sharingServicePicker(_ sharingServicePicker: NSSharingServicePicker, didChoose service: NSSharingService?) { |
| 177 | Task { @MainActor in self.complete() } |
| 178 | } |
| 179 | |
| 180 | private func complete() { |
| 181 | let completion = finished |
| 182 | finished = nil |
| 183 | picker = nil |
| 184 | completion?() |
| 185 | } |
| 186 | } |
| 187 | #endif |