Sesame-iOS/Sesame/HistoryView.swift
2023-08-14 10:39:29 +02:00

61 lines
1.5 KiB
Swift

import SwiftUI
struct HistoryView: View {
let history: HistoryManagerProtocol
@State
private var items: [HistoryItem] = []
@State
private var unlockCount = 0
private var percentage: Double {
guard items.count > 0 else {
return 0
}
return Double(unlockCount * 100) / Double(items.count)
}
var body: some View {
NavigationView {
List {
HStack {
Text("\(items.count) requests")
.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")
}
.onAppear {
load()
}
}
private func load() {
Task {
let entries = history.loadEntries()
DispatchQueue.main.async {
items = entries
unlockCount = items.count {
$0.response == .openSesame
}
}
}
}
}
struct HistoryView_Previews: PreviewProvider {
static var previews: some View {
HistoryView(history: HistoryManagerMock())
}
}