Sesame-iOS/Sesame/History/HistoryView.swift
2023-12-12 17:33:42 +01:00

62 lines
1.6 KiB
Swift

import SwiftUI
import SwiftData
struct HistoryView: View {
@Query
private var items: [HistoryItem] = []
private var unlockCount: Int {
items.count { $0.response == .unlocked }
}
private var percentage: Double {
guard items.count > 0 else {
return 0
}
return Double(unlockCount * 100) / Double(items.count)
}
private var requestNumberText: String {
guard items.count != 1 else {
return "1 Request"
}
return "\(items.count) Requests"
}
var body: some View {
NavigationView {
List {
HStack {
Text(requestNumberText)
.foregroundColor(.primary)
.font(.body)
Spacer()
Text(String(format: "%d successful (%.1f %%)", unlockCount, percentage))
.foregroundColor(.secondary)
.font(.footnote)
}
ForEach(items) {entry in
HistoryListItem(entry: entry)
}
}
.navigationTitle("History")
}
}
}
#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 HistoryView()
.modelContainer(container)
} catch {
fatalError("Failed to create model container.")
}
}