ProjectPanelView.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // ProjectPanelView.swift
  3. // Todos
  4. //
  5. // Created by Sam Jaffe on 2/28/26.
  6. //
  7. import SwiftUI
  8. import SwiftData
  9. struct ProjectPanelView: View {
  10. @Environment(\.modelContext) private var modelContext
  11. @Bindable var item: Project
  12. @State private var showDialogue = false
  13. @State private var move = false
  14. @State private var taskFilter = ""
  15. @State private var statuses = StatusList()
  16. var body: some View {
  17. HStack {
  18. TextField("Project Name", text: $item.name)
  19. .font(.title)
  20. .padding(.leading, 10)
  21. CategoryColorDisplay(category: $item.category)
  22. Spacer()
  23. Button(action: addItem) {
  24. Image(systemName: "plus")
  25. }
  26. .help("New Task")
  27. .padding(.trailing, 10)
  28. if move {
  29. Label("", systemImage: "arrow.up.arrow.down")
  30. .foregroundStyle(.red)
  31. .font(.title2)
  32. .help("Re-ordering mode is enabled, text fields will be unresponsive")
  33. }
  34. if !taskFilter.isEmpty {
  35. Label("", systemImage: "text.magnifyingglass")
  36. .foregroundStyle(.blue)
  37. .font(.title2)
  38. .help("Only showing text matching '\(taskFilter)'")
  39. }
  40. if !statuses.all {
  41. Label("", systemImage: "exclamationmark.magnifyingglass")
  42. .foregroundStyle(.blue)
  43. .font(.title2)
  44. .help(statuses.description)
  45. }
  46. Button {
  47. showDialogue = !showDialogue
  48. } label: {
  49. Label("", systemImage: "ellipsis.circle")
  50. .foregroundStyle(.gray)
  51. .font(.title)
  52. }
  53. .buttonStyle(.borderless)
  54. .popover(isPresented: $showDialogue) {
  55. List {
  56. Label("Settings", systemImage: "gearshape")
  57. .font(.title)
  58. CategoryPicker(category: $item.category)
  59. Label("Filters", systemImage: "magnifyingglass")
  60. .font(.title)
  61. HStack {
  62. Label("", systemImage: "arrow.up.arrow.down")
  63. Toggle("Move Tasks", isOn: $move)
  64. }
  65. HStack {
  66. Label("", systemImage: "text.magnifyingglass")
  67. TextField("Filter Tasks", text: $taskFilter)
  68. }
  69. StatusChecklist(statuses: $statuses)
  70. }
  71. }
  72. Text("")
  73. }
  74. TextField("Project Notes", text: $item.notes, axis: .vertical)
  75. .padding(.leading, 20)
  76. List {
  77. ForEach(selected($item.tasks), id: \.id) { task in
  78. TaskView(task: task)
  79. .swipeActions {
  80. Button("Delete", systemImage: "trash", role: .destructive) {
  81. deleteItem(item: task.wrappedValue, from: item)
  82. }
  83. }
  84. ForEach(selected(task.subtasks), id: \.id) { subtask in
  85. SubTaskView(task: subtask)
  86. .swipeActions {
  87. Button("Delete", systemImage: "trash", role: .destructive) {
  88. deleteItem(item: subtask.wrappedValue, from: task.wrappedValue)
  89. }
  90. }
  91. }
  92. .onMove(perform: { moveItem(task.wrappedValue, $0, $1) })
  93. .moveDisabled(!move)
  94. }
  95. .onMove(perform: { moveItem(item, $0, $1) })
  96. .moveDisabled(!move)
  97. }
  98. }
  99. private func selected<T: Ordered & Filterable>(_ items: Binding<[T]>) -> [Binding<T>] {
  100. return items.sorted(by: T.less).filter({
  101. let value = $0.wrappedValue
  102. return value.name.isEmpty ||
  103. (statuses.test(value.status) &&
  104. (taskFilter.isEmpty || value.containsText(taskFilter)))
  105. })
  106. }
  107. private func addItem() {
  108. withAnimation {
  109. let newTask = Task(parent: item)
  110. modelContext.insert(newTask)
  111. item.tasks.append(newTask)
  112. }
  113. }
  114. private func moveItem(_ within: any Aggregate, _ fromOffsets: IndexSet, _ toOffset: Int) {
  115. withAnimation {
  116. within.move(fromOffsets: fromOffsets, toOffset: toOffset)
  117. }
  118. }
  119. private func deleteItem<T: Aggregate>(item: T.Element, from: T) where T.Element: PersistentModel {
  120. withAnimation {
  121. from.remove(item)
  122. modelContext.delete(item)
  123. }
  124. }
  125. }
  126. #Preview {
  127. @Previewable @State var item = Project()
  128. ProjectPanelView(item: item)
  129. }