// // TaskView.swift // Todos // // Created by Sam Jaffe on 2/28/26. // import SwiftUI import SwiftData struct TaskView: View { @Environment(\.modelContext) private var modelContext @AppStorage(UserDefaultsKeys.Category) var allGroups = CodableArray() @Binding var task: Task @State private var hideTags: Bool = false @State private var hideNotes: Bool = false @State private var empty = Category() @Binding var isMoveMode: Bool @FocusState private var isFocused: Bool var body: some View { VStack { HStack { if let grp = $allGroups.first(where: { $0.name.wrappedValue == task.category }) { ColorPicker("", selection: grp.color).disabled(true).scaledToFit() } Image(systemName: task.status.label) .frame(width: 20) .padding(.trailing, -10) Picker("", selection: $task.status) { ForEach(Status.allCases) { unit in Text(String(describing: unit)) } } .fixedSize(horizontal: true, vertical: false) .onChange(of: task.status) { if task.status.isStrong { task.subtasks .filter({ !$0.status.isStrong }) .forEach({ subtask in subtask.status = task.status }) } } TextField("Task Name", text: $task.name) .focused($isFocused) if isMoveMode { Button { deleteItem(item: task) } label: { Image(systemName: "trash") }.help("Delete Task '\(task.name)'") } else { Button(action: addItem) { Image(systemName: "plus") .help("Add a Subtask") } } } if isFocused || !(hideTags || task.tags.isEmpty) { HStack { TagBarView(task: $task) .font(.footnote) .padding(.leading, 30) VisibilityTapper(hideToggle: $hideTags) if isFocused { Picker("", selection: $task.category) { Text(empty.name).tag("") ForEach(allGroups) { group in Text(group.name) } } .fixedSize(horizontal: true, vertical: false) } }.focused($isFocused) } if isFocused || !(hideNotes || task.notes.isEmpty) { HStack { TextField("Notes", text: $task.notes, axis: .vertical) .font(.footnote) .padding(.leading, 30) VisibilityTapper(hideToggle: $hideNotes) }.focused($isFocused) } VStack { ForEach($task.subtasks, id: \.self) { subtask in HStack { SubTaskView(task: subtask) .padding(.leading, 5) if isMoveMode { Button { deleteItem(item: subtask.wrappedValue, fromTask: task) } label: { Image(systemName: "trash") }.help("Delete Subtask '\(subtask.name.wrappedValue)'") } } } } } } private func addItem() { withAnimation { let newSubtask = SubTask(name: "Subtask", parent: task) modelContext.insert(newSubtask) task.subtasks.append(newSubtask) } } private func deleteItem(item: Task) { withAnimation { if let fromProject = item.project { fromProject.tasks.removeAll(where: { $0.id == item.id }) modelContext.delete(item) } } } private func deleteItem(item: SubTask, fromTask: Task) { withAnimation { fromTask.subtasks.removeAll(where: { $0.id == item.id }) modelContext.delete(item) } } } #Preview { @Previewable @State var isMoveMode = false @Previewable @State var task = Task(name: "New Task") TaskView(task: $task, isMoveMode: $isMoveMode) .frame(minHeight: 100) // Preview does not resize window properly }