Einkaufsliste, Ablaufliste und Produktverwaltung in der App
Der Startbildschirm bekommt drei weitere Einstiege: Einkaufsliste (Artikel und
Gruppen unter Mindestbestand), Bald ablaufend (Chargen mit Restfrist, abgelaufene
rot) und Produkte. Alle Listen lassen sich durch Herunterziehen aktualisieren.
In den Produktdetails sind Name, Marke, Packungsgroesse, Gebinde-Bezeichnung und
MHD-Angabe aenderbar. Die Chargen werden mit Menge und MHD aufgelistet; Antippen
oeffnet die Korrektur von Menge und MHD (inklusive Umschalten auf Monat/Jahr),
Wischen loescht eine Charge. Mengen werden in der Leitangabe des Artikels
angezeigt und beim Speichern in Basiseinheiten zurueckgerechnet, weil die
Chargen-Schnittstelle in Basiseinheiten arbeitet.
Dabei ist ein bestehender Fehler aufgefallen und mitbehoben: Fuer das Gebinde
kennt das Backend nur das Schluesselwort "package" (services/conversion.py
akzeptiert package/packung/pkg/pack). Die App hat stattdessen die
Gebinde-Bezeichnung selbst als Einheit geschickt. Bei Produkten mit eigener
Bezeichnung - "Glas", "Dose", "Flasche", "Tuete" - schlugen Einlagern und
Auslagern damit mit "Unbekannte Einheit: Dose" fehl. Nur die Standardbezeichnung
"Packung" funktionierte zufaellig, weil sie in der Liste der Schluesselwoerter
steht. Anzeige und uebertragener Wert sind jetzt getrennt (neuer Typ
UnitOption): angezeigt wird weiterhin "Glas", geschickt wird "package".
Ausserdem die Warnung zu den Bildschirmausrichtungen behoben - das iPad verlangt
alle Ausrichtungen, solange die App nicht auf Vollbild besteht.
Getestet: iOS-Geraetebuild fehler- und warnungsfrei. Die Endpunkte hinter den
neuen Ansichten sind gegen die laufende API geprueft, ebenso das Aendern und
Loeschen von Chargen. Der Einheiten-Fehler wurde an der echten API
nachgestellt ("Unbekannte Einheit: Dose") und mit "package" als 200 bestaetigt.
Die neuen Ansichten habe ich nicht auf dem Geraet bedient.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
259
ios/Sources/ProductDetailView.swift
Normal file
259
ios/Sources/ProductDetailView.swift
Normal file
@@ -0,0 +1,259 @@
|
||||
import SwiftUI
|
||||
|
||||
/// Produkt ansehen und ändern, dazu die Chargen korrigieren oder löschen.
|
||||
struct ProductDetailView: View {
|
||||
let product: Product
|
||||
|
||||
@State private var current: Product
|
||||
@State private var lots: [Lot] = []
|
||||
@State private var busy = false
|
||||
@State private var error: String?
|
||||
@State private var status: String?
|
||||
|
||||
// Bearbeitbare Felder
|
||||
@State private var name = ""
|
||||
@State private var brand = ""
|
||||
@State private var packageSize = ""
|
||||
@State private var packageLabel = ""
|
||||
@State private var datePrecision = "day"
|
||||
|
||||
@State private var editLot: Lot?
|
||||
|
||||
private let labels = ["", "Glas", "Tüte", "Flasche", "Dose", "Tube", "Becher", "Beutel", "Karton"]
|
||||
|
||||
init(product: Product) {
|
||||
self.product = product
|
||||
_current = State(initialValue: product)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
if let status {
|
||||
Section { Text(status).foregroundStyle(.green).font(.callout) }
|
||||
}
|
||||
if let error {
|
||||
Section { Text(error).foregroundStyle(.red).font(.callout) }
|
||||
}
|
||||
|
||||
Section("Bestand") {
|
||||
LabeledContent("Vorrat",
|
||||
value: "\(formatAmount(current.stockInArticleUnits)) \(current.articleUnitLabel)")
|
||||
if current.expiredCount > 0 {
|
||||
LabeledContent("Abgelaufen", value: "\(current.expiredCount)")
|
||||
.foregroundStyle(.red)
|
||||
}
|
||||
}
|
||||
|
||||
Section("Artikel") {
|
||||
TextField("Name", text: $name)
|
||||
TextField("Marke", text: $brand)
|
||||
TextField("Packungsgröße (in \(current.baseUnit))", text: $packageSize)
|
||||
.keyboardType(.decimalPad)
|
||||
Picker("Bezeichnung", selection: $packageLabel) {
|
||||
ForEach(labels, id: \.self) { Text($0.isEmpty ? "Packung (Standard)" : $0).tag($0) }
|
||||
}
|
||||
Picker("MHD-Angabe", selection: $datePrecision) {
|
||||
Text("Tagesdatum").tag("day")
|
||||
Text("nur Monat/Jahr").tag("month")
|
||||
}
|
||||
}
|
||||
|
||||
Section {
|
||||
Button(busy ? "Speichern…" : "Änderungen speichern") { Task { await save() } }
|
||||
.disabled(busy || name.isEmpty)
|
||||
}
|
||||
|
||||
Section("Chargen") {
|
||||
ForEach(lots) { lot in
|
||||
Button {
|
||||
editLot = lot
|
||||
} label: {
|
||||
HStack {
|
||||
VStack(alignment: .leading, spacing: 2) {
|
||||
Text("MHD \(formatBestBefore(lot.bestBefore, precision: lot.bestBeforePrecision))")
|
||||
Text("\(formatAmount(lot.quantity / current.articleUnitFactor)) \(current.articleUnitLabel)")
|
||||
.font(.caption).foregroundStyle(.secondary)
|
||||
}
|
||||
Spacer()
|
||||
Image(systemName: "chevron.right").foregroundStyle(.tertiary)
|
||||
}
|
||||
}
|
||||
.foregroundStyle(.primary)
|
||||
}
|
||||
.onDelete { indexSet in
|
||||
Task { await deleteLots(at: indexSet) }
|
||||
}
|
||||
if lots.isEmpty {
|
||||
Text("Keine Chargen im Bestand.").foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationTitle(current.name)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.sheet(item: $editLot) { lot in
|
||||
NavigationStack {
|
||||
LotEditView(lot: lot, product: current) {
|
||||
Task { await reload() }
|
||||
}
|
||||
}
|
||||
}
|
||||
.task {
|
||||
fillForm()
|
||||
await reload()
|
||||
}
|
||||
}
|
||||
|
||||
private func fillForm() {
|
||||
name = current.name
|
||||
brand = current.brand ?? ""
|
||||
packageSize = current.packageSize.map { formatAmount($0) } ?? ""
|
||||
packageLabel = current.packageLabel ?? ""
|
||||
datePrecision = current.datePrecision == "month" ? "month" : "day"
|
||||
}
|
||||
|
||||
private func reload() async {
|
||||
lots = (try? await APIClient.shared.lots(productId: current.id)) ?? []
|
||||
if let frisch = try? await APIClient.shared.product(id: current.id) {
|
||||
current = frisch
|
||||
}
|
||||
}
|
||||
|
||||
private func save() async {
|
||||
error = nil
|
||||
status = nil
|
||||
busy = true
|
||||
defer { busy = false }
|
||||
do {
|
||||
current = try await APIClient.shared.updateProduct(
|
||||
id: current.id,
|
||||
ProductUpdateRequest(
|
||||
name: name,
|
||||
brand: brand.isEmpty ? nil : brand,
|
||||
packageSize: Double(packageSize.replacingOccurrences(of: ",", with: ".")),
|
||||
packageLabel: packageLabel.isEmpty ? nil : packageLabel,
|
||||
datePrecision: datePrecision,
|
||||
minStock: nil
|
||||
)
|
||||
)
|
||||
status = "Gespeichert."
|
||||
} catch {
|
||||
self.error = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
private func deleteLots(at indexSet: IndexSet) async {
|
||||
error = nil
|
||||
for index in indexSet {
|
||||
do {
|
||||
try await APIClient.shared.deleteLot(id: lots[index].id)
|
||||
} catch {
|
||||
self.error = error.localizedDescription
|
||||
}
|
||||
}
|
||||
await reload()
|
||||
}
|
||||
}
|
||||
|
||||
/// Menge und MHD einer Charge korrigieren.
|
||||
struct LotEditView: View {
|
||||
let lot: Lot
|
||||
let product: Product
|
||||
var onSaved: () -> Void
|
||||
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
@State private var quantity = ""
|
||||
@State private var hasDate = false
|
||||
@State private var bestBefore = Date()
|
||||
@State private var precision = "day"
|
||||
@State private var busy = false
|
||||
@State private var error: String?
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
Section("Menge") {
|
||||
HStack {
|
||||
Text(product.articleUnitLabel)
|
||||
Spacer(minLength: 12)
|
||||
TextField("Menge", text: $quantity)
|
||||
.keyboardType(.decimalPad)
|
||||
.multilineTextAlignment(.trailing)
|
||||
.frame(maxWidth: 110)
|
||||
}
|
||||
}
|
||||
|
||||
Section("MHD") {
|
||||
Toggle("MHD angeben", isOn: $hasDate)
|
||||
if hasDate {
|
||||
Picker("Angabe", selection: $precision) {
|
||||
Text("Tagesdatum").tag("day")
|
||||
Text("nur Monat/Jahr").tag("month")
|
||||
}
|
||||
if precision == "month" {
|
||||
MonthYearPicker(date: $bestBefore)
|
||||
} else {
|
||||
DatePicker("MHD", selection: $bestBefore, displayedComponents: .date)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let error {
|
||||
Section { Text(error).foregroundStyle(.red).font(.callout) }
|
||||
}
|
||||
|
||||
Section {
|
||||
Button(busy ? "Speichern…" : "Speichern") { Task { await save() } }
|
||||
.disabled(busy)
|
||||
}
|
||||
}
|
||||
.navigationTitle("Charge ändern")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .topBarLeading) { Button("Abbrechen") { dismiss() } }
|
||||
}
|
||||
.onAppear(perform: fill)
|
||||
}
|
||||
|
||||
private func fill() {
|
||||
quantity = formatAmount(lot.quantity / product.articleUnitFactor)
|
||||
precision = lot.bestBeforePrecision == "month" ? "month" : "day"
|
||||
if let raw = lot.bestBefore {
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateFormat = "yyyy-MM-dd"
|
||||
if let datum = formatter.date(from: raw) {
|
||||
bestBefore = datum
|
||||
hasDate = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func save() async {
|
||||
error = nil
|
||||
guard let menge = Double(quantity.replacingOccurrences(of: ",", with: ".")), menge > 0 else {
|
||||
error = "Bitte eine Menge größer 0 angeben."
|
||||
return
|
||||
}
|
||||
busy = true
|
||||
defer { busy = false }
|
||||
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateFormat = "yyyy-MM-dd"
|
||||
let datum = precision == "month" ? bestBefore.startOfMonth : bestBefore
|
||||
|
||||
do {
|
||||
// Das Backend rechnet Chargenmengen in Basiseinheiten.
|
||||
_ = try await APIClient.shared.updateLot(
|
||||
id: lot.id,
|
||||
LotUpdateRequest(
|
||||
quantity: menge * product.articleUnitFactor,
|
||||
bestBefore: hasDate ? formatter.string(from: datum) : nil,
|
||||
bestBeforePrecision: precision
|
||||
)
|
||||
)
|
||||
onSaved()
|
||||
dismiss()
|
||||
} catch {
|
||||
self.error = error.localizedDescription
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user