// // Copyright Amazon.com Inc. or its affiliates. // All Rights Reserved. // // SPDX-License-Identifier: Apache-2.0 // import Foundation @_spi(InternalAWSPinpoint) public class PinpointSession: Codable { typealias SessionId = String let sessionId: SessionId let startTime: Date private(set) var stopTime: Date? init(appId: String, uniqueId: String) { sessionId = Self.generateSessionId(appId: appId, uniqueId: uniqueId) startTime = Date() stopTime = nil } init(sessionId: SessionId, startTime: Date, stopTime: Date?) { self.sessionId = sessionId self.startTime = startTime self.stopTime = stopTime } var isPaused: Bool { return stopTime != nil } var duration: Date.Millisecond { let endTime = stopTime ?? Date() return endTime.millisecondsSince1970 - startTime.millisecondsSince1970 } func stop() { guard stopTime == nil else { return } stopTime = Date() } func pause() { guard !isPaused else { return } stopTime = Date() } func resume() { stopTime = nil } private static func generateSessionId(appId: String, uniqueId: String) -> SessionId { let now = Date() let dateFormatter = DateFormatter() dateFormatter.timeZone = TimeZone(abbreviation: Constants.Date.defaultTimezone) dateFormatter.locale = Locale(identifier: Constants.Date.defaultLocale) // Timestamp: Day dateFormatter.dateFormat = Constants.Date.dateFormat let timestampDay = dateFormatter.string(from: now) // Timestamp: Time dateFormatter.dateFormat = Constants.Date.timeFormat let timestampTime = dateFormatter.string(from: now) let appIdKey = appId.padding(toLength: Constants.maxAppKeyLength, withPad: Constants.paddingChar, startingAt: 0) let uniqueIdKey = uniqueId.padding(toLength: Constants.maxUniqueIdLength, withPad: Constants.paddingChar, startingAt: 0) // Create Session ID formatted as - - -