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

68 lines
1.9 KiB
Swift

import SwiftUI
struct HistoryView: View {
@ObservedObject
var history: HistoryManager
private var unlockCount: Int {
history.entries.count {
$0.response == .openSesame
}
}
private var percentage: Double {
guard history.entries.count > 0 else {
return 0
}
return Double(unlockCount * 100) / Double(history.entries.count)
}
var body: some View {
NavigationView {
List {
HStack {
VStack(alignment: .leading) {
Text("\(history.entries.count) requests")
.foregroundColor(.primary)
.font(.body)
Text(String(format: "%.1f %% success", percentage))
.foregroundColor(.secondary)
.font(.footnote)
}
Spacer()
}
.listRowBackground(Color.clear)
ForEach(history.entries) { item in
NavigationLink {
HistoryItemDetail(item: item, history: history)
} label: {
HistoryListRow(item: item)
}
.swipeActions(edge: .trailing) {
Button {
delete(item: item)
} label: {
Label("Delete", systemSymbol: .trash)
}.tint(.red)
}
}
}
.navigationTitle("History")
}
}
private func delete(item: HistoryItem) {
guard history.delete(item: item) else {
return
}
}
}
struct HistoryView_Previews: PreviewProvider {
static var previews: some View {
HistoryView(history: HistoryManager())
}
}