From 5f77eec20f05da44288c1a4a9bb8dde550dd71ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacek=20=C5=81yp?= Date: Thu, 19 Dec 2024 16:42:29 +0100 Subject: [PATCH] Remove an array of consecutive backgrounds, keep just 2 but add counter --- Core/Pixel.swift | 3 ++- .../AppLifecycle/AppStateTransitions.swift | 4 ++-- .../AppLifecycle/AppStates/Background.swift | 22 +++++++++---------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Core/Pixel.swift b/Core/Pixel.swift index 648a1ca2cb..dfca49cf37 100644 --- a/Core/Pixel.swift +++ b/Core/Pixel.swift @@ -167,12 +167,13 @@ public struct PixelParameters { public static let appEvent = "event" public static let firstBackgroundTimestamp = "firstBackgroundTimestamp" - public static let consecutiveBackgroundTimestamps = "consecutiveBackgroundTimestamps" + public static let secondBackgroundTimestamp = "secondBackgroundTimestamp" public static let didReceiveMemoryWarningTimestamp = "didReceiveMemoryWarningTimestamp" public static let didReceiveMXPayloadTimestamp = "didReceiveMXPayloadTimestamp" public static let didReceiveUNNotification = "didReceiveUNNotification" public static let didStartRemoteMessagingClientBackgroundTask = "didStartRemoteMessagingClientBackgroundTask" public static let didStartAppConfigurationFetchBackgroundTask = "didStartAppConfigurationFetchBackgroundTask" + public static let numberOfBackgrounds = "numberOfBackgrounds" } public struct PixelValues { diff --git a/DuckDuckGo/AppLifecycle/AppStateTransitions.swift b/DuckDuckGo/AppLifecycle/AppStateTransitions.swift index 7a0e6da79f..1dfb74e8a8 100644 --- a/DuckDuckGo/AppLifecycle/AppStateTransitions.swift +++ b/DuckDuckGo/AppLifecycle/AppStateTransitions.swift @@ -91,7 +91,7 @@ extension Background { case .openURL: return self case .backgrounding: - return DoubleBackground(firstTimeBackgroundTimestamp: timestamp, consecutiveTimestamps: []) + return DoubleBackground(firstTimeBackgroundTimestamp: timestamp, counter: 0) case .launching, .suspending: return handleUnexpectedEvent(event) } @@ -111,7 +111,7 @@ extension DoubleBackground { case .suspending(let application): return Inactive(application: application) case .backgrounding(let application): - return DoubleBackground(firstTimeBackgroundTimestamp: firstTimeBackgroundTimestamp, consecutiveTimestamps: consecutiveTimestamps) + return DoubleBackground(firstTimeBackgroundTimestamp: firstTimeBackgroundTimestamp, counter: counter) case .launching, .openURL: return self } diff --git a/DuckDuckGo/AppLifecycle/AppStates/Background.swift b/DuckDuckGo/AppLifecycle/AppStates/Background.swift index 6f3ac71b53..d927668bb8 100644 --- a/DuckDuckGo/AppLifecycle/AppStates/Background.swift +++ b/DuckDuckGo/AppLifecycle/AppStates/Background.swift @@ -39,21 +39,21 @@ struct DoubleBackground: AppState { }() let firstTimeBackgroundTimestamp: Date - var consecutiveTimestamps: [Date] = [] + var counter: Int - init(firstTimeBackgroundTimestamp: Date, consecutiveTimestamps: [Date]) { - self.firstTimeBackgroundTimestamp = firstTimeBackgroundTimestamp - self.consecutiveTimestamps = consecutiveTimestamps - let lastTimestamp = Date() - self.consecutiveTimestamps.append(lastTimestamp) + init(firstTimeBackgroundTimestamp: Date, counter: Int) { + let secondBackgroundTimestamp = Date() + self.firstTimeBackgroundTimestamp = secondBackgroundTimestamp + self.counter = counter + 1 - var parameters = [PixelParameters.firstBackgroundTimestamp: dateFormatter.string(from: firstTimeBackgroundTimestamp)] - - let formattedConsecutiveTimestamps = consecutiveTimestamps.map { dateFormatter.string(from: $0) } - parameters[PixelParameters.consecutiveBackgroundTimestamps] = formattedConsecutiveTimestamps.joined(separator: ",") + var parameters = [ + PixelParameters.firstBackgroundTimestamp: dateFormatter.string(from: firstTimeBackgroundTimestamp), + PixelParameters.secondBackgroundTimestamp: dateFormatter.string(from: secondBackgroundTimestamp), + PixelParameters.numberOfBackgrounds: String(counter) + ] func isValid(timestamp: Date) -> Bool { - timestamp >= firstTimeBackgroundTimestamp && timestamp <= lastTimestamp + timestamp >= firstTimeBackgroundTimestamp && timestamp <= secondBackgroundTimestamp } if let appDelegate = UIApplication.shared.delegate as? AppDelegate {