My workaround:
//
// ContentView.swift
// exampleBug work around
//
// This works by using an order attribute in the Item class and a nextOrder state variable
// The nextOrder always starts out as 0, which causes the view to call itself sending the count + 1 of the AllItems array from the query.
// If anyone can find a way to use the results of the query without needing to call the view a second time, that would be great.
//
import SwiftUI
import SwiftData
@Model
final class Item {
var timestamp: Date
var order: Int
var checkbox: Bool = false
init(timestamp: Date, order: Int) {
self.timestamp = timestamp
self.order = order
}
}
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
@State private var editMode = EditMode.inactive
@State var nextOrder = 0
@Query(sort: [
SortDescriptor(\Item.order),
]
) public var AllItems: [Item]
var body: some View {
if(nextOrder == 0) {
ContentView.init(nextOrder: AllItems.count + 1)
} else {
NavigationStack {
List {
ForEach(AllItems) { item in
HStack {
Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))
Text(item.order, format: .number)
Button("", systemImage: item.checkbox ? "checkmark.circle.fill" : "circle") {
item.checkbox.toggle()
try? modelContext.save()
}
}
}
.onMove(perform: { indices, newOffset in
var theItems = AllItems
theItems.move(fromOffsets: indices, toOffset: newOffset)
var order: Int = 1
theItems.forEach
{ list in
list.order = order
order += 1
}
})
}
.environment(\.editMode, $editMode)
.moveDisabled(false)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
EditButton()
}
ToolbarItem {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
}
}
}
func addItem() {
withAnimation {
let newItem = Item(timestamp: Date(), order: nextOrder)
self.nextOrder += 1
modelContext.insert(newItem)
}
}
func deleteItems(offsets: IndexSet) {
withAnimation {
for index in offsets {
modelContext.delete(AllItems[index])
}
}
}
}
#Preview {
ContentView()
.modelContainer(for: Item.self, inMemory: true)
}```