// // ProjectCreate.swift // GasTankAR // // Created by Dennis Nemec on 25.08.26. // import SwiftUI import SwiftData struct ProjectCreateView: View { @Environment(\.modelContext) private var modelContext @Environment(\.dismiss) private var dismiss let onCreated: (Project) -> Void @State private var leadID: String = "" @State private var customerName: String = "" @State private var creationDate: Date = .now @State private var tank: Tank = Tank() /// Whether the entered values are sufficient to create a project. private var canSave: Bool { !leadID.trimmingCharacters(in: .whitespaces).isEmpty && !customerName.trimmingCharacters(in: .whitespaces).isEmpty } var body: some View { NavigationStack { Form { Section("Projektdaten") { TextField("Lead-ID", text: $leadID) .textInputAutocapitalization(.characters) .autocorrectionDisabled() TextField("Kundenname", text: $customerName) .textInputAutocapitalization(.words) DatePicker( "Erstellungsdatum", selection: $creationDate, displayedComponents: [.date] ) } TankSelectionSection(tank: $tank) } .navigationTitle("Projekt erstellen") .navigationBarTitleDisplayMode(.inline) .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Abbrechen", systemImage: "xmark") { dismiss() } } ToolbarItem(placement: .confirmationAction) { Button("Speichern", systemImage: "checkmark") { save() } .buttonStyle(.borderedProminent) .disabled(!canSave) } } } } private func save() { let project = Project( leadID: leadID.trimmingCharacters(in: .whitespaces), customerName: customerName.trimmingCharacters(in: .whitespaces), creationDate: creationDate, tank: tank ) modelContext.insert(project) onCreated(project) dismiss() } } #Preview { ProjectCreateView { _ in } .modelContainer(for: Project.self, inMemory: true) }