Bläddra i källkod

refactor: flatten SubTask view creation into ProjectPanelView, allowing proper contextmenu

Sam Jaffe 2 veckor sedan
förälder
incheckning
ce17ad4844
3 ändrade filer med 28 tillägg och 28 borttagningar
  1. 1 1
      Todos/TodosApp.swift
  2. 27 13
      Todos/View/ProjectPanelView.swift
  3. 0 14
      Todos/View/TaskView.swift

+ 1 - 1
Todos/TodosApp.swift

@@ -35,7 +35,7 @@ struct TodosApp: App {
 
   var body: some Scene {
     WindowGroup {
-      ContentView(isMoveMode: $isMoveMode).onAppear {
+      ContentView().onAppear {
         // Disable the tab bar options
         NSWindow.allowsAutomaticWindowTabbing = false
       }

+ 27 - 13
Todos/View/ProjectPanelView.swift

@@ -19,14 +19,14 @@ struct ProjectPanelView: View {
     let style = Date.FormatStyle(date: .numeric, time: .standard)
     HStack {
       TextField("", text: $item.name)
-          .font(.title)
-          .padding(.leading, 10)
+        .font(.title)
+        .padding(.leading, 10)
       Spacer()
       Button(action: addItem) {
         Image(systemName: "plus")
       }
-        .help("New Task")
-        .padding(.trailing, 10)
+      .help("New Task")
+      .padding(.trailing, 10)
     }
     HStack {
       if let grp = $allGroups.first(where: { $0.name.wrappedValue == item.category }) {
@@ -43,17 +43,24 @@ struct ProjectPanelView: View {
     List {
       ForEach($item.tasks) { task in
         TaskView(task: task)
-        .contextMenu {
-          Button(action: {
-            deleteItem(item: task.wrappedValue, fromProject: item)
-          }) {
-            Label("Delete", systemImage: "trash")
+          .contextMenu {
+            Button("Delete", systemImage: "trash", role: .destructive) {
+              deleteItem(item: task.wrappedValue, from: item)
+            }
           }
+
+        ForEach(task.subtasks) { subtask in
+          SubTaskView(task: subtask)
+            .contextMenu {
+              Button("Delete", systemImage: "trash", role: .destructive) {
+                deleteItem(item: subtask.wrappedValue, from: task.wrappedValue)
+              }
+            }
         }
       }
     }
   }
-  
+    
   private func addItem() {
     withAnimation {
       let newTask = Task(name: "New Task", parent: item)
@@ -61,10 +68,17 @@ struct ProjectPanelView: View {
       item.tasks.append(newTask)
     }
   }
-
-  private func deleteItem(item: Task, fromProject: Project) {
+  
+  private func deleteItem(item: Task, from: Project) {
+    withAnimation {
+      from.tasks.removeAll(where: { $0.id == item.id })
+      modelContext.delete(item)
+    }
+  }
+  
+  private func deleteItem(item: SubTask, from: Task) {
     withAnimation {
-      fromProject.tasks.removeAll(where: { $0.id == item.id })
+      from.subtasks.removeAll(where: { $0.id == item.id })
       modelContext.delete(item)
     }
   }

+ 0 - 14
Todos/View/TaskView.swift

@@ -76,20 +76,6 @@ struct TaskView: View {
           VisibilityTapper(hideToggle: $hideNotes)
         }.focused($isFocused)
       }
-      
-      VStack {
-        ForEach($task.subtasks) { subtask in
-          SubTaskView(task: subtask)
-            .padding(.leading, 5)
-            .contextMenu {
-              Button(action: {
-                deleteItem(item: subtask.wrappedValue, fromTask: task)
-              }) {
-                Label("Delete", systemImage: "trash")
-              }
-            }
-         }
-      }
     }
   }