Browse Source

feat: implement text filtering

Sam Jaffe 2 weeks ago
parent
commit
799ae24b15
2 changed files with 20 additions and 3 deletions
  1. 7 1
      Todos/View/ProjectPanelView.swift
  2. 13 2
      Todos/ViewModel/Filterable.swift

+ 7 - 1
Todos/View/ProjectPanelView.swift

@@ -17,6 +17,7 @@ struct ProjectPanelView: View {
 
   @State private var showDialogue = false
   @State private var move = false
+  @State private var taskFilter = ""
   @State private var statuses = StatusList()
 
   var body: some View {
@@ -44,6 +45,10 @@ struct ProjectPanelView: View {
             Label("", systemImage: "arrow.up.arrow.down")
             Toggle("Move Tasks", isOn: $move)
           }
+          HStack {
+            Label("", systemImage: "magnifyingglass")
+            TextField("Filter Tasks", text: $taskFilter)
+          }
           StatusChecklist(statuses: $statuses)
         }
       }
@@ -89,7 +94,8 @@ struct ProjectPanelView: View {
   
   private func selected<T : Ordered & Filterable>(_ items: Binding<[T]>) -> [Binding<T>] {
     return items.sorted(by: T.less).filter({
-      statuses.test($0.wrappedValue.status)
+      statuses.test($0.wrappedValue.status) &&
+        (taskFilter.isEmpty || $0.wrappedValue.containsText(taskFilter))
     })
   }
 

+ 13 - 2
Todos/ViewModel/Filterable.swift

@@ -9,7 +9,18 @@ import Foundation
 
 protocol Filterable {
   var status: Status { get }
+  func containsText(_ text: String) -> Bool
 }
 
-extension SubTask : Filterable {}
-extension Task : Filterable {}
+extension SubTask : Filterable {
+  func containsText(_ text: String) -> Bool {
+    return name.contains(text) || notes.contains(text)
+  }
+}
+
+extension Task : Filterable {
+  func containsText(_ text: String) -> Bool {
+    return name.contains(text) || notes.contains(text) ||
+      subtasks.contains(where: { $0.containsText(text) })
+  }
+}