TaskView.swift 2.3 KB

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