ProjectPanelView.swift 3.3 KB

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