Files
todos/App/Sources/Views/ContentView.swift
T

64 lines
1.7 KiB
Swift
Raw Normal View History

2026-02-08 18:01:45 +01:00
import SwiftData
2026-02-08 20:20:11 +00:00
import SwiftUI
2026-02-08 18:01:45 +01:00
struct ContentView: View {
2026-02-08 20:20:11 +00:00
@Environment(\.modelContext)
private var modelContext
@Query
2026-02-09 15:40:49 +01:00
private var items: [Todo]
2026-02-08 18:01:45 +01:00
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))
}
}
.onDelete(perform: deleteItems)
}
2026-02-08 20:20:11 +00:00
#if os(macOS)
2026-02-08 18:01:45 +01:00
.navigationSplitViewColumnWidth(min: 180, ideal: 200)
2026-02-08 20:20:11 +00:00
#endif
2026-02-08 18:01:45 +01:00
.toolbar {
2026-02-08 20:20:11 +00:00
#if os(iOS)
2026-02-08 18:01:45 +01:00
ToolbarItem(placement: .navigationBarTrailing) {
EditButton()
}
2026-02-08 20:20:11 +00:00
#endif
2026-02-08 18:01:45 +01:00
ToolbarItem {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
} detail: {
Text("Select an item")
}
}
private func addItem() {
withAnimation {
2026-02-09 15:40:49 +01:00
let newItem = Todo(title: "New Todo", due: Date())
2026-02-08 18:01:45 +01:00
modelContext.insert(newItem)
}
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
for index in offsets {
modelContext.delete(items[index])
}
}
}
}
#Preview {
ContentView()
2026-02-09 15:40:49 +01:00
.modelContainer(for: Todo.self, inMemory: true)
2026-02-08 18:01:45 +01:00
}