2023-08-09 16:29:18 +02:00
|
|
|
import SwiftUI
|
2023-12-12 17:33:42 +01:00
|
|
|
import SwiftData
|
2023-08-09 16:29:18 +02:00
|
|
|
import SFSafeSymbols
|
|
|
|
|
|
|
|
private let df: DateFormatter = {
|
|
|
|
let df = DateFormatter()
|
|
|
|
df.dateStyle = .short
|
|
|
|
df.timeStyle = .short
|
|
|
|
df.doesRelativeDateFormatting = true
|
|
|
|
return df
|
|
|
|
}()
|
|
|
|
|
|
|
|
struct HistoryItemDetail: View {
|
2023-12-12 17:33:42 +01:00
|
|
|
|
|
|
|
@Environment(\.modelContext)
|
|
|
|
private var modelContext
|
2023-08-09 16:29:18 +02:00
|
|
|
|
|
|
|
let item: HistoryItem
|
2023-08-14 10:39:29 +02:00
|
|
|
|
|
|
|
@Environment(\.dismiss) private var dismiss
|
2023-08-09 16:29:18 +02:00
|
|
|
|
|
|
|
private var entryTime: String {
|
2023-12-12 17:33:42 +01:00
|
|
|
df.string(from: item.startDate)
|
2023-08-09 16:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
List {
|
|
|
|
SettingsListTextItem(
|
|
|
|
title: "Status",
|
2023-08-14 10:39:29 +02:00
|
|
|
value: item.response.description)
|
2023-08-09 16:29:18 +02:00
|
|
|
SettingsListTextItem(
|
|
|
|
title: "Date",
|
|
|
|
value: entryTime)
|
|
|
|
SettingsListTextItem(
|
|
|
|
title: "Connection",
|
2023-12-12 17:33:42 +01:00
|
|
|
value: item.route.displayName)
|
2023-08-14 10:39:29 +02:00
|
|
|
SettingsListTextItem(
|
2023-08-09 16:29:18 +02:00
|
|
|
title: "Round Trip Time",
|
2023-08-14 10:39:29 +02:00
|
|
|
value: "\(Int(item.roundTripTime * 1000)) ms")
|
2023-12-12 17:33:42 +01:00
|
|
|
SettingsListTextItem(
|
|
|
|
title: "Client challenge",
|
|
|
|
value: "\(item.message.clientChallenge)")
|
|
|
|
SettingsListTextItem(
|
|
|
|
title: "Server challenge",
|
|
|
|
value: "\(item.message.serverChallenge)")
|
2023-08-14 10:39:29 +02:00
|
|
|
Button {
|
|
|
|
delete(item: item)
|
|
|
|
} label: {
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
Label("Delete", systemSymbol: .trash)
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.listRowBackground(
|
|
|
|
RoundedRectangle(cornerSize: CGSize(width: 8, height: 8))
|
|
|
|
.fill(.red)
|
|
|
|
)
|
|
|
|
.foregroundColor(.white)
|
2023-08-09 16:29:18 +02:00
|
|
|
}.navigationTitle("Details")
|
|
|
|
}
|
2023-08-14 10:39:29 +02:00
|
|
|
|
|
|
|
private func delete(item: HistoryItem) {
|
2023-12-12 17:33:42 +01:00
|
|
|
modelContext.delete(item)
|
2023-08-14 10:39:29 +02:00
|
|
|
dismiss()
|
|
|
|
}
|
2023-08-09 16:29:18 +02:00
|
|
|
}
|
|
|
|
|
2023-12-12 17:33:42 +01:00
|
|
|
#Preview {
|
|
|
|
do {
|
|
|
|
let config = ModelConfiguration(isStoredInMemoryOnly: true)
|
|
|
|
let container = try ModelContainer(for: HistoryItem.self, configurations: config)
|
|
|
|
|
|
|
|
let item = HistoryItem.mock
|
|
|
|
container.mainContext.insert(item)
|
|
|
|
try container.mainContext.save()
|
|
|
|
return HistoryItemDetail(item: .mock)
|
|
|
|
.modelContainer(container)
|
|
|
|
} catch {
|
|
|
|
fatalError("Failed to create model container.")
|
2023-08-09 16:29:18 +02:00
|
|
|
}
|
|
|
|
}
|