Files
LabWiseiOS/LabWise/SpreadsheetView.swift

191 lines
7.7 KiB
Swift
Raw Permalink Normal View History

import SwiftUI
import LabWiseKit
struct SpreadsheetView: View {
let chemicals: [Chemical]
let onDismiss: () -> Void
private struct ColumnDef {
let label: String
let width: CGFloat
let value: (Chemical) -> String
}
private static let columnDefs: [ColumnDef] = [
ColumnDef(label: "PI First Name", width: 130) { $0.piFirstName },
ColumnDef(label: "Physical State", width: 120) { $0.physicalState },
ColumnDef(label: "Chemical Name", width: 180) { $0.chemicalName },
ColumnDef(label: "Bldg Code", width: 100) { $0.bldgCode },
ColumnDef(label: "LAB", width: 80) { $0.lab },
ColumnDef(label: "Storage Location", width: 145) { $0.storageLocation },
ColumnDef(label: "Storage Device", width: 140) { $0.storageDevice },
ColumnDef(label: "# of Containers", width: 120) { $0.numberOfContainers },
ColumnDef(label: "Amount / Container", width: 150) { $0.amountPerContainer },
ColumnDef(label: "Unit of Measure", width: 130) { $0.unitOfMeasure },
ColumnDef(label: "CAS #", width: 115) { $0.casNumber },
ColumnDef(label: "Chemical Formula", width: 140) { $0.chemicalFormula ?? "" },
ColumnDef(label: "Molecular Weight", width: 130) { $0.molecularWeight ?? "" },
ColumnDef(label: "Vendor", width: 130) { $0.vendor ?? "" },
ColumnDef(label: "Catalog #", width: 120) { $0.catalogNumber ?? "" },
ColumnDef(label: "Lot Number", width: 120) { $0.lotNumber ?? "" },
ColumnDef(label: "Concentration", width: 120) { $0.concentration ?? "" },
ColumnDef(label: "Expiration Date", width: 130) { $0.expirationDate ?? "" },
ColumnDef(label: "Contact", width: 130) { $0.contact ?? "" },
ColumnDef(label: "Barcode", width: 130) { $0.barcode ?? "" },
ColumnDef(label: "Comments", width: 180) { $0.comments ?? "" },
2026-04-05 00:27:29 -05:00
ColumnDef(label: "% Full", width: 80) { c in
c.percentageFull.map { String(format: "%.0f%%", $0) } ?? ""
},
ColumnDef(label: "Date Entered", width: 130) { c in
c.createdAt.map { String($0.prefix(10)) } ?? ""
},
ColumnDef(label: "Last Changed", width: 130) { c in
c.updatedAt.map { String($0.prefix(10)) } ?? ""
},
]
2026-04-05 00:27:29 -05:00
private static let topBarHeight: CGFloat = 52
private static let headerHeight: CGFloat = 44
var body: some View {
2026-04-05 00:27:29 -05:00
ZStack(alignment: .top) {
Color(.systemBackground).ignoresSafeArea()
2026-04-05 00:27:29 -05:00
// Full-screen table padded down so it starts below the floating bar
GeometryReader { proxy in
ScrollView(.horizontal, showsIndicators: true) {
VStack(alignment: .leading, spacing: 0) {
2026-04-05 00:27:29 -05:00
columnHeaderRow
Divider()
ScrollView(.vertical, showsIndicators: true) {
LazyVStack(spacing: 0) {
ForEach(Array(chemicals.enumerated()), id: \.element.id) { idx, chemical in
2026-04-05 00:27:29 -05:00
dataRow(chemical: chemical, index: idx)
Divider()
}
}
}
2026-04-05 00:27:29 -05:00
.frame(height: max(0, proxy.size.height - Self.topBarHeight - Self.headerHeight - 1))
}
}
2026-04-05 00:27:29 -05:00
.padding(.top, Self.topBarHeight)
}
// Floating Liquid Glass top bar
HStack(spacing: 0) {
closeButton
.padding(.leading, 12)
Text("Inventory Spreadsheet")
.font(.headline)
.padding(.leading, 8)
Spacer()
Text("\(chemicals.count) item\(chemicals.count == 1 ? "" : "s")")
.font(.subheadline)
.foregroundStyle(.secondary)
.padding(.trailing, 16)
}
2026-04-05 00:27:29 -05:00
.frame(height: Self.topBarHeight)
.background(.regularMaterial)
}
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
setLandscape()
}
}
.onDisappear {
restorePortrait()
}
}
2026-04-05 00:27:29 -05:00
// MARK: - Subviews
private var columnHeaderRow: some View {
HStack(spacing: 0) {
ForEach(Array(Self.columnDefs.enumerated()), id: \.offset) { i, col in
Text(col.label)
.font(.caption.weight(.semibold))
.foregroundStyle(.white)
.lineLimit(2)
.multilineTextAlignment(.center)
// 📍 Move padding INSIDE
.padding(.horizontal, 4)
// 📍 Frame goes OUTSIDE to lock the total width safely
.frame(width: col.width, height: Self.headerHeight)
if i < Self.columnDefs.count - 1 {
Rectangle()
.fill(Color.white.opacity(0.3))
.frame(width: 1, height: Self.headerHeight)
}
}
}
.background(Color.accentColor)
}
private func dataRow(chemical: Chemical, index: Int) -> some View {
HStack(spacing: 0) {
ForEach(Array(Self.columnDefs.enumerated()), id: \.offset) { i, col in
Text(col.value(chemical))
.font(.caption)
.lineLimit(1)
// 📍 Move padding INSIDE
.padding(.vertical, 7)
.padding(.horizontal, 6)
// 📍 Frame goes OUTSIDE to lock the total width safely
.frame(width: col.width, alignment: .leading)
.background(index % 2 == 0
? Color(.systemBackground)
: Color(.systemGroupedBackground))
if i < Self.columnDefs.count - 1 {
Rectangle()
.fill(Color(.separator))
// 📍 Changed to 1px to perfectly match the header divider!
.frame(width: 1, height: 30)
}
}
}
}
/// Close button: Liquid Glass on iOS 26+, plain icon on older releases.
@ViewBuilder
private var closeButton: some View {
if #available(iOS 26.0, *) {
Button {
restorePortrait()
onDismiss()
} label: {
Image(systemName: "xmark")
.font(.system(size: 14, weight: .bold))
}
.buttonStyle(.glass)
} else {
Button {
restorePortrait()
onDismiss()
} label: {
Image(systemName: "xmark.circle.fill")
.font(.title2)
.foregroundStyle(.secondary)
}
}
}
// MARK: - Orientation
private func setLandscape() {
AppDelegate.orientationLock = .landscape
guard let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene else { return }
scene.requestGeometryUpdate(.iOS(interfaceOrientations: .landscape))
}
private func restorePortrait() {
AppDelegate.orientationLock = .allButUpsideDown
guard let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene else { return }
scene.requestGeometryUpdate(.iOS(interfaceOrientations: .portrait))
}
}