From 46e61bae00853a9b014ae5d9759a6f54cecb88f5 Mon Sep 17 00:00:00 2001 From: pulipakaa24 Date: Sun, 5 Apr 2026 00:15:25 -0500 Subject: [PATCH] Working spreadsheet view, pending UI updates --- LabWise/InventoryView.swift | 13 +++ LabWise/LabWiseApp.swift | 15 ++++ LabWise/SpreadsheetView.swift | 164 ++++++++++++++++++++++++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 LabWise/SpreadsheetView.swift diff --git a/LabWise/InventoryView.swift b/LabWise/InventoryView.swift index a246775..883bbc8 100644 --- a/LabWise/InventoryView.swift +++ b/LabWise/InventoryView.swift @@ -47,6 +47,7 @@ struct InventoryView: View { @State private var addMode: AddMode? @State private var isSelectMode = false @State private var selectedIDs: Set = [] + @State private var showSpreadsheet = false enum AddMode: String, Identifiable { case manual, scan @@ -141,6 +142,13 @@ struct InventoryView: View { .disabled(selectedIDs.isEmpty) } } else { + ToolbarItem(placement: .topBarLeading) { + Button { + showSpreadsheet = true + } label: { + Image(systemName: "tablecells") + } + } ToolbarItem(placement: .topBarTrailing) { Menu { Button { @@ -184,5 +192,10 @@ struct InventoryView: View { .task { await viewModel.loadChemicals() } + .fullScreenCover(isPresented: $showSpreadsheet) { + SpreadsheetView(chemicals: viewModel.chemicals) { + showSpreadsheet = false + } + } } } diff --git a/LabWise/LabWiseApp.swift b/LabWise/LabWiseApp.swift index c109b95..4f0e33a 100644 --- a/LabWise/LabWiseApp.swift +++ b/LabWise/LabWiseApp.swift @@ -1,8 +1,23 @@ import SwiftUI import LabWiseKit +import UIKit + +// Controls which orientations are allowed app-wide at runtime. +// SpreadsheetView flips this to .landscape; all other screens use .allButUpsideDown. +final class AppDelegate: NSObject, UIApplicationDelegate { + static var orientationLock: UIInterfaceOrientationMask = .allButUpsideDown + + func application( + _ application: UIApplication, + supportedInterfaceOrientationsFor window: UIWindow? + ) -> UIInterfaceOrientationMask { + AppDelegate.orientationLock + } +} @main struct LabWiseApp: App { + @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate @State private var appState = AppState.shared var body: some Scene { diff --git a/LabWise/SpreadsheetView.swift b/LabWise/SpreadsheetView.swift new file mode 100644 index 0000000..c791431 --- /dev/null +++ b/LabWise/SpreadsheetView.swift @@ -0,0 +1,164 @@ +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 ?? "" }, + 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)) } ?? "" + }, + ] + + private static let headerHeight: CGFloat = 48 + + var body: some View { + VStack(spacing: 0) { + // Top bar + HStack(spacing: 4) { + Button { + restorePortrait() + onDismiss() + } label: { + Image(systemName: "xmark.circle.fill") + .font(.title2) + .foregroundStyle(.secondary) + } + .padding(.leading, 12) + + Text("Inventory Spreadsheet") + .font(.headline) + .padding(.leading, 4) + + Spacer() + + Text("\(chemicals.count) item\(chemicals.count == 1 ? "" : "s")") + .font(.subheadline) + .foregroundStyle(.secondary) + .padding(.trailing, 16) + } + .frame(height: 50) + .background(Color(.systemBackground)) + + Divider() + + // Table + GeometryReader { proxy in + ScrollView(.horizontal, showsIndicators: true) { + VStack(alignment: .leading, spacing: 0) { + // Sticky header row + 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) + .frame(width: col.width, height: Self.headerHeight) + .padding(.horizontal, 4) + + if i < Self.columnDefs.count - 1 { + Rectangle() + .fill(Color.white.opacity(0.35)) + .frame(width: 1, height: Self.headerHeight) + } + } + } + .background(Color.accentColor) + + Divider() + + // Scrollable data rows + ScrollView(.vertical, showsIndicators: true) { + LazyVStack(spacing: 0) { + ForEach(Array(chemicals.enumerated()), id: \.element.id) { idx, chemical in + HStack(spacing: 0) { + ForEach(Array(Self.columnDefs.enumerated()), id: \.offset) { i, col in + Text(col.value(chemical)) + .font(.caption) + .lineLimit(1) + .frame(width: col.width, alignment: .leading) + .padding(.vertical, 7) + .padding(.horizontal, 6) + .background(idx % 2 == 0 + ? Color(.systemBackground) + : Color(.systemGray6)) + + if i < Self.columnDefs.count - 1 { + Rectangle() + .fill(Color(.separator)) + .frame(width: 0.5) + .frame(height: 30) + } + } + } + Divider() + } + } + } + // Subtract header height and the 1-pt divider + .frame(height: proxy.size.height - Self.headerHeight - 1) + } + } + } + } + .onAppear { + // Slight delay so the full-screen cover animation finishes before rotating + DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) { + setLandscape() + } + } + .onDisappear { + // Safety net in case something else dismisses this view + restorePortrait() + } + } + + // MARK: - Orientation helpers + + 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)) + } +}