Some messy stuff cleaned up in Inventory UI

This commit is contained in:
2026-03-20 02:40:51 -05:00
parent 310d9faf33
commit 813031c823
4 changed files with 53 additions and 20 deletions

View File

@@ -1,6 +1,52 @@
import SwiftUI
import LabWiseKit
private let isoDateFormatter: ISO8601DateFormatter = {
let f = ISO8601DateFormatter()
f.formatOptions = [.withFullDate]
return f
}()
private let isoTimestampFormatter: ISO8601DateFormatter = {
let f = ISO8601DateFormatter()
f.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
return f
}()
private let displayDateFormatter: DateFormatter = {
let f = DateFormatter()
f.dateStyle = .medium
f.timeStyle = .none
return f
}()
private let displayTimestampFormatter: DateFormatter = {
let f = DateFormatter()
f.dateStyle = .medium
f.timeStyle = .short
return f
}()
private func formatDate(_ iso: String?) -> String {
guard let iso else { return "" }
if let date = isoDateFormatter.date(from: iso) {
return displayDateFormatter.string(from: date)
}
// Fallback: try full timestamp (e.g. "2025-03-01T00:00:00.000Z")
if let date = isoTimestampFormatter.date(from: iso) {
return displayDateFormatter.string(from: date)
}
return iso
}
private func formatTimestamp(_ iso: String?) -> String {
guard let iso else { return "" }
if let date = isoTimestampFormatter.date(from: iso) {
return displayTimestampFormatter.string(from: date)
}
return iso
}
struct ChemicalDetailView: View {
@State private var chemical: Chemical
@State private var showEdit = false
@@ -56,8 +102,8 @@ struct ChemicalDetailView: View {
if let lot = chemical.lotNumber {
LabeledContent("Lot #", value: lot)
}
if let exp = chemical.expirationDate {
LabeledContent("Expiration", value: exp)
if let exp = chemical.expirationDate, !exp.isEmpty {
LabeledContent("Expiration", value: formatDate(exp))
}
if let barcode = chemical.barcode {
LabeledContent("Barcode", value: barcode)
@@ -76,10 +122,10 @@ struct ChemicalDetailView: View {
Section("Record") {
if let created = chemical.createdAt {
LabeledContent("Created", value: created)
LabeledContent("Created", value: formatTimestamp(created))
}
if let updated = chemical.updatedAt {
LabeledContent("Updated", value: updated)
LabeledContent("Updated", value: formatTimestamp(updated))
}
}
}

View File

@@ -65,8 +65,7 @@ final class DashboardViewModel {
do {
chemicals = try await client.list()
} catch {
print("[DashboardViewModel] load error: \(error)")
errorMessage = "Failed to load: \(error)"
errorMessage = "Failed to load dashboard"
}
}

View File

@@ -16,8 +16,7 @@ final class InventoryViewModel {
do {
chemicals = try await client.list()
} catch {
print("[InventoryViewModel] loadChemicals error: \(error)")
errorMessage = "Failed to load chemicals: \(error)"
errorMessage = "Failed to load chemicals"
}
}
@@ -26,8 +25,7 @@ final class InventoryViewModel {
try await client.delete(id: chemical.id)
chemicals.removeAll { $0.id == chemical.id }
} catch {
print("[InventoryViewModel] delete error: \(error)")
errorMessage = "Failed to delete: \(error)"
errorMessage = "Failed to delete"
}
}
}