Add button to save

This commit is contained in:
june 2025-02-23 23:33:34 +13:00
parent 953a064de6
commit 63eacbb378
Signed by untrusted user who does not match committer: breadone
GPG Key ID: FDC19FE143200483

View File

@ -8,6 +8,9 @@
import SwiftUI import SwiftUI
struct AddEntryView: View { struct AddEntryView: View {
@Environment(\.presentationMode) var presentationMode
@Environment(\.modelContext) var ctx
@State private var t1: String = "" @State private var t1: String = ""
@State private var t2: String = "" @State private var t2: String = ""
@ -42,15 +45,29 @@ struct AddEntryView: View {
.navigationTitle("New Entry") .navigationTitle("New Entry")
.toolbar { .toolbar {
ToolbarItem(placement: .cancellationAction) { ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {} Button(action: { presentationMode.wrappedValue.dismiss() }) {
Text("Cancel")
.foregroundStyle(.red)
}
} }
ToolbarItem(placement: .confirmationAction) { ToolbarItem(placement: .confirmationAction) {
Button("Confirm") { Button(action: addItem) {
Text("Add")
}
}
}
}
}
} func addItem() {
} let newItem = Entry(
} odo: Int(Odometer) ?? 0,
} cost: Double(TotalCost) ?? 0,
quantity: Double(Litres) ?? 0,
fuelType: Location
)
ctx.insert(newItem)
try? ctx.save()
} }
} }