87 lines
3.2 KiB
Swift
87 lines
3.2 KiB
Swift
//
|
|
// HistoryView.swift
|
|
// MoneyCounter
|
|
//
|
|
// Created by Dallas Groot on 2026-04-06.
|
|
//
|
|
|
|
import SwiftUI
|
|
import SwiftData
|
|
|
|
struct HistoryView: View {
|
|
@Environment(\.dismiss) private var dismiss
|
|
@Environment(\.modelContext) private var modelContext
|
|
|
|
// Auto-fetches and sorts by date descending
|
|
@Query(sort: \SavedCount.date, order: .reverse) private var savedCounts: [SavedCount]
|
|
|
|
var viewModel: MoneyCounterViewModel
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
List {
|
|
ForEach(savedCounts) { save in
|
|
VStack(alignment: .leading, spacing: 6) {
|
|
HStack {
|
|
Text(save.date.formatted(date: .abbreviated, time: .shortened))
|
|
.font(.headline)
|
|
Spacer()
|
|
Text(String(format: "$%.2f", save.total))
|
|
.font(.title3).bold()
|
|
}
|
|
|
|
if save.startingFloat > 0 {
|
|
HStack {
|
|
Text("Float: $\(save.startingFloat, specifier: "%.2f")")
|
|
.font(.subheadline).foregroundColor(.secondary)
|
|
Spacer()
|
|
Text(save.discrepancy >= 0 ? "Dep: +$\(save.discrepancy, specifier: "%.2f")" : "Short: $\(save.discrepancy, specifier: "%.2f")")
|
|
.font(.subheadline)
|
|
.foregroundColor(save.discrepancy >= 0 ? .green : .red)
|
|
}
|
|
}
|
|
|
|
if !save.notes.isEmpty {
|
|
Text(save.notes)
|
|
.font(.caption)
|
|
.foregroundColor(.secondary)
|
|
.lineLimit(2)
|
|
}
|
|
}
|
|
.padding(.vertical, 4)
|
|
.swipeActions(edge: .leading) {
|
|
Button(action: {
|
|
resume(save: save)
|
|
}) {
|
|
Label("Resume", systemImage: "arrow.counterclockwise")
|
|
}
|
|
.tint(.blue)
|
|
}
|
|
}
|
|
.onDelete(perform: deleteItems)
|
|
}
|
|
.navigationTitle("Count History")
|
|
.toolbar {
|
|
ToolbarItem(placement: .navigationBarTrailing) {
|
|
Button("Close") { dismiss() }
|
|
}
|
|
}
|
|
.overlay {
|
|
if savedCounts.isEmpty {
|
|
ContentUnavailableView("No History", systemImage: "tray", description: Text("Saved counts will appear here."))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func deleteItems(offsets: IndexSet) {
|
|
for index in offsets {
|
|
modelContext.delete(savedCounts[index])
|
|
}
|
|
}
|
|
|
|
private func resume(save: SavedCount) {
|
|
viewModel.restore(from: save.snapshotData, float: save.startingFloat)
|
|
dismiss()
|
|
}
|
|
}
|