TaskView.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // TaskView.swift
  3. // Todos
  4. //
  5. // Created by Sam Jaffe on 2/28/26.
  6. //
  7. import SwiftUI
  8. import SwiftData
  9. struct TaskView: View {
  10. @Environment(\.modelContext) private var modelContext
  11. @AppStorage(UserDefaultsKeys.Category) var allGroups = CodableArray<Category>()
  12. @Binding var task: Task
  13. @State private var hideTags: Bool = false
  14. @State private var hideNotes: Bool = false
  15. @State private var empty = Category()
  16. @FocusState private var isFocused: Bool
  17. var body: some View {
  18. VStack {
  19. HStack {
  20. if !task.category.isEmpty {
  21. let grp = $allGroups.first(where: { $0.name.wrappedValue == task.category })
  22. ColorPicker("", selection: grp!.color).disabled(true).scaledToFit()
  23. }
  24. Image(systemName: task.status.label)
  25. .frame(width: 20)
  26. .padding(.trailing, -10)
  27. Picker("", selection: $task.status) {
  28. ForEach(Status.allCases) { unit in
  29. Text(String(describing: unit))
  30. }
  31. }
  32. .fixedSize(horizontal: true, vertical: false)
  33. .onChange(of: task.status) {
  34. if task.status.isStrong {
  35. task.subtasks
  36. .filter({ !$0.status.isStrong })
  37. .forEach({ subtask in subtask.status = task.status })
  38. }
  39. }
  40. TextField("Task Name", text: $task.name)
  41. .focused($isFocused)
  42. Button(action: addItem) {
  43. Image(systemName: "plus")
  44. .help("Add a Subtask")
  45. }
  46. }
  47. if isFocused || !(hideTags || task.tags.isEmpty) {
  48. HStack {
  49. TagBarView(task: $task)
  50. .font(.footnote)
  51. .focused($isFocused)
  52. .padding(.leading, 30)
  53. VisibilityTapper(hideToggle: $hideTags)
  54. .focused($isFocused)
  55. if isFocused {
  56. Picker("", selection: $task.category) {
  57. Text(empty.name).tag("")
  58. ForEach(allGroups) { group in
  59. Text(group.name)
  60. }
  61. }.focused($isFocused)
  62. }
  63. }
  64. }
  65. if isFocused || !(hideNotes || task.notes.isEmpty) {
  66. HStack {
  67. TextField("Notes", text: $task.notes, axis: .vertical)
  68. .font(.footnote)
  69. .focused($isFocused)
  70. .padding(.leading, 30)
  71. VisibilityTapper(hideToggle: $hideNotes)
  72. .focused($isFocused)
  73. }
  74. }
  75. VStack {
  76. ForEach($task.subtasks) { subtask in
  77. SubTaskView(task: subtask)
  78. .padding(.leading, 5)
  79. .contextMenu {
  80. Button(action: {
  81. deleteItem(item: subtask.wrappedValue, fromTask: task)
  82. }) {
  83. Label("Delete", systemImage: "trash")
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. private func addItem() {
  91. withAnimation {
  92. let newSubtask = SubTask(name: "Subtask", parent: task)
  93. modelContext.insert(newSubtask)
  94. task.subtasks.append(newSubtask)
  95. }
  96. }
  97. private func deleteItem(item: SubTask, fromTask: Task) {
  98. withAnimation {
  99. fromTask.subtasks.removeAll(where: { $0.id == item.id })
  100. modelContext.delete(item)
  101. }
  102. }
  103. }
  104. #Preview {
  105. @Previewable @State var task = Task(name: "New Task")
  106. TaskView(task: $task)
  107. .frame(minHeight: 100) // Preview does not resize window properly
  108. }