0

When Sheet is dismissed, screen returns to wrong View.

I'm currently facing some weird behavior that i can't seem to solve by myself. I have hierarchical structure where user can select one specific workout and see list of exercises. Then, he can click on exercise and see how many sets and reps he have to complete.

User can add new Workout with toolbar button + sheet in WorkoutListView (all works fine). User can also add new Exercise with toolbar button + sheet in ExerciseListView (one step deeper, but all works fine). Then, when user is inside the Exercise and he wants to add Sets & reps, with toolbar button + sheet, when this sheet is dismissed, user is returned not to SetsListView, but one step back - to ExerciseListView. And then when he enters the SetsListView the data is there.

WorkoutListView -> click on workout -> ExerciseListView -> click on exercise -> SetsListView.

struct WorkoutListView: View {
    @EnvironmentObject var viewModel: ViewModel
    @State var showAddWorkoutView = false

    var body: some View {
        NavigationView {
            List {
                ForEach(viewModel.workouts) { workout in
                    NavigationLink(destination: ExerciseListView(selectedWorkout: workout)) {
                        Text(workout.title)
                    }
                } // ForEach
            } // List
            .navigationTitle("Workouts")
            .toolbar {
                ToolbarItem {
                    Button {
                        showAddWorkoutView = true
                    } label: {
                        Image(systemName: "plus")
                    }
                }
            }
            .sheet(isPresented: $showAddWorkoutView) {
                AddWorkoutView()
            }
        } // NavigationView
    } // Body
}
struct ExerciseListView: View {
    @EnvironmentObject var viewModel: ViewModel
    var selectedWorkout: Workout

    @State var showAddExerciseView = false

    var body: some View {
        VStack {
            List {
                ForEach(selectedWorkout.exercises) { exercise in
                    NavigationLink(destination: SetsListView(selectedWorkout: selectedWorkout, selectedExercise: exercise)) {
                        Text(exercise.title)
                    }
                }
            }
        }
        .navigationTitle(selectedWorkout.title)
        .toolbar {
            ToolbarItem {
                Button {
                    showAddExerciseView = true
                } label: {
                    Image(systemName: "plus")
                }

            }
        }
        .sheet(isPresented: $showAddExerciseView) {
            AddExerciseView(selectedWorkout: selectedWorkout)
        }
    }
}
struct SetsListView: View {
    @EnvironmentObject var viewModel: ViewModel
    var selectedWorkout: Workout
    var selectedExercise: Exercise

    @State var showAddSetView = false

    var body: some View {
        List {
            ForEach(selectedExercise.sets) { currentSet in
                Text(currentSet.title)
            }
        }
        .navigationTitle(selectedExercise.title)
        .toolbar {
            ToolbarItem {
                Button {
                    showAddSetView = true
                } label: {
                    Text("Add")
                }

            }
        }
        .sheet(isPresented: $showAddSetView) {
            AddSetView(selectedWorkout: selectedWorkout, selectedExercise: selectedExercise)
        }
    }
}

Do someone know why this is happening and how to solve it?

0 Answers0