Skip to content

Commit

Permalink
BAL-194-GP-NEW EVENT TRACKING (#196)
Browse files Browse the repository at this point in the history
- New event tracking
-- Open by notification
-- Open manually
  • Loading branch information
gperissetcelteeka authored May 22, 2024
1 parent 677cd6c commit f14b097
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Balance/Balance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct Balance: App {

@Environment(\.scenePhase)
var scenePhase
@State var heartAlert = false

var body: some Scene {
WindowGroup {
Expand Down Expand Up @@ -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")
}
}
}

Expand All @@ -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
}
}
12 changes: 12 additions & 0 deletions Balance/BalanceAppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
8 changes: 8 additions & 0 deletions Balance/Utils/BalanceExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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..<min])
}
}

0 comments on commit f14b097

Please sign in to comment.