TaskView.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // TaskView.swift
  3. // Todos
  4. //
  5. // Created by Sam Jaffe on 2/28/26.
  6. //
  7. import SwiftUI
  8. struct TaskView: View {
  9. @Binding var task: Task
  10. @State private var hideTags: Bool = false
  11. @State private var hideNotes: Bool = false
  12. @FocusState private var isFocused: Bool
  13. var body: some View {
  14. VStack {
  15. HStack {
  16. Image(systemName: task.status.label)
  17. .frame(width: 20)
  18. .padding(.trailing, -10)
  19. Picker("", selection: $task.status) {
  20. ForEach(Status.allCases) { unit in
  21. Text(String(describing: unit))
  22. }
  23. }
  24. .fixedSize(horizontal: true, vertical: false)
  25. .onChange(of: task.status) {
  26. if task.status.isStrong {
  27. task.subtasks
  28. .filter({ !$0.status.isStrong })
  29. .forEach({ subtask in subtask.status = task.status })
  30. }
  31. }
  32. TextField("Task Name", text: $task.name)
  33. .focused($isFocused)
  34. Button() {
  35. task.subtasks.append(SubTask(name: "Subtask"))
  36. } label: {
  37. Image(systemName: "plus")
  38. .help("Add a Subtask")
  39. }
  40. }
  41. if isFocused || !(hideTags || task.tags.isEmpty) {
  42. HStack {
  43. TagBarView(tags: $task.tags)
  44. .font(.footnote)
  45. .focused($isFocused)
  46. .padding(.leading, 30)
  47. VisibilityTapper(hideToggle: $hideTags)
  48. .focused($isFocused)
  49. }
  50. }
  51. if isFocused || !(hideNotes || task.notes.isEmpty) {
  52. HStack {
  53. TextField("Notes", text: $task.notes)
  54. .font(.footnote)
  55. .focused($isFocused)
  56. .padding(.leading, 30)
  57. VisibilityTapper(hideToggle: $hideNotes)
  58. .focused($isFocused)
  59. }
  60. }
  61. VStack {
  62. ForEach($task.subtasks) { subtask in
  63. SubTaskView(task: subtask)
  64. .padding(.leading, 5)
  65. }
  66. }
  67. }
  68. }
  69. }
  70. #Preview {
  71. @Previewable @State var task = Task(name: "New Task")
  72. TaskView(task: $task)
  73. .frame(minHeight: 100) // Preview does not resize window properly
  74. }