Browse Source

feat: make Category, with an editable name

Sam Jaffe 3 weeks ago
parent
commit
60b95d7b4d

+ 0 - 59
Todos/ContentView.swift

@@ -1,59 +0,0 @@
-//
-//  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: [Item]
-
-    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)
-            }
-            .navigationSplitViewColumnWidth(min: 180, ideal: 200)
-            .toolbar {
-                ToolbarItem {
-                    Button(action: addItem) {
-                        Label("Add Item", systemImage: "plus")
-                    }
-                }
-            }
-        } detail: {
-            Text("Select an item")
-        }
-    }
-
-    private func addItem() {
-        withAnimation {
-            let newItem = Item(timestamp: Date())
-            modelContext.insert(newItem)
-        }
-    }
-
-    private func deleteItems(offsets: IndexSet) {
-        withAnimation {
-            for index in offsets {
-                modelContext.delete(items[index])
-            }
-        }
-    }
-}
-
-#Preview {
-    ContentView()
-        .modelContainer(for: Item.self, inMemory: true)
-}

+ 0 - 18
Todos/Item.swift

@@ -1,18 +0,0 @@
-//
-//  Item.swift
-//  Todos
-//
-//  Created by Sam Jaffe on 2/28/26.
-//
-
-import Foundation
-import SwiftData
-
-@Model
-final class Item {
-    var timestamp: Date
-    
-    init(timestamp: Date) {
-        self.timestamp = timestamp
-    }
-}

+ 20 - 0
Todos/Model/Category.swift

@@ -0,0 +1,20 @@
+//
+//  Category.swift
+//  Todos
+//
+//  Created by Sam Jaffe on 2/28/26.
+//
+
+import Foundation
+import SwiftData
+
+@Model
+final class Category {
+  var timestamp: Date
+  var name: String
+  
+  init(timestamp: Date) {
+    self.timestamp = timestamp
+    self.name = "New Category"
+  }
+}

+ 16 - 16
Todos/TodosApp.swift

@@ -10,23 +10,23 @@ import SwiftData
 
 @main
 struct TodosApp: App {
-    var sharedModelContainer: ModelContainer = {
-        let schema = Schema([
-            Item.self,
-        ])
-        let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
+  var sharedModelContainer: ModelContainer = {
+    let schema = Schema([
+      Category.self,
+    ])
+    let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
 
-        do {
-            return try ModelContainer(for: schema, configurations: [modelConfiguration])
-        } catch {
-            fatalError("Could not create ModelContainer: \(error)")
-        }
-    }()
+    do {
+      return try ModelContainer(for: schema, configurations: [modelConfiguration])
+    } catch {
+      fatalError("Could not create ModelContainer: \(error)")
+    }
+  }()
 
-    var body: some Scene {
-        WindowGroup {
-            ContentView()
-        }
-        .modelContainer(sharedModelContainer)
+  var body: some Scene {
+    WindowGroup {
+        ContentView()
     }
+    .modelContainer(sharedModelContainer)
+  }
 }

+ 22 - 0
Todos/VIew/CategoryView.swift

@@ -0,0 +1,22 @@
+//
+//  CategoryView.swift
+//  Todos
+//
+//  Created by Sam Jaffe on 2/28/26.
+//
+
+import SwiftUI
+import SwiftData
+
+struct CategoryView: View {
+  @Binding var name: String;
+
+  var body: some View {
+    TextField("Category", text: $name)
+  }
+}
+
+#Preview {
+  @Previewable @State var name = "New Category"
+  CategoryView(name: $name)
+}

+ 59 - 0
Todos/VIew/ContentView.swift

@@ -0,0 +1,59 @@
+//
+//  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]
+
+  var body: some View {
+    NavigationSplitView {
+      List {
+        ForEach(items) { item in
+          NavigationLink {
+            Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard)): \(item.name)")
+          } label: {
+            CategoryView(name: Bindable(item).name)
+          }
+        }
+        .onDelete(perform: deleteItems)
+      }
+      .navigationSplitViewColumnWidth(min: 180, ideal: 200)
+      .toolbar {
+        ToolbarItem {
+          Button(action: addItem) {
+            Label("Add Item", systemImage: "plus")
+          }
+        }
+      }
+    } detail: {
+        Text("Select an item")
+    }
+  }
+
+  private func addItem() {
+    withAnimation {
+      let newItem = Category(timestamp: Date())
+      modelContext.insert(newItem)
+    }
+  }
+
+  private func deleteItems(offsets: IndexSet) {
+    withAnimation {
+      for index in offsets {
+        modelContext.delete(items[index])
+      }
+    }
+  }
+}
+
+#Preview {
+  ContentView()
+      .modelContainer(for: Category.self, inMemory: true)
+}