diff --git a/gastrack/ContentView.swift b/gastrack/ContentView.swift index ee2ae4e..11b2f54 100644 --- a/gastrack/ContentView.swift +++ b/gastrack/ContentView.swift @@ -10,19 +10,14 @@ import SwiftData struct ContentView: View { @Environment(\.modelContext) private var modelContext - @Query private var items: [Item] + @Query private var entries: [Entry] var body: some View { NavigationSplitView { List { - ForEach(items) { item in - NavigationLink { - Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))") - } label: { - Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard)) - } + ForEach(entries) { e in + Text("Entry: \(e.odo)") } - .onDelete(perform: deleteItems) } .toolbar { ToolbarItem(placement: .navigationBarTrailing) { @@ -41,15 +36,16 @@ struct ContentView: View { private func addItem() { withAnimation { - let newItem = Item(timestamp: Date()) + let newItem = Entry(odo: 75239, cost: 92.19, quantity: 36.34, fuelType: "91") modelContext.insert(newItem) + try? modelContext.save() } } private func deleteItems(offsets: IndexSet) { withAnimation { for index in offsets { - modelContext.delete(items[index]) + modelContext.delete(entries[index]) } } } @@ -57,5 +53,5 @@ struct ContentView: View { #Preview { ContentView() - .modelContainer(for: Item.self, inMemory: true) + .modelContainer(for: Entry.self, inMemory: true) } diff --git a/gastrack/Entry.swift b/gastrack/Entry.swift new file mode 100644 index 0000000..6aad292 --- /dev/null +++ b/gastrack/Entry.swift @@ -0,0 +1,27 @@ +// +// Entry.swift +// gastrack +// +// Created by Pradyun Setti on 23/02/2025. +// + +import Foundation +import SwiftData + +@Model +final class Entry { + var id: UUID + + var odo: Int + var cost: Double + var quantity: Double + var fuelType: String + + public init(odo: Int, cost: Double, quantity: Double, fuelType: String) { + self.id = UUID() + self.odo = odo + self.cost = cost + self.quantity = quantity + self.fuelType = fuelType + } +} diff --git a/gastrack/Item.swift b/gastrack/Item.swift deleted file mode 100644 index ad8182f..0000000 --- a/gastrack/Item.swift +++ /dev/null @@ -1,18 +0,0 @@ -// -// Item.swift -// gastrack -// -// Created by Pradyun Setti on 23/02/2025. -// - -import Foundation -import SwiftData - -@Model -final class Item { - var timestamp: Date - - init(timestamp: Date) { - self.timestamp = timestamp - } -} diff --git a/gastrack/gastrackApp.swift b/gastrack/gastrackApp.swift index 9c8557e..a054da2 100644 --- a/gastrack/gastrackApp.swift +++ b/gastrack/gastrackApp.swift @@ -12,7 +12,7 @@ import SwiftData struct gastrackApp: App { var sharedModelContainer: ModelContainer = { let schema = Schema([ - Item.self, + Entry.self, ]) let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)