Add Entry model

This commit is contained in:
june 2025-02-23 22:27:24 +13:00
parent ccc7efbd63
commit 078719ef2b
Signed by untrusted user who does not match committer: breadone
GPG Key ID: FDC19FE143200483
4 changed files with 35 additions and 30 deletions

View File

@ -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)
}

27
gastrack/Entry.swift Normal file
View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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)