| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- //
- // ContentView.swift
- // Todos
- //
- // Created by Sam Jaffe on 2/28/26.
- //
- import SwiftUI
- import SwiftData
- struct ContentView: View {
- @Environment(\.modelContext) private var modelContext
-
- @Query private var items: [Category]
-
- @State private var showingPopup = false
- @State private var currentHint: URLHint = URLHint()
-
- var body: some View {
- NavigationSplitView {
- List {
- ForEach(items) { item in
- NavigationLink {
- CategoryPanelView(item: item)
- } label: {
- CategorySidebarView(name: Bindable(item).name)
- } .contextMenu {
- Button(action: { deleteItem(item: item) }) {
- Label("Delete", systemImage: "trash")
- }
- }
- }
- .onDelete(perform: deleteItems)
- }
- .navigationSplitViewColumnWidth(min: 180, ideal: 200)
- .toolbar {
- ToolbarItem {
- Button(action: addItem) {
- Label("New Category", systemImage: "plus")
- }
- }
- }
- } detail: {
- Text("Select an item")
- }
- }
- private func addItem() {
- withAnimation {
- let newItem = Category(timestamp: Date())
- let count = items.count(where: { $0.name.starts(with: "New Category") })
- if (count > 0) {
- newItem.name += " (\(count))"
- }
- modelContext.insert(newItem)
- }
- }
-
- private func deleteItem(item: Category) {
- withAnimation {
- modelContext.delete(item)
- }
- }
- private func deleteItems(offsets: IndexSet) {
- withAnimation {
- for index in offsets {
- modelContext.delete(items[index])
- }
- }
- }
- }
- #Preview {
- ContentView()
- .modelContainer(for: Category.self, inMemory: true)
- .environmentObject(URLHintArray())
- }
|