TaskView.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 let grp = $allGroups.first(where: { $0.name.wrappedValue == task.category }) {
  21. ColorPicker("", selection: grp.color).disabled(true).scaledToFit()
  22. }
  23. Image(systemName: task.status.label)
  24. .frame(width: 20)
  25. .padding(.trailing, -10)
  26. Picker("", selection: $task.status) {
  27. ForEach(Status.allCases) { unit in
  28. Text(String(describing: unit))
  29. }
  30. }
  31. .fixedSize(horizontal: true, vertical: false)
  32. .onChange(of: task.status) {
  33. if task.status.isStrong {
  34. task.subtasks
  35. .filter({ !$0.status.isStrong })
  36. .forEach({ subtask in subtask.status = task.status })
  37. }
  38. }
  39. TextField("Task Name", text: $task.name)
  40. .focused($isFocused)
  41. Button(action: addItem) {
  42. Image(systemName: "plus")
  43. .help("Add a Subtask")
  44. }
  45. }
  46. if isFocused || !(hideTags || task.tags.isEmpty) {
  47. HStack {
  48. TagBarView(task: $task)
  49. .font(.footnote)
  50. .padding(.leading, 30)
  51. if isFocused {
  52. Picker("", selection: $task.category) {
  53. Text(empty.name).tag("")
  54. ForEach(allGroups) { group in
  55. Text(group.name)
  56. }
  57. }
  58. .fixedSize(horizontal: true, vertical: false)
  59. }
  60. VisibilityTapper(hideToggle: $hideTags)
  61. }.focused($isFocused)
  62. }
  63. if isFocused || !(hideNotes || task.notes.isEmpty) {
  64. HStack {
  65. TextField("Notes", text: $task.notes, axis: .vertical)
  66. .font(.footnote)
  67. .padding(.leading, 30)
  68. VisibilityTapper(hideToggle: $hideNotes)
  69. }.focused($isFocused)
  70. }
  71. }
  72. }
  73. private func addItem() {
  74. withAnimation {
  75. let newSubtask = SubTask(name: "Subtask", parent: task)
  76. modelContext.insert(newSubtask)
  77. task.subtasks.append(newSubtask)
  78. }
  79. }
  80. private func deleteItem(item: SubTask, fromTask: Task) {
  81. withAnimation {
  82. fromTask.subtasks.removeAll(where: { $0.id == item.id })
  83. modelContext.delete(item)
  84. }
  85. }
  86. }
  87. #Preview {
  88. @Previewable @State var task = Task(name: "New Task")
  89. TaskView(task: $task)
  90. .frame(minHeight: 100) // Preview does not resize window properly
  91. }