TaskView.swift 2.3 KB

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