Files
Gas-Tank-AR-App/GasTankAR/Views/ProjectCreate.swift
2026-09-02 20:54:36 +02:00

83 lines
2.5 KiB
Swift

//
// 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
@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]
)
}
Section("Tankauswahl") {
TankEditor(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)
dismiss()
}
}
#Preview {
ProjectCreateView()
.modelContainer(for: Project.self, inMemory: true)
}