Sesame-iOS/Sesame/History/HistoryItem.swift

60 lines
1.3 KiB
Swift
Raw Normal View History

import Foundation
2023-12-12 17:33:42 +01:00
import SwiftData
2023-12-12 17:33:42 +01:00
@Model
final class HistoryItem {
2023-12-12 17:33:42 +01:00
let startDate: Date
let messageData: Data
var message: Message {
try! .init(decodeFrom: messageData)
}
2023-12-12 17:33:42 +01:00
let route: TransmissionType
2023-12-12 17:33:42 +01:00
let finishDate: Date
2023-12-12 17:33:42 +01:00
init(message: Message, startDate: Date, route: TransmissionType, finishDate: Date) {
self.startDate = startDate
self.messageData = message.encoded
2023-12-12 17:33:42 +01:00
self.finishDate = finishDate
self.route = route
}
2023-12-12 17:33:42 +01:00
var roundTripTime: TimeInterval {
finishDate.timeIntervalSince(startDate)
}
2023-12-12 17:33:42 +01:00
var response: MessageResult {
message.result
}
}
extension HistoryItem: Identifiable {
2023-12-12 17:33:42 +01:00
var id: Double {
startDate.timeIntervalSince1970
}
}
extension HistoryItem: Comparable {
2023-12-12 17:33:42 +01:00
static func < (lhs: HistoryItem, rhs: HistoryItem) -> Bool {
2023-12-12 17:33:42 +01:00
lhs.startDate < rhs.startDate
}
}
extension HistoryItem {
2023-12-12 17:33:42 +01:00
static var mock: HistoryItem {
2023-12-12 17:33:42 +01:00
let message = Message(messageType: .request, clientChallenge: 123, serverChallenge: 234, result: .unlocked)
2023-08-14 10:39:29 +02:00
return .init(
2023-12-12 17:33:42 +01:00
message: message,
startDate: Date.now.addingTimeInterval(-5),
route: .throughServer,
finishDate: Date.now)
}
}