TaskView.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. Image(systemName: task.status.label)
  21. .frame(width: 20)
  22. .padding(.trailing, -10)
  23. Picker("", selection: $task.status) {
  24. ForEach(Status.allCases) { unit in
  25. Text(unit.description).tag(unit)
  26. }
  27. }
  28. .fixedSize(horizontal: true, vertical: false)
  29. .onChange(of: task.status) {
  30. if task.status.isStrong {
  31. task.subtasks
  32. .filter({ !$0.status.isStrong })
  33. .forEach({ subtask in subtask.status = task.status })
  34. }
  35. }
  36. TextField("Task Name", text: $task.name)
  37. .focused($isFocused)
  38. if let grp = $allGroups.first(where: { $0.name.wrappedValue == task.category }) {
  39. ColorPicker("", selection: grp.color).disabled(true).scaledToFit()
  40. }
  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. }
  81. #Preview {
  82. @Previewable @State var task = Task(name: "New Task")
  83. TaskView(task: $task)
  84. .frame(minHeight: 100) // Preview does not resize window properly
  85. }