ProjectPanelView.swift 4.3 KB

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