TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Not all objects are updated

Forums > SwiftUI

As shown in the video, when I change the issue type, there are changes in my list, but they are not displayed in the issue list, the previous value of the type is retained there. However, if I add a new task, then there is an updated option in the add window, but immediately after adding, there are no new or changed types when editing. Please, help me.

https://youtu.be/YnUuz8sL6R8

struct Tasks: View {
    @EnvironmentObject var tasksManager: TasksManager
    @State var showAddTaskMenu = false
    @State var showTaskDetail = false
    var body: some View {
        NavigationView{
            ZStack{
                VStack(alignment: .leading){

                    List{
                        ForEach(0..<tasksManager.tasks.count, id: \.self){ i in
                            Button(action: {showTaskDetail.toggle()}, label: {
                                TaskRow(task: $tasksManager.tasks[i])
                            })
                            .sheet(isPresented: $showTaskDetail, content: {
                               EditTaskView(task: $tasksManager.tasks[i])
                                .environmentObject(TasksManager())
                            })

                        }.onDelete(perform: { indexSet in
                            tasksManager.tasks.remove(atOffsets: indexSet)
                        })
                    }.listStyle(PlainListStyle())
                }
                .padding(.top)

                // Mark: - Add new task button.
                VStack {
                    Spacer()
                    HStack{
                        Spacer()
                        Button(action: {showAddTaskMenu.toggle()}, label: {
                            Image(systemName: "plus")
                                .font(.system(.largeTitle))
                                .frame(width: 77, height: 70)
                                .foregroundColor(Color.white)
                                .padding(.bottom, 3)
                        })
                        .animation(.easeInOut)
                        .background(Color.accentColor)
                        .cornerRadius(38.5)
                        .padding()
                        .shadow(color: Color.black.opacity(0.4),
                                radius: 3,
                                x: 0,
                                y: 4)

                    }
                }
                .sheet(isPresented: $showAddTaskMenu, content: {
                    AddTaskView()

                })
            }
            .navigationBarHidden(true)
        }
    }
}

struct TaskRow: View {
    @Binding var task: TasksData
    static let taskDateFormat: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateStyle = .long
        formatter.timeStyle = .short
        return formatter
    }()
    var body: some View{
        VStack(alignment: .leading){
            HStack{
                Image(systemName: task.isDone ? "checkmark.circle" : "circle")
                .resizable()
                .frame(width: 20, height: 20, alignment: .center)
                    .onTapGesture {
                        task.isDone.toggle()
                    }
            Text(task.title)
                .font(.custom(appFont, size: 20, relativeTo: .body)).bold()
                .strikethrough(task.isDone)
                .lineLimit(2)
            }
            HStack{
                Image(systemName: "circle.fill")
                    .foregroundColor(task.type.labelColor)
                Text(task.type.label)
                    .font(.custom(appFont, size: 15, relativeTo: .body))
                Text("• \(task.dueDate, formatter: Self.taskDateFormat)")
                    .font(.custom(appFont, size: 15, relativeTo: .body))
                    .foregroundColor(Color(.systemGray2))
                Text("• \(task.completedIntervals)/\(task.plannedIntervals)")
                    .font(.custom(appFont, size: 15, relativeTo: .body))
                    .foregroundColor(.accentColor)

            }
            .padding(.vertical, 2)
        }
        .padding(.vertical)
    }
}

struct EditTaskView: View {
    @Environment(\.presentationMode) var presentationMode
    @EnvironmentObject var taskManager: TasksManager

    @State var dueDate: Date = Date()
    @State var allDayDate = false
    @State var taskWithoutDueDate = false

    @Binding var task: TasksData

    var body: some View{
        NavigationView{
            List{
                Section(header: Text("Task label").font(.custom(appFont, size: 13, relativeTo: .caption))){
                    TextField("Colonize Mars.", text: $task.title, onCommit: {
                        hideKeyboard()
                    })
                }

                Section(header: Text("Task notes").font(.custom(appFont, size: 13, relativeTo: .caption))){
                    TextField("Take a selfie for Elon", text: $task.notes, onCommit: {
                        hideKeyboard()
                    })
                }

                Section{
                    Picker("Activity Tag", selection: $task.type){
                        ForEach(taskManager.activityTypes, id: \.self){ type in
                            HStack{
                                Text(type.label)

                                //Spacer()
                                RoundedRectangle(cornerRadius: 50)
                                    .fill(type.labelColor)
                                    .frame(width: 20, height: 20)
                                    .padding(.horizontal)
                            }
                        }
                    }.padding(.vertical, 8)

                    HStack{
                        Text("Planned Intervals")
                        TextField("0", text: $task.plannedIntervals, onCommit: {
                            hideKeyboard()
                        })
                            .multilineTextAlignment(.trailing)
                            .keyboardType(.numberPad)

                    }.padding(.vertical, 8)
                }

                Section{
                    Toggle("Task without a due date", isOn: $taskWithoutDueDate.animation())

                    if !taskWithoutDueDate {
                        DatePicker("Due date", selection: $task.dueDate, displayedComponents: !allDayDate ? [.date, .hourAndMinute] : [.date] )
                            .padding(.vertical, 8)
                        Toggle("All day dask", isOn: $allDayDate)
                    }

                }

                Section{
                    Button(action: {
                        presentationMode.wrappedValue.dismiss()
                    }, label: {
                        HStack{
                            Spacer()
                            Text("Cancel").foregroundColor(.red).bold()
                            Spacer()
                        }
                    })
                }

            }
            .padding(.top)
            .background(Color(.systemGray6))
            .font(.custom(appFont, size: 18, relativeTo: .body))
            .listStyle(InsetGroupedListStyle())
            .navigationBarTitle(task.title)
            .navigationBarTitleDisplayMode(.inline)
            .navigationBarColor(backgroundColor: UIColor(.accentColor), tintColor: .white)
            .navigationBarItems(leading: Button(action: {

                presentationMode.wrappedValue.dismiss()
            }, label: {
                Image(systemName: "xmark").resizable().font(.custom(appFont, size: 18, relativeTo: .body))
            }) ,trailing: Button(action: {
                presentationMode.wrappedValue.dismiss()
            }, label: {
                Text("Save").font(.custom(appFont, size: 18, relativeTo: .body)).bold()
            }))

        }
    }
}

3      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.