ProjectPanelView.swift 3.6 KB

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