| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- //
- // 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 {
- Image(systemName: task.status.label)
- .frame(width: 20)
- .padding(.trailing, -10)
- Picker("", selection: $task.status) {
- ForEach(Status.allCases) { unit in
- Text(unit.description).tag(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 let grp = $allGroups.first(where: { $0.name.wrappedValue == task.category }) {
- ColorPicker("", selection: grp.color).disabled(true).scaledToFit()
- }
- 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)
- if isFocused {
- Picker("", selection: $task.category) {
- Text(empty.name).tag("")
- ForEach(allGroups) { group in
- Text(group.name)
- }
- }
- .fixedSize(horizontal: true, vertical: false)
- }
- VisibilityTapper(hideToggle: $hideTags)
- }.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)
- }
- }
- }
- private func addItem() {
- withAnimation {
- let newSubtask = SubTask(name: "Subtask", parent: task)
- modelContext.insert(newSubtask)
- task.subtasks.append(newSubtask)
- }
- }
- }
- #Preview {
- @Previewable @State var task = Task(name: "New Task")
- TaskView(task: $task)
- .frame(minHeight: 100) // Preview does not resize window properly
- }
|