Add Entry model
This commit is contained in:
parent
ccc7efbd63
commit
078719ef2b
@ -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
27
gastrack/Entry.swift
Normal 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
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user