Crossmate/Models/TipStore.swift (9941B)
1import Foundation
2import Observation
3import UIKit
4
5
6
7
8enum TipPlatform: Hashable, CaseIterable {
9 case iPhone
10 case iPad
11
12
13 static let all: Set<TipPlatform> = Set(allCases)
14
15
16 @MainActor static var current: TipPlatform {
17 UIDevice.current.userInterfaceIdiom == .pad ? .iPad : .iPhone
18 }
19}
20
21
22
23
24struct Tip: Identifiable, Equatable {
25 let id: String
26 let title: String
27 let body: String
28
29
30 var only: Set<TipPlatform> = TipPlatform.all
31}
32
33enum TipCatalog {
34
35
36
37
38 static let all: [Tip] = [
39 Tip(
40 id: "solve-together",
41 title: "Try a Multiplayer Crossword",
42 body: "Start a puzzle and choose Invite Players from the Players menu."
43 ),
44 Tip(
45 id: "connect-providers",
46 title: "Get More Puzzles",
47 body: "Add an external provider in Settings. Connected providers appear on the new puzzle screen."
48 ),
49 Tip(
50 id: "pick-your-colour",
51 title: "Pick Your Colour",
52 body: "Choose your colour. A different colour is selected each time for crossmates."
53 ),
54 Tip(
55 id: "get-attention",
56 title: "Announce Your Availability",
57 body: "Nudge your crossmates from the Players menu in a shared puzzle."
58 ),
59 Tip(
60 id: "be-notified",
61 title: "Control Your Notifications",
62 body: "Adjust the notifications you want to see in Settings."
63 ),
64 Tip(
65 id: "take-a-hint",
66 title: "Get Unstuck in Difficult Puzzles",
67 body: "Use the Hints menu in a puzzle to check your letters or get an answer."
68 ),
69 Tip(
70 id: "import-puzzles",
71 title: "Import Your Own Puzzles",
72 body: "Download Across Lite or XD files in Safari or save to the Crossmate folder in iCloud Drive."
73 ),
74 Tip(
75 id: "undo-redo",
76 title: "Undo Mistakes Easily",
77 body: "Press the overflow button on the onscreen keyboard to access undo and redo."
78 ),
79 Tip(
80 id: "hardware-keyboard",
81 title: "Use a Hardware Keyboard",
82 body: "Connect a keyboard to solve more swiftly.",
83 only: [.iPad]
84 ),
85 Tip(
86 id: "partial-fill",
87 title: "Break Solver's Block",
88 body: "Choose Fill Quarter or Fill Half from the Hints menu to reveal random squares."
89 ),
90 Tip(
91 id: "manage-blocked",
92 title: "Block and Unblock Users",
93 body: "Block using the Crossmates menu in the Game List. Unblock from your block list in Settings."
94 ),
95 Tip(
96 id: "start-shared",
97 title: "Start a Shared Puzzle",
98 body: "Tap the Crossmates button in the Game List and tap a crossmate to start a shared puzzle with them."
99 ),
100 Tip(
101 id: "share-your-win",
102 title: "Share Your Solve",
103 body: "Use the Share control when you've completed a puzzle to show everyone your solve."
104 ),
105 ]
106}
107
108
109
110
111
112@MainActor
113@Observable
114final class TipStore {
115
116
117
118
119
120 static let launchesBeforeTips = 2
121
122
123
124 var isDisabled: Bool {
125 didSet { defaults.set(isDisabled, forKey: disabledKey) }
126 }
127
128
129
130 private var dismissedIDs: Set<String> {
131 didSet { defaults.set(Array(dismissedIDs), forKey: dismissedKey) }
132 }
133
134
135
136
137 private var coldLaunchCount: Int {
138 didSet { defaults.set(coldLaunchCount, forKey: coldLaunchCountKey) }
139 }
140
141 @ObservationIgnored private let defaults: UserDefaults
142 @ObservationIgnored private let catalog: [Tip]
143 @ObservationIgnored private let platform: TipPlatform
144 @ObservationIgnored private let disabledKey = "tipsDisabled"
145 @ObservationIgnored private let dismissedKey = "dismissedTipIDs"
146 @ObservationIgnored private let coldLaunchCountKey = "tipColdLaunchCount"
147
148 init(
149 defaults: UserDefaults = .standard,
150 catalog: [Tip] = TipCatalog.all,
151 platform: TipPlatform = .current
152 ) {
153 self.defaults = defaults
154 self.catalog = catalog
155 self.platform = platform
156 self.isDisabled = defaults.bool(forKey: disabledKey)
157 self.dismissedIDs = Set(defaults.stringArray(forKey: dismissedKey) ?? [])
158 self.coldLaunchCount = defaults.integer(forKey: coldLaunchCountKey)
159 }
160
161
162
163
164 func noteColdLaunch() {
165 guard coldLaunchCount <= Self.launchesBeforeTips else { return }
166 coldLaunchCount += 1
167 }
168
169
170
171
172 var visibleTips: [Tip] {
173 catalog.filter { $0.only.contains(platform) }
174 }
175
176
177
178
179 func currentTip() -> Tip? {
180 guard !isDisabled else { return nil }
181 guard coldLaunchCount > Self.launchesBeforeTips else { return nil }
182 return firstUndismissedTip()
183 }
184
185
186
187
188
189 func firstUndismissedTip() -> Tip? {
190 visibleTips.first { !dismissedIDs.contains($0.id) }
191 }
192
193
194 func markDismissed(_ id: String) {
195 guard dismissedIDs.insert(id).inserted else { return }
196 }
197
198
199 func disable() { isDisabled = true }
200
201
202 func enable() { isDisabled = false }
203}
204
205extension Tip {
206
207
208 static func announcementID(for tipID: String) -> String { "tip-\(tipID)" }
209
210
211
212 static func tipID(fromAnnouncementID announcementID: String) -> String? {
213 let prefix = "tip-"
214 guard announcementID.hasPrefix(prefix) else { return nil }
215 return String(announcementID.dropFirst(prefix.count))
216 }
217
218
219
220 func liveAnnouncement() -> Announcement {
221 Announcement(
222 id: Self.announcementID(for: id),
223 scope: .global,
224 severity: .tip,
225 title: title,
226 body: body,
227 dismissal: .manual
228 )
229 }
230
231
232
233
234 func archiveAnnouncement() -> Announcement {
235 Announcement(
236 id: Self.announcementID(for: id),
237 scope: .global,
238 severity: .tip,
239 title: title,
240 body: body,
241 dismissal: .sticky
242 )
243 }
244}