| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- //
- // 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 showDialogue: Bool = false
- @State private var hideTags: Bool = false
- @State private var hideNotes: Bool = false
- @State private var empty = Category()
- let unset: Priority? = nil
- @FocusState private var isFocused: Bool
- var body: some View {
- VStack {
- HStack {
- if let priority = task.priority {
- Image(systemName: priority.label)
- .foregroundStyle(priority.style)
- .bold()
- .frame(width: 20)
- .padding(.trailing, -10)
- }
- StatusPicker(status: $task.status)
- .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")
- }
- Button {
- showDialogue = !showDialogue
- } label: {
- Label("", systemImage: "ellipsis.circle")
- .foregroundStyle(.gray)
- .font(.title2)
- }
- .buttonStyle(.borderless)
- .popover(isPresented: $showDialogue) {
- List{
- Picker("Task Category", selection: $task.category) {
- Text(empty.name).tag("")
- ForEach(allGroups) { group in
- Text(group.name)
- }
- }
- .fixedSize(horizontal: true, vertical: false)
- Picker("Priority", selection: $task.priority) {
- Text("").tag(unset)
- ForEach(Priority.allCases) { unit in
- Text(unit.id).tag(unit)
- }
- }
- }
- }
- Text("")
- }
- if isFocused || !(hideTags || task.tags.isEmpty) {
- HStack {
- TagBarView(task: $task)
- .font(.footnote)
- .padding(.leading, 30)
- 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(parent: task)
- modelContext.insert(newSubtask)
- task.subtasks.append(newSubtask)
- }
- }
- }
- #Preview {
- @Previewable @State var task = Task()
- TaskView(task: $task)
- .frame(minHeight: 100) // Preview does not resize window properly
- }
|