Sesame-iOS/Sesame/History/HistoryItem.swift
2023-12-29 22:52:04 +01:00

60 lines
1.3 KiB
Swift

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