84 lines
2.3 KiB
Swift
84 lines
2.3 KiB
Swift
//
|
|
// AddEntryView.swift
|
|
// gastrack
|
|
//
|
|
// Created by Pradyun Setti on 23/02/2025.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
struct AddEntryView: View {
|
|
@Environment(\.presentationMode) var presentationMode
|
|
@Environment(\.modelContext) var ctx
|
|
|
|
@State private var t1: String = ""
|
|
@State private var t2: String = ""
|
|
|
|
@State private var Odometer = ""
|
|
@State private var CostPerLitre = ""
|
|
@State private var Litres = ""
|
|
@State private var TotalCost = ""
|
|
@State private var FuelType = ""
|
|
@State private var Location = ""
|
|
|
|
var body: some View {
|
|
NavigationView {
|
|
Form {
|
|
HStack() {
|
|
Text("Last: \(72398)")
|
|
Divider()
|
|
TextField("Odometer", text: $Odometer)
|
|
|
|
}
|
|
|
|
HStack {
|
|
TextField("Cost/L", text: $CostPerLitre)
|
|
Divider()
|
|
TextField("Litres", text: $Litres)
|
|
Divider()
|
|
TextField("Total Cost", text: $TotalCost)
|
|
}
|
|
|
|
TextField("Fuel Type", text: $FuelType)
|
|
TextField("Location", text: $Location)
|
|
}
|
|
.navigationTitle("New Entry")
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
Button(action: { presentationMode.wrappedValue.dismiss() }) {
|
|
Text("Cancel")
|
|
.foregroundStyle(.red)
|
|
}
|
|
}
|
|
ToolbarItem(placement: .confirmationAction) {
|
|
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 {
|
|
NavigationView {
|
|
Text("s")
|
|
.sheet(isPresented: Binding(get: {true}, set: {_ in})) {
|
|
AddEntryView()
|
|
.presentationDetents([.medium])
|
|
.presentationCornerRadius(20)
|
|
}
|
|
}
|
|
}
|