ProjectPanelView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. @AppStorage(UserDefaultsKeys.Category) var allGroups = CodableArray<Category>()
  12. @Bindable var item: Project
  13. @State private var empty = Category()
  14. @State private var showDialogue = false
  15. @State private var move = false
  16. @State private var taskFilter = ""
  17. @State private var statuses = StatusList()
  18. var body: some View {
  19. HStack {
  20. TextField("Project Name", text: $item.name)
  21. .font(.title)
  22. .padding(.leading, 10)
  23. if let grp = $allGroups.first(where: { $0.name.wrappedValue == item.category }) {
  24. ColorPicker("", selection: grp.color).disabled(true).scaledToFit()
  25. }
  26. Spacer()
  27. Button(action: addItem) {
  28. Image(systemName: "plus")
  29. }
  30. .help("New Task")
  31. .padding(.trailing, 10)
  32. if move {
  33. Label("", systemImage: "arrow.up.arrow.down")
  34. .foregroundStyle(.red)
  35. .font(.title2)
  36. .help("Re-ordering mode is enabled, text fields will be unresponsive")
  37. }
  38. if !taskFilter.isEmpty {
  39. Label("", systemImage: "text.magnifyingglass")
  40. .foregroundStyle(.blue)
  41. .font(.title2)
  42. .help("Only showing text matching '\(taskFilter)'")
  43. }
  44. if !statuses.all {
  45. Label("", systemImage: "exclamationmark.magnifyingglass")
  46. .foregroundStyle(.blue)
  47. .font(.title2)
  48. .help(statuses.description)
  49. }
  50. Button {
  51. showDialogue = !showDialogue
  52. } label: {
  53. Label("", systemImage: "ellipsis.circle")
  54. .foregroundStyle(.gray)
  55. .font(.title)
  56. }
  57. .buttonStyle(.borderless)
  58. .popover(isPresented: $showDialogue) {
  59. List{
  60. Label("Settings", systemImage: "gearshape")
  61. .font(.title)
  62. Picker("Category", selection: $item.category) {
  63. Text(empty.name).tag("")
  64. ForEach(allGroups) { group in
  65. Text(group.name)
  66. }
  67. }
  68. Label("Filters", systemImage: "magnifyingglass")
  69. .font(.title)
  70. HStack {
  71. Label("", systemImage: "arrow.up.arrow.down")
  72. Toggle("Move Tasks", isOn: $move)
  73. }
  74. HStack {
  75. Label("", systemImage: "text.magnifyingglass")
  76. TextField("Filter Tasks", text: $taskFilter)
  77. }
  78. StatusChecklist(statuses: $statuses)
  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(!move)
  103. }
  104. .onMove(perform: { moveItem(item, $0, $1) })
  105. .moveDisabled(!move)
  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. (statuses.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. }