TaskView.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. }
  62. .fixedSize(horizontal: true, vertical: false)
  63. .focused($isFocused)
  64. }
  65. }
  66. }
  67. if isFocused || !(hideNotes || task.notes.isEmpty) {
  68. HStack {
  69. TextField("Notes", text: $task.notes, axis: .vertical)
  70. .font(.footnote)
  71. .focused($isFocused)
  72. .padding(.leading, 30)
  73. VisibilityTapper(hideToggle: $hideNotes)
  74. .focused($isFocused)
  75. }
  76. }
  77. VStack {
  78. ForEach($task.subtasks) { subtask in
  79. SubTaskView(task: subtask)
  80. .padding(.leading, 5)
  81. .contextMenu {
  82. Button(action: {
  83. deleteItem(item: subtask.wrappedValue, fromTask: task)
  84. }) {
  85. Label("Delete", systemImage: "trash")
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. private func addItem() {
  93. withAnimation {
  94. let newSubtask = SubTask(name: "Subtask", parent: task)
  95. modelContext.insert(newSubtask)
  96. task.subtasks.append(newSubtask)
  97. }
  98. }
  99. private func deleteItem(item: SubTask, fromTask: Task) {
  100. withAnimation {
  101. fromTask.subtasks.removeAll(where: { $0.id == item.id })
  102. modelContext.delete(item)
  103. }
  104. }
  105. }
  106. #Preview {
  107. @Previewable @State var task = Task(name: "New Task")
  108. TaskView(task: $task)
  109. .frame(minHeight: 100) // Preview does not resize window properly
  110. }