ProjectPanelView.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. Picker("Default Category", selection: $item.category) {
  61. Text(empty.name).tag("")
  62. ForEach(allGroups) { group in
  63. Text(group.name)
  64. }
  65. }
  66. HStack {
  67. Label("", systemImage: "arrow.up.arrow.down")
  68. Toggle("Move Tasks", isOn: $move)
  69. }
  70. HStack {
  71. Label("", systemImage: "magnifyingglass")
  72. TextField("Filter Tasks", text: $taskFilter)
  73. }
  74. StatusChecklist(statuses: $statuses)
  75. }
  76. }
  77. Text("")
  78. }
  79. TextField("Project Notes", text: $item.notes, axis: .vertical)
  80. .padding(.leading, 20)
  81. List {
  82. ForEach(selected($item.tasks), id: \.id) { task in
  83. TaskView(task: task)
  84. .swipeActions {
  85. Button("Delete", systemImage: "trash", role: .destructive) {
  86. deleteItem(item: task.wrappedValue, from: item)
  87. }
  88. }
  89. ForEach(selected(task.subtasks), id: \.id) { subtask in
  90. SubTaskView(task: subtask)
  91. .swipeActions {
  92. Button("Delete", systemImage: "trash", role: .destructive) {
  93. deleteItem(item: subtask.wrappedValue, from: task.wrappedValue)
  94. }
  95. }
  96. }
  97. .onMove(perform: { moveItem(task.wrappedValue, $0, $1) })
  98. .moveDisabled(!move)
  99. }
  100. .onMove(perform: { moveItem(item, $0, $1) })
  101. .moveDisabled(!move)
  102. }
  103. }
  104. private func selected<T : Ordered & Filterable>(_ items: Binding<[T]>) -> [Binding<T>] {
  105. return items.sorted(by: T.less).filter({
  106. let value = $0.wrappedValue
  107. return value.name.isEmpty ||
  108. (statuses.test(value.status) &&
  109. (taskFilter.isEmpty || value.containsText(taskFilter)))
  110. })
  111. }
  112. private func addItem() {
  113. withAnimation {
  114. let newTask = Task(parent: item)
  115. modelContext.insert(newTask)
  116. item.tasks.append(newTask)
  117. }
  118. }
  119. private func moveItem(_ within: any Aggregate, _ fromOffsets: IndexSet, _ toOffset: Int) {
  120. withAnimation {
  121. within.move(fromOffsets: fromOffsets, toOffset: toOffset)
  122. }
  123. }
  124. private func deleteItem<T: Aggregate>(item: T.Element, from: T) where T.Element: PersistentModel {
  125. withAnimation {
  126. from.remove(item)
  127. modelContext.delete(item)
  128. }
  129. }
  130. }
  131. #Preview {
  132. @Previewable @State var item = Project()
  133. ProjectPanelView(item: item)
  134. }