2022-05-01 18:30:30 +02:00
|
|
|
import Foundation
|
2023-12-12 17:33:42 +01:00
|
|
|
import SwiftData
|
2022-05-01 18:30:30 +02:00
|
|
|
|
2023-12-12 17:33:42 +01:00
|
|
|
@Model
|
|
|
|
final class HistoryItem {
|
2023-04-11 18:18:31 +02:00
|
|
|
|
2023-12-12 17:33:42 +01:00
|
|
|
let startDate: Date
|
2023-04-11 18:18:31 +02:00
|
|
|
|
2023-12-29 22:52:04 +01:00
|
|
|
let messageData: Data
|
|
|
|
|
|
|
|
var message: Message {
|
|
|
|
try! .init(decodeFrom: messageData)
|
|
|
|
}
|
2023-04-11 18:18:31 +02:00
|
|
|
|
2023-12-12 17:33:42 +01:00
|
|
|
let route: TransmissionType
|
2023-04-11 18:18:31 +02:00
|
|
|
|
2023-12-12 17:33:42 +01:00
|
|
|
let finishDate: Date
|
2023-04-11 18:18:31 +02:00
|
|
|
|
2023-12-12 17:33:42 +01:00
|
|
|
init(message: Message, startDate: Date, route: TransmissionType, finishDate: Date) {
|
|
|
|
self.startDate = startDate
|
2023-12-29 22:52:04 +01:00
|
|
|
self.messageData = message.encoded
|
2023-12-12 17:33:42 +01:00
|
|
|
self.finishDate = finishDate
|
|
|
|
self.route = route
|
2022-05-01 18:30:30 +02:00
|
|
|
}
|
2023-04-11 18:18:31 +02:00
|
|
|
|
2023-12-12 17:33:42 +01:00
|
|
|
var roundTripTime: TimeInterval {
|
|
|
|
finishDate.timeIntervalSince(startDate)
|
2022-05-01 18:30:30 +02:00
|
|
|
}
|
2023-04-11 18:18:31 +02:00
|
|
|
|
2023-12-12 17:33:42 +01:00
|
|
|
var response: MessageResult {
|
|
|
|
message.result
|
2022-05-01 18:30:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension HistoryItem: Identifiable {
|
2023-12-12 17:33:42 +01:00
|
|
|
|
|
|
|
var id: Double {
|
|
|
|
startDate.timeIntervalSince1970
|
2022-05-01 18:30:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension HistoryItem: Comparable {
|
2023-12-12 17:33:42 +01:00
|
|
|
|
2022-05-01 18:30:30 +02:00
|
|
|
static func < (lhs: HistoryItem, rhs: HistoryItem) -> Bool {
|
2023-12-12 17:33:42 +01:00
|
|
|
lhs.startDate < rhs.startDate
|
2023-04-11 18:18:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension HistoryItem {
|
2023-12-12 17:33:42 +01:00
|
|
|
|
2023-04-11 18:18:31 +02: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)
|
2022-05-01 18:30:30 +02:00
|
|
|
}
|
|
|
|
}
|