From f14b0970d470907fcd2ea4f7a85d5fb5745b802f Mon Sep 17 00:00:00 2001 From: gperissetcelteeka <129070838+gperissetcelteeka@users.noreply.github.com> Date: Wed, 22 May 2024 18:19:10 -0300 Subject: [PATCH] BAL-194-GP-NEW EVENT TRACKING (#196) - New event tracking -- Open by notification -- Open manually --- Balance/Balance.swift | 23 +++++++++++++++++++++++ Balance/BalanceAppDelegate.swift | 12 ++++++++++++ Balance/Utils/BalanceExtensions.swift | 8 ++++++++ 3 files changed, 43 insertions(+) diff --git a/Balance/Balance.swift b/Balance/Balance.swift index 6969d3c..3f474b4 100644 --- a/Balance/Balance.swift +++ b/Balance/Balance.swift @@ -28,6 +28,7 @@ struct Balance: App { @Environment(\.scenePhase) var scenePhase + @State var heartAlert = false var body: some Scene { WindowGroup { @@ -74,6 +75,10 @@ struct Balance: App { print("ScenePhase: unexpected state") } } + .onReceive( NotificationCenter.default.publisher( for: Notification.Name.heartAlert)) { _ in + self.heartAlert = true + appEvent(description: "App Opened via Heart Rate notification") + } } } @@ -84,10 +89,28 @@ struct Balance: App { if value == false { activityLogEntry.reset() } + self.heartAlert = false } func activeApp() { + if heartAlert == false { + appEvent(description: "App Opened manually") + } UIApplication.shared.applicationIconBadgeNumber = 0 UserDefaults.standard.set(false, forKey: StorageKeys.spotifyConnect) } + + func appEvent(description: String) { + activityLogEntry.addActionButton(actionDescription: description) +#if DEMO + logStore.saveLog(activityLogEntry) + ActivityLogStore.save(logs: logStore.logs) { result in + if case .failure(let error) = result { + print(error.localizedDescription) + } + } +#else + ActivityStorageManager.shared.uploadActivity(activityLogEntry: activityLogEntry) +#endif + } } diff --git a/Balance/BalanceAppDelegate.swift b/Balance/BalanceAppDelegate.swift index 67bdb03..2aec63a 100644 --- a/Balance/BalanceAppDelegate.swift +++ b/Balance/BalanceAppDelegate.swift @@ -79,4 +79,16 @@ class BalanceAppDelegate: CardinalKitAppDelegate { sceneConfig.delegateClass = SceneDelegate.self return sceneConfig } + + // swiftlint:disable:next discouraged_optional_collection + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + UNUserNotificationCenter.current().delegate = self + return true + } +} + +extension BalanceAppDelegate: UNUserNotificationCenterDelegate { + func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async { + NotificationCenter.default.post(name: Notification.Name.heartAlert, object: nil) + } } diff --git a/Balance/Utils/BalanceExtensions.swift b/Balance/Utils/BalanceExtensions.swift index 1bd23a7..be4a741 100644 --- a/Balance/Utils/BalanceExtensions.swift +++ b/Balance/Utils/BalanceExtensions.swift @@ -131,6 +131,7 @@ extension Notification.Name { static let coinsUpdate = Notification.Name("coinsUpdate") static let coinsRefresh = Notification.Name("coinsRefresh") static let coinsAlert = Notification.Name("coinsAlert") + static let heartAlert = Notification.Name("heartAlert") } // swiftlint:disable operator_whitespace @@ -158,3 +159,10 @@ extension UIScreen { static let screenHeight = UIScreen.main.bounds.size.height static let screenSize = UIScreen.main.bounds.size } + +extension Array { + func first(elementCount: Int) -> Array { + let min = Swift.min(elementCount, count) + return Array(self[0..