ProjectPanelView.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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("", text: $item.name)
  21. .font(.title)
  22. .padding(.leading, 10)
  23. Spacer()
  24. Button(action: addItem) {
  25. Image(systemName: "plus")
  26. }
  27. .help("New Task")
  28. .padding(.trailing, 10)
  29. if move {
  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 !statuses.all {
  42. Label("", systemImage: "exclamationmark.magnifyingglass")
  43. .foregroundStyle(.blue)
  44. .font(.title2)
  45. .help(statuses.description)
  46. }
  47. Button {
  48. showDialogue = !showDialogue
  49. } label: {
  50. Label("", systemImage: "ellipsis.circle")
  51. .foregroundStyle(.gray)
  52. .font(.title)
  53. }
  54. .buttonStyle(.borderless)
  55. .popover(isPresented: $showDialogue) {
  56. List{
  57. HStack {
  58. Label("", systemImage: "arrow.up.arrow.down")
  59. Toggle("Move Tasks", isOn: $move)
  60. }
  61. HStack {
  62. Label("", systemImage: "magnifyingglass")
  63. TextField("Filter Tasks", text: $taskFilter)
  64. }
  65. StatusChecklist(statuses: $statuses)
  66. }
  67. }
  68. Text("")
  69. }
  70. HStack(alignment: .top) {
  71. TextField("Project Notes", text: $item.notes, axis: .vertical)
  72. .padding(.leading, 20)
  73. if let grp = $allGroups.first(where: { $0.name.wrappedValue == item.category }) {
  74. ColorPicker("", selection: grp.color).disabled(true).scaledToFit()
  75. }
  76. Picker("", selection: $item.category) {
  77. Text(empty.name).tag("")
  78. ForEach(allGroups) { group in
  79. Text(group.name)
  80. }
  81. }.help("Default category for new Tasks")
  82. }
  83. List {
  84. ForEach(selected($item.tasks), id: \.id) { task in
  85. TaskView(task: task)
  86. .swipeActions {
  87. Button("Delete", systemImage: "trash", role: .destructive) {
  88. deleteItem(item: task.wrappedValue, from: item)
  89. }
  90. }
  91. ForEach(selected(task.subtasks), id: \.id) { subtask in
  92. SubTaskView(task: subtask)
  93. .swipeActions {
  94. Button("Delete", systemImage: "trash", role: .destructive) {
  95. deleteItem(item: subtask.wrappedValue, from: task.wrappedValue)
  96. }
  97. }
  98. }
  99. .onMove(perform: { moveItem(task.wrappedValue, $0, $1) })
  100. .moveDisabled(!move)
  101. }
  102. .onMove(perform: { moveItem(item, $0, $1) })
  103. .moveDisabled(!move)
  104. }
  105. }
  106. private func selected<T : Ordered & Filterable>(_ items: Binding<[T]>) -> [Binding<T>] {
  107. return items.sorted(by: T.less).filter({
  108. let value = $0.wrappedValue
  109. return value.name.isEmpty ||
  110. (statuses.test(value.status) &&
  111. (taskFilter.isEmpty || value.containsText(taskFilter)))
  112. })
  113. }
  114. private func addItem() {
  115. withAnimation {
  116. let newTask = Task(parent: item)
  117. modelContext.insert(newTask)
  118. item.tasks.append(newTask)
  119. }
  120. }
  121. private func moveItem(_ within: any Aggregate, _ fromOffsets: IndexSet, _ toOffset: Int) {
  122. withAnimation {
  123. within.move(fromOffsets: fromOffsets, toOffset: toOffset)
  124. }
  125. }
  126. private func deleteItem<T: Aggregate>(item: T.Element, from: T) where T.Element: PersistentModel {
  127. withAnimation {
  128. from.remove(item)
  129. modelContext.delete(item)
  130. }
  131. }
  132. }
  133. #Preview {
  134. @Previewable @State var item = Project()
  135. ProjectPanelView(item: item)
  136. }