Files
Vorrania/ios/Sources/ProductViews.swift
Scarriffle 3a0c130cb1 iOS-App kompiliert erstmals: Info.plist gesichert und zwei Build-Fehler behoben
Die App war bisher nie durch einen Compiler gelaufen. Mit Xcode 26.6 baut sie
jetzt fehler- und warnungsfrei gegen das iOS-16-Deployment-Target.

Info.plist: Der "info:"-Block in project.yml hat die handgepflegte
Sources/Info.plist bei jedem "xcodegen generate" ueberschrieben und dabei
Schluessel verloren, die nur in der Datei standen - insbesondere
NSAppTransportSecurity/NSAllowsLocalNetworking. Ohne diese Ausnahme haette die
App den Server im Heimnetz per http:// ueberhaupt nicht erreichen koennen.
Die Datei ist jetzt die einzige Quelle: project.yml verweist nur noch per
INFOPLIST_FILE darauf und nimmt sie aus den Sources heraus. Die Bundle-Keys,
die vorher nur die erzeugte Fassung hatte (CFBundleExecutable,
CFBundleIdentifier, CFBundlePackageType ...), stehen nun in der Datei selbst.

ProductViews: onChange(of:) wurde in der zweiwertigen iOS-17-Form verwendet und
war damit ein harter Fehler - auf die einwertige Form umgestellt.

ProjectGoodApp: application(_:performActionFor:) als async-Fassung reicht das
nicht-Sendable UIApplicationShortcutItem ueber eine Actor-Grenze. Das ist heute
eine Warnung und im Swift-6-Sprachmodus ein Fehler; jetzt die Variante mit
Completion-Handler.

Der dritte Verdacht hat sich nicht bestaetigt: AudioServicesPlaySystemSound in
ScannerView uebersetzt ohne zusaetzliches "import AudioToolbox", weil
AVFoundation es bereits mitbringt. Deshalb dort keine Aenderung.

Das erzeugte ProjectGood.xcodeproj wandert in .gitignore, da project.yml es
vollstaendig beschreibt.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-22 16:57:27 +02:00

137 lines
4.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import SwiftUI
/// Artikel per Namenssuche auswählen (Alternative zum Scannen).
struct ProductSearchView: View {
var onPick: (Product) -> Void
@Environment(\.dismiss) private var dismiss
@State private var query = ""
@State private var results: [Product] = []
@State private var busy = false
var body: some View {
List {
ForEach(results) { product in
Button {
onPick(product)
dismiss()
} label: {
VStack(alignment: .leading, spacing: 2) {
Text(product.name)
Text("\(formatted(product.stockInArticleUnits)) \(product.articleUnitLabel)")
.font(.caption).foregroundStyle(.secondary)
}
}
}
if results.isEmpty && !busy {
Text("Keine Treffer").foregroundStyle(.secondary)
}
}
.searchable(text: $query, prompt: "Artikel suchen")
.onSubmit(of: .search) { Task { await search() } }
.task { await search() }
.onChange(of: query) { _ in Task { await search() } }
.navigationTitle("Artikel suchen")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .topBarLeading) { Button("Abbrechen") { dismiss() } }
}
}
private func formatted(_ value: Double) -> String {
value == value.rounded() ? String(Int(value)) : String(format: "%.2f", value)
}
private func search() async {
busy = true
defer { busy = false }
results = (try? await APIClient.shared.searchProducts(query)) ?? []
}
}
/// Neues Produkt anlegen bei Bedarf mit den Daten von Open Food Facts vorbefüllt.
struct ProductFormView: View {
let prefillBarcode: String?
let groupId: Int?
var suggestion: LookupResult.Suggestion?
var onCreated: (Product) -> Void
@Environment(\.dismiss) private var dismiss
@State private var barcode = ""
@State private var name = ""
@State private var brand = ""
@State private var baseUnit = "piece"
@State private var packageSize = ""
@State private var packageLabel = ""
@State private var busy = false
@State private var error: String?
private let baseUnits = [("piece", "Stück"), ("gram", "Gramm"), ("milliliter", "Milliliter")]
private let labels = ["", "Glas", "Tüte", "Flasche", "Dose", "Tube", "Becher", "Beutel", "Karton"]
var body: some View {
Form {
Section("Artikel") {
TextField("Barcode", text: $barcode).keyboardType(.numberPad)
TextField("Name", text: $name)
TextField("Marke", text: $brand)
}
Section("Einheit") {
Picker("Basiseinheit", selection: $baseUnit) {
ForEach(baseUnits, id: \.0) { Text($0.1).tag($0.0) }
}
TextField("Packungsgröße (in Basiseinheit)", text: $packageSize)
.keyboardType(.decimalPad)
Picker("Bezeichnung", selection: $packageLabel) {
ForEach(labels, id: \.self) { Text($0.isEmpty ? "Packung (Standard)" : $0).tag($0) }
}
}
if let error {
Section { Text(error).foregroundStyle(.red).font(.callout) }
}
Section {
Button(busy ? "Anlegen…" : "Anlegen & weiter") { Task { await create() } }
.disabled(busy || name.isEmpty)
}
}
.navigationTitle("Neuer Artikel")
.navigationBarTitleDisplayMode(.inline)
.onAppear(perform: prefill)
}
private func prefill() {
barcode = suggestion?.barcode ?? prefillBarcode ?? ""
if let suggestion {
name = suggestion.name
brand = suggestion.brand ?? ""
baseUnit = suggestion.baseUnit ?? "piece"
if let size = suggestion.packageSize { packageSize = String(size) }
}
}
private func create() async {
error = nil
busy = true
defer { busy = false }
do {
let product = try await APIClient.shared.createProduct(
NewProductRequest(
barcode: barcode.isEmpty ? nil : barcode,
name: name,
brand: brand.isEmpty ? nil : brand,
imageUrl: suggestion?.imageUrl,
baseUnit: baseUnit,
packageSize: Double(packageSize.replacingOccurrences(of: ",", with: ".")),
packageLabel: packageLabel.isEmpty ? nil : packageLabel,
groupId: groupId
)
)
onCreated(product)
dismiss()
} catch {
self.error = error.localizedDescription
}
}
}