-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathOneSignalViewModel.swift
More file actions
476 lines (380 loc) · 14.6 KB
/
Copy pathOneSignalViewModel.swift
File metadata and controls
476 lines (380 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
* Modified MIT License
*
* Copyright 2024 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import Foundation
import Combine
import OneSignalFramework
/// ViewModel that backs every section of the demo
@MainActor
final class OneSignalViewModel: ObservableObject {
// MARK: - App / Consent
@Published var appId: String
@Published var consentRequired: Bool = false
@Published var consentGiven: Bool = false
// MARK: - Identity
@Published var externalUserId: String?
@Published var aliases: [KeyValueItem] = []
// MARK: - Push
@Published var pushSubscriptionId: String?
@Published var isPushEnabled: Bool = false
@Published var hasNotificationPermission: Bool = false
// MARK: - Channels
@Published var emails: [String] = []
@Published var smsNumbers: [String] = []
// MARK: - Tags / Triggers
@Published var tags: [KeyValueItem] = []
@Published var triggers: [KeyValueItem] = []
// MARK: - In-App / Location
@Published var isInAppMessagesPaused: Bool = false
@Published var isLocationShared: Bool = false
// MARK: - UI State
@Published var activeTooltip: TooltipData?
// MARK: - Computed
var isLoggedIn: Bool {
guard let id = externalUserId else { return false }
return !id.isEmpty
}
var loginButtonTitle: String { isLoggedIn ? "SWITCH USER" : "LOGIN USER" }
// MARK: - Private
private let service: OneSignalService
private let prefs: PreferencesService
private var observers = Observers()
/// Monotonically incremented on every `fetchUserDataFromApi` call. The
/// value captured at entry guards the post-await write so a slow fetch
/// for an old `onesignalId` cannot overwrite a newer fetch's results.
private var requestSequence: UInt64 = 0
// MARK: - Init
init(service: OneSignalService = .shared, prefs: PreferencesService = .shared) {
self.service = service
self.prefs = prefs
self.appId = service.appId
self.consentRequired = service.consentRequired
self.consentGiven = service.consentGiven
self.externalUserId = service.externalId ?? prefs.getExternalUserId()
self.hasNotificationPermission = service.hasNotificationPermission
refreshState()
setupObservers()
TooltipService.shared.loadIfNeeded()
if service.onesignalId != nil {
Task { await fetchUserDataFromApi() }
}
}
// MARK: - State sync
func refreshState() {
pushSubscriptionId = service.pushSubscriptionId
isPushEnabled = service.isPushEnabled
isInAppMessagesPaused = service.isInAppMessagesPaused
isLocationShared = service.isLocationShared
hasNotificationPermission = service.hasNotificationPermission
externalUserId = service.externalId
let sdkTags = service.getTags()
tags = sdkTags.map { KeyValueItem(key: $0.key, value: $0.value) }
}
func fetchUserDataFromApi() async {
guard let onesignalId = service.onesignalId else { return }
requestSequence &+= 1
let captured = requestSequence
let userData = await UserFetchService.shared.fetchUser(appId: appId, onesignalId: onesignalId)
// Drop the result if a newer fetch has started while this one was in flight.
guard captured == requestSequence else { return }
if let userData = userData {
aliases = userData.aliases.map { KeyValueItem(key: $0.key, value: $0.value) }
tags = userData.tags.map { KeyValueItem(key: $0.key, value: $0.value) }
emails = userData.emails
smsNumbers = userData.smsNumbers
if let extId = userData.externalId, !extId.isEmpty {
externalUserId = extId
}
}
}
// MARK: - Consent
func setConsentRequired(_ required: Bool) {
consentRequired = required
service.consentRequired = required
if !required {
consentGiven = true
service.consentGiven = true
}
}
func setConsentGiven(_ granted: Bool) {
consentGiven = granted
service.consentGiven = granted
}
// MARK: - User
func login(externalId: String, token: String? = nil) {
let trimmed = externalId.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return }
let trimmedToken = token?.trimmingCharacters(in: .whitespacesAndNewlines)
service.login(externalId: trimmed, token: (trimmedToken?.isEmpty ?? true) ? nil : trimmedToken)
externalUserId = trimmed
clearUserData()
}
func updateUserJwt(externalId: String, token: String) {
let trimmedId = externalId.trimmingCharacters(in: .whitespacesAndNewlines)
let trimmedToken = token.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmedId.isEmpty, !trimmedToken.isEmpty else { return }
service.updateUserJwt(externalId: trimmedId, token: trimmedToken)
}
func logout() {
service.logout()
externalUserId = nil
clearUserData()
}
private func clearUserData() {
aliases.removeAll()
emails.removeAll()
smsNumbers.removeAll()
tags.removeAll()
triggers.removeAll()
}
// MARK: - Aliases
func addAlias(label: String, id: String) {
service.addAlias(label: label, id: id)
aliases.removeAll { $0.key == label }
aliases.append(KeyValueItem(key: label, value: id))
}
func addAliases(_ pairs: [(String, String)]) {
let dict = Dictionary(pairs, uniquingKeysWith: { _, last in last })
service.addAliases(dict)
for (key, value) in pairs {
aliases.removeAll { $0.key == key }
aliases.append(KeyValueItem(key: key, value: value))
}
}
func removeAlias(_ item: KeyValueItem) {
service.removeAlias(item.key)
aliases.removeAll { $0.id == item.id }
}
// MARK: - Push
func setPushEnabled(_ enabled: Bool) {
if enabled {
service.optInPush()
isPushEnabled = true
} else {
service.optOutPush()
isPushEnabled = false
}
}
func promptPushPermission() {
service.requestPushPermission { [weak self] accepted in
Task { @MainActor in
self?.hasNotificationPermission = accepted
self?.isPushEnabled = accepted
}
}
}
// MARK: - Email
func addEmail(_ email: String) {
service.addEmail(email)
if !emails.contains(email) { emails.append(email) }
}
func removeEmail(_ email: String) {
service.removeEmail(email)
emails.removeAll { $0 == email }
}
// MARK: - SMS
func addSms(_ number: String) {
service.addSms(number)
if !smsNumbers.contains(number) { smsNumbers.append(number) }
}
func removeSms(_ number: String) {
service.removeSms(number)
smsNumbers.removeAll { $0 == number }
}
// MARK: - Tags
func addTag(key: String, value: String) {
service.addTag(key: key, value: value)
tags.removeAll { $0.key == key }
tags.append(KeyValueItem(key: key, value: value))
}
func addTags(_ pairs: [(String, String)]) {
let dict = Dictionary(pairs, uniquingKeysWith: { _, last in last })
service.addTags(dict)
for (key, value) in pairs {
tags.removeAll { $0.key == key }
tags.append(KeyValueItem(key: key, value: value))
}
}
func removeTag(_ item: KeyValueItem) {
service.removeTag(item.key)
tags.removeAll { $0.id == item.id }
}
func removeSelectedTags(_ keys: [String]) {
guard !keys.isEmpty else { return }
service.removeTags(keys)
tags.removeAll { keys.contains($0.key) }
}
// MARK: - Outcomes
func sendOutcome(_ name: String) {
service.sendOutcome(name)
print("[OneSignal] Outcome sent: \(name)")
}
func sendUniqueOutcome(_ name: String) {
service.sendUniqueOutcome(name)
print("[OneSignal] Unique outcome sent: \(name)")
}
func sendOutcome(_ name: String, value: Double) {
service.sendOutcome(name, value: NSNumber(value: value))
print("[OneSignal] Outcome sent: \(name) = \(value)")
}
// MARK: - In-App
func setIamPaused(_ paused: Bool) {
isInAppMessagesPaused = paused
service.isInAppMessagesPaused = paused
}
func sendIamTrigger(_ type: InAppMessageType) {
service.addTrigger(key: "iam_type", value: type.triggerValue)
triggers.removeAll { $0.key == "iam_type" }
triggers.append(KeyValueItem(key: "iam_type", value: type.triggerValue))
}
// MARK: - Triggers
func addTrigger(key: String, value: String) {
service.addTrigger(key: key, value: value)
triggers.removeAll { $0.key == key }
triggers.append(KeyValueItem(key: key, value: value))
}
func addTriggers(_ pairs: [(String, String)]) {
let dict = Dictionary(pairs, uniquingKeysWith: { _, last in last })
service.addTriggers(dict)
for (key, value) in pairs {
triggers.removeAll { $0.key == key }
triggers.append(KeyValueItem(key: key, value: value))
}
}
func removeTrigger(_ item: KeyValueItem) {
service.removeTrigger(item.key)
triggers.removeAll { $0.id == item.id }
}
func removeSelectedTriggers(_ keys: [String]) {
guard !keys.isEmpty else { return }
service.removeTriggers(keys)
triggers.removeAll { keys.contains($0.key) }
}
func clearTriggers() {
service.clearTriggers()
triggers.removeAll()
}
// MARK: - Custom Events
func trackEvent(name: String, properties: [String: Any]?) {
service.trackEvent(name: name, properties: properties)
print("[OneSignal] Event tracked: \(name)")
}
// MARK: - Location
func setLocationShared(_ shared: Bool) {
isLocationShared = shared
service.isLocationShared = shared
}
func promptLocation() {
service.requestLocationPermission()
}
func checkLocationShared() -> Bool {
let shared = service.isLocationShared
print("[OneSignal] Location shared: \(shared)")
return shared
}
// MARK: - Notifications
func clearAllNotifications() {
service.clearAllNotifications()
}
func sendNotification(_ type: NotificationType) {
guard let subscriptionId = service.pushSubscriptionId, !subscriptionId.isEmpty else { return }
NotificationSender.shared.sendNotification(type, appId: appId, subscriptionId: subscriptionId) { _ in }
}
func sendCustomNotification(title: String, body: String) {
guard let subscriptionId = service.pushSubscriptionId, !subscriptionId.isEmpty else { return }
NotificationSender.shared.sendCustomNotification(title: title, body: body, appId: appId, subscriptionId: subscriptionId) { _ in }
}
// MARK: - Live Activities
func startLiveActivity(activityId: String, orderNumber: String, status: LiveActivityStatus) {
let trimmedId = activityId.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmedId.isEmpty else { return }
if #available(iOS 16.1, *) {
LiveActivityController.start(
activityId: trimmedId,
orderNumber: orderNumber,
status: status
)
}
}
func updateLiveActivity(activityId: String, status: LiveActivityStatus) {
let trimmedId = activityId.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmedId.isEmpty else { return }
Task {
_ = await LiveActivityController.update(
appId: appId,
activityId: trimmedId,
status: status
)
}
}
func endLiveActivity(activityId: String) {
let trimmedId = activityId.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmedId.isEmpty else { return }
Task {
_ = await LiveActivityController.end(
appId: appId,
activityId: trimmedId
)
}
}
// MARK: - Tooltips
func showTooltip(for key: String) {
if let tooltip = TooltipService.shared.tooltip(for: key) {
activeTooltip = tooltip
}
}
func dismissTooltip() {
activeTooltip = nil
}
// MARK: - Observers
private func setupObservers() {
observers.viewModel = self
service.addPushSubscriptionObserver(observers)
service.addUserObserver(observers)
service.addPermissionObserver(observers)
}
}
// MARK: - Observer Bridge
private final class Observers: NSObject, OSPushSubscriptionObserver, OSUserStateObserver, OSNotificationPermissionObserver {
weak var viewModel: OneSignalViewModel?
func onPushSubscriptionDidChange(state: OSPushSubscriptionChangedState) {
Task { @MainActor in
viewModel?.pushSubscriptionId = state.current.id
viewModel?.isPushEnabled = state.current.optedIn
}
}
func onUserStateDidChange(state: OSUserChangedState) {
Task { @MainActor in
await viewModel?.fetchUserDataFromApi()
}
}
func onNotificationPermissionDidChange(_ permission: Bool) {
Task { @MainActor in
viewModel?.hasNotificationPermission = permission
viewModel?.isPushEnabled = OneSignal.User.pushSubscription.optedIn
}
}
}