Save select value across the certain action workflow in SwiftUI

Viewed 22

I have this view (only relevant parts):

private var lists: [TaskList]
var body: some View {
        HStack(
            alignment: .top
        ) {
            ForEach(lists) { list in
                let tasks = list.tasks?.allObjects as! [Task]
                
                ScrollView {
                    LazyVStack(alignment: .leading) {
                        Text(list.title ?? "")
                        Button(
                            action: { showAddTask(currentList: list) },
                            label: { Image(systemName: "plus.square")}
                        )
                   }
                }
            }
        }
        .sheet(isPresented: $isNewTaskSheetVisible) {
            NewTaskSheetView(
                isVisible: self.$isNewTaskSheetVisible,
                callback: addTask)
        }
    }
    private func showAddTask(currentList: TaskList) {
        isNewTaskSheetVisible = true
        // save info about current list???
    }
    private func addTask(taskForm: TaskForm) { // task form is just task data (name, etc)
        // save in viewContext using current list
    }

The flow is like this:

  • user clicks on a button
  • I want to remember the currentList as that is one I need to add a new task to
  • I open the sheet with the flag
  • The sheet closes itself by setting the same flag to false
  • I have a callback for sheet closing that will perform the actual save

How can I pass the current list to the addTask method? What am I doing wrong?

I also want UI to refresh after the addTask - currently, this is not happening.

0 Answers
Related