90 lines
2.4 KiB
Swift
90 lines
2.4 KiB
Swift
|
import SwiftUI
|
||
|
|
||
|
private let df: DateFormatter = {
|
||
|
let df = DateFormatter()
|
||
|
df.dateStyle = .short
|
||
|
df.timeStyle = .short
|
||
|
return df
|
||
|
}()
|
||
|
|
||
|
struct HistoryListItem: View {
|
||
|
|
||
|
let entry: HistoryItem
|
||
|
|
||
|
var entryTime: String {
|
||
|
df.string(from: entry.outgoingDate)
|
||
|
}
|
||
|
|
||
|
var roundTripText: String {
|
||
|
guard let time = entry.roundTripTime else {
|
||
|
return ""
|
||
|
}
|
||
|
return "⇆ \(Int(time * 1000)) ms"
|
||
|
}
|
||
|
|
||
|
var counterText: String {
|
||
|
let sentCounter = entry.outgoingMessage.content.id
|
||
|
let startText = "🔗 \(sentCounter)"
|
||
|
guard let rCounter = entry.incomingMessage?.content.id else {
|
||
|
return startText
|
||
|
}
|
||
|
let diff = Int(rCounter) - Int(sentCounter)
|
||
|
guard diff != 1 else {
|
||
|
return startText
|
||
|
}
|
||
|
return startText + "→\(rCounter)"
|
||
|
}
|
||
|
|
||
|
var timeOffsetText: String {
|
||
|
guard let offset = entry.clockOffset, offset != 0 else {
|
||
|
return ""
|
||
|
}
|
||
|
return "🕓 \(offset) s"
|
||
|
}
|
||
|
|
||
|
var body: some View {
|
||
|
VStack {
|
||
|
HStack {
|
||
|
Text(entry.response?.description ?? "")
|
||
|
.font(.headline)
|
||
|
Spacer()
|
||
|
Text(entryTime)
|
||
|
}.padding(.bottom, 1)
|
||
|
HStack {
|
||
|
Text(roundTripText)
|
||
|
.font(.subheadline)
|
||
|
.foregroundColor(.secondary)
|
||
|
Text(counterText)
|
||
|
.font(.subheadline)
|
||
|
.foregroundColor(.secondary)
|
||
|
Text(timeOffsetText)
|
||
|
.font(.subheadline)
|
||
|
.foregroundColor(.secondary)
|
||
|
Spacer()
|
||
|
}
|
||
|
}.padding()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
struct HistoryListItem_Previews: PreviewProvider {
|
||
|
static var previews: some View {
|
||
|
HistoryListItem(entry: .mock)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private extension HistoryItem {
|
||
|
|
||
|
static var mock: HistoryItem {
|
||
|
let mac = Data(repeating: 42, count: 32)
|
||
|
let content = Message.Content(time: Date.now.timestamp, id: 123)
|
||
|
let content2 = Message.Content(time: (Date.now + 1).timestamp, id: 124)
|
||
|
return .init(
|
||
|
sent: Message(mac: mac, content: content),
|
||
|
date: .now)
|
||
|
.didReceive(
|
||
|
response: .openSesame,
|
||
|
date: .now + 2,
|
||
|
message: Message(mac: mac, content: content2))
|
||
|
}
|
||
|
}
|