Add button to save
This commit is contained in:
parent
953a064de6
commit
63eacbb378
@ -8,6 +8,9 @@
|
||||
import SwiftUI
|
||||
|
||||
struct AddEntryView: View {
|
||||
@Environment(\.presentationMode) var presentationMode
|
||||
@Environment(\.modelContext) var ctx
|
||||
|
||||
@State private var t1: String = ""
|
||||
@State private var t2: String = ""
|
||||
|
||||
@ -42,16 +45,30 @@ struct AddEntryView: View {
|
||||
.navigationTitle("New Entry")
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
Button("Cancel") {}
|
||||
Button(action: { presentationMode.wrappedValue.dismiss() }) {
|
||||
Text("Cancel")
|
||||
.foregroundStyle(.red)
|
||||
}
|
||||
}
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
|
Loading…
x
Reference in New Issue
Block a user