I need to implement the below scenario:
When user is in mainPage view and types the input and clicks it, it calls the viewModel.selectItem function and that will in turn call the API from the backend and change the apiResponse value and i have implemented that whenever apiResponse value changes it calls the ApiSymbolResults view and displays this view, the ApiSymbolResults view has many other symbols present in Peer list and when any of the peer is clicked it calls the viewModel.selectItem ( which that will in turn call the API from the backend and change the apiResponse value) and loads the ApiResponse screen once again. When ApiSymbolResults is loaded for the first( when user types the symbol and clicks on it) the page will have a back button named Preview and when clicked on Preview it goes back to the mainPage, in the exact same stage where it was before loading the ApiSymbolResults view. I need to implement this scenario where from ApiSymbolResults view if the user clicks on another symbol it should load that new symbol information but also store the previous information using NavigationLink and a back button value should be changed to the previous Symbol that was present before loading the new user symbol in the ApiSymbolResults view. And when that back button is clicked it should go to the previous page where it was before loading the current page. (Scenario 2)
Eg: User in the mainPage , types Alpha and clicks enter the backend API responds to this and sends the results, then loads the ApiSymbolResults view. Now the API would have returned the Peer details (For eg: Beta, Gamma, XYS, …) and if user clicks on Beta, it fetches the details from the API, loads the ApiSymbolResults view with those details and when it loads the back button should have value “Alpha” and when clicked on that should take us back to the Alpha page from where beta was loaded (that is previous page of beta page(current page) without calling the API to fetch the details. It should use NavigationLink and stack the previous pages and pop it when the back button is clicked. I was successful in implementing from moving back to mainPage from ApiSymbolResults view but was unable to implement the Scenario 2(from ApiSymbolResults view load another peer in ApiSymbolResults view but also preserve the state of the previous symbol) How to implement this case ? I’m using Xcode15 and MacOS:Sonoma 14

struct  ApiSymbolResults: View {
    let response: String
    @Environment(.presentationMode) var presentationMode
    @ObservedObject var viewModel: SearchViewModel

    var body: some View {
        GeometryReader { geometry in
            ScrollView {
                VStack(alignment: .leading) {
                    Spacer(minLength: 10)
                    if let responses = Global.shared.responseGot {
                        Text(responses.symbol.title)
                                      
                            }
                        }
                        .padding(.leading, 20)
                      
                  
                        ScrollView(.horizontal, showsIndicators: false) {
                            LazyHStack {
                                ForEach(responses.peer.removingDuplicates().filter { !$0.contains(".") }, id: .self) { peer in
                                    NavigationLink(destination:  ApiSymbolResults(response: peer, viewModel: viewModel)) {
                                        Text(peer)
                                            .foregroundColor(.blue)
                                            .onTapGesture {
                                                viewModel.selectItem(with: peer)
                                                print("Clicked on peer: (peer)")
                                            }
                                    }
                                }
                            }
                        }
                       
                        .padding(.leading, 20)


                        
                    }
                    Spacer()
                }
                .frame(width: geometry.size.width, alignment: .leading)
                .background(Color.white)
            }
            .navigationBarItems(leading: HStack {
                Image(systemName: "chevron.left")
                    .foregroundColor(.blue)
                Button("Preview") {
                    self.presentationMode.wrappedValue.dismiss()
                    viewModel.apiResponse = nil
                }
            }, trailing: Button(action: {
                print("Plus button tapped")
            }) {
                Image(systemName: "plus")
                    .foregroundColor(.white)
                    .padding(8)
                    .font(.system(size: 6, weight: .bold))
                    .background(Circle().fill(Color.blue))
            })
            .navigationBarBackButtonHidden(true)
        }
    }
}