| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- //
- // 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<Category>()
- @Binding var task: Task
- @State private var hideTags: Bool = false
- @State private var hideNotes: Bool = false
- @State private var empty = Category()
- @FocusState private var isFocused: Bool
- var body: some View {
- VStack {
- HStack {
- if !task.category.isEmpty {
- 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)
- Button(action: addItem) {
- Image(systemName: "plus")
- .help("Add a Subtask")
- }
- }
-
- if isFocused || !(hideTags || task.tags.isEmpty) {
- HStack {
- TagBarView(task: $task)
- .font(.footnote)
- .focused($isFocused)
- .padding(.leading, 30)
- VisibilityTapper(hideToggle: $hideTags)
- .focused($isFocused)
- 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)
- .focused($isFocused)
- .padding(.leading, 30)
- VisibilityTapper(hideToggle: $hideNotes)
- .focused($isFocused)
- }
- }
-
- VStack {
- ForEach($task.subtasks) { subtask in
- SubTaskView(task: subtask)
- .padding(.leading, 5)
- .contextMenu {
- Button(action: {
- deleteItem(item: subtask.wrappedValue, fromTask: task)
- }) {
- Label("Delete", systemImage: "trash")
- }
- }
- }
- }
- }
- }
- private func addItem() {
- withAnimation {
- let newSubtask = SubTask(name: "Subtask", parent: task)
- modelContext.insert(newSubtask)
- task.subtasks.append(newSubtask)
- }
- }
- private func deleteItem(item: SubTask, fromTask: Task) {
- withAnimation {
- fromTask.subtasks.removeAll(where: { $0.id == item.id })
- modelContext.delete(item)
- }
- }
- }
- #Preview {
- @Previewable @State var task = Task(name: "New Task")
- TaskView(task: $task)
- .frame(minHeight: 100) // Preview does not resize window properly
- }
|