Przeglądaj źródła

refactor: pull out CategoryPicker and CategoryColorDisplay for brevity

Sam Jaffe 2 tygodni temu
rodzic
commit
77f4340966

+ 25 - 0
Todos/View/Components/CategoryColorDisplay.swift

@@ -0,0 +1,25 @@
+//
+//  CategoryColorDisplay.swift
+//  Todos
+//
+//  Created by Sam Jaffe on 3/7/26.
+//
+
+import SwiftUI
+
+struct CategoryColorDisplay: View {
+  @AppStorage(UserDefaultsKeys.Category) var allGroups = CodableArray<Category>()
+
+  @Binding var category: String
+
+  var body: some View {
+    if let grp = $allGroups.first(where: { $0.name.wrappedValue == category }) {
+      ColorPicker("", selection: grp.color).disabled(true).scaledToFit()
+    }
+  }
+}
+
+#Preview {
+  @Previewable @State var category = ""
+  CategoryColorDisplay(category: $category)
+}

+ 29 - 0
Todos/View/Components/CategoryPicker.swift

@@ -0,0 +1,29 @@
+//
+//  CategoryPicker.swift
+//  Todos
+//
+//  Created by Sam Jaffe on 3/7/26.
+//
+
+import SwiftUI
+
+struct CategoryPicker: View {
+  @AppStorage(UserDefaultsKeys.Category) var allGroups = CodableArray<Category>()
+  @State private var empty = Category()
+
+  @Binding var category: String
+
+  var body: some View {
+    Picker("Category", selection: $category) {
+      Text(empty.name).tag("")
+      ForEach(allGroups) { group in
+        Text(group.name)
+      }
+    }
+  }
+}
+
+#Preview {
+  @Previewable @State var category = ""
+  CategoryPicker(category: $category)
+}

+ 2 - 11
Todos/View/ProjectPanelView.swift

@@ -10,10 +10,8 @@ import SwiftData
 
 struct ProjectPanelView: View {
   @Environment(\.modelContext) private var modelContext
-  @AppStorage(UserDefaultsKeys.Category) var allGroups = CodableArray<Category>()
 
   @Bindable var item: Project
-  @State private var empty = Category()
 
   @State private var showDialogue = false
   @State private var move = false
@@ -25,9 +23,7 @@ struct ProjectPanelView: View {
       TextField("Project Name", text: $item.name)
         .font(.title)
         .padding(.leading, 10)
-      if let grp = $allGroups.first(where: { $0.name.wrappedValue == item.category }) {
-        ColorPicker("", selection: grp.color).disabled(true).scaledToFit()
-      }
+      CategoryColorDisplay(category: $item.category)
       Spacer()
       Button(action: addItem) {
         Image(systemName: "plus")
@@ -64,12 +60,7 @@ struct ProjectPanelView: View {
         List {
           Label("Settings", systemImage: "gearshape")
             .font(.title)
-          Picker("Category", selection: $item.category) {
-            Text(empty.name).tag("")
-            ForEach(allGroups) { group in
-              Text(group.name)
-            }
-          }
+          CategoryPicker(category: $item.category)
           Label("Filters", systemImage: "magnifyingglass")
             .font(.title)
           HStack {

+ 3 - 12
Todos/View/TaskView.swift

@@ -10,13 +10,11 @@ import SwiftData
 
 struct TaskView: View {
   @Environment(\.modelContext) private var modelContext
-  @AppStorage(UserDefaultsKeys.Category) var allGroups = CodableArray<Category>()
   @Binding var task: Task
 
   @State private var showDialogue: Bool = false
   @State private var hideTags: Bool = false
   @State private var hideNotes: Bool = false
-  @State private var empty = Category()
   let unset: Priority? = nil
 
   @FocusState private var isFocused: Bool
@@ -44,9 +42,7 @@ struct TaskView: View {
         TextField("Task Name", text: $task.name)
           .focused($isFocused)
 
-        if let grp = $allGroups.first(where: { $0.name.wrappedValue == task.category }) {
-          ColorPicker("", selection: grp.color).disabled(true).scaledToFit()
-        }
+        CategoryColorDisplay(category: $task.category)
 
         Button(action: addItem) {
           Image(systemName: "plus")
@@ -62,13 +58,8 @@ struct TaskView: View {
         .buttonStyle(.borderless)
         .popover(isPresented: $showDialogue) {
           List {
-            Picker("Category", selection: $task.category) {
-              Text(empty.name).tag("")
-              ForEach(allGroups) { group in
-                Text(group.name)
-              }
-            }
-            .fixedSize(horizontal: true, vertical: false)
+            CategoryPicker(category: $task.category)
+              .fixedSize(horizontal: true, vertical: false)
 
             Picker("Priority", selection: $task.priority) {
               Text("").tag(unset)