JamesSedlacek / Routing

SwiftUI library for abstracting navigation logic from views

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Router Stack is Always Running

zardasht-jazaa opened this issue · comments

Hello, I have an issue with Router Stack array , I just add an print for the RoutingView
public struct RoutingView<RootView: View, Routes: Routable>: View but I see in the terminal it print constantly , and at the same time I have another issue with navigating to root , for example when the User Is Logout I navigate them to root View , but when user is back to root View , then repeat the process the function of addToStack is called multiple time when an navigation happen.

this is called multiple time after back to root is called and stack get empty.

public func navigate(to destination: Destination) {
        stack.append(destination)
        debugLog("stackAPpend: \(stack.count)")
    }


Screen.Recording.2024-03-17.at.12.07.36.AM.mov
public struct RoutingView<RootView: View, Routes: Routable>: View {
    @AppStorage(AppStorageKeys.localIdentifier.rawValue) var localIdentifier = "en"
    @ObservedObject private var router: Router<Routes>
    private let rootView: () -> RootView
    @EnvironmentObject var loaderViewModel: LoaderViewModel
    
    public init(_ router: Router<Routes>, @ViewBuilder rootView: @escaping () -> RootView) {
        self.router = router
        self.rootView = rootView
        debugPrint("router.stack.count: \(router.stack.count)")
    }

    public var body: some View {
        NavigationStack(path: $router.stack) {
            rootView()
                .navigationDestination(for: Router<Routes>.Destination.self) { route in
                    route.body.environmentObject(router)
                }
                .environmentObject(router)
                .environmentObject(loaderViewModel)
                .id(localIdentifier)
                .buttonStyle(ScalledButtonStyle())
        }
    }
}

I find out what was the issue:

I had an listener on updating an collection from firebase

Auth.auth().addStateDidChangeListener { [weak self] (_, user) in
            guard let self = self, let user = user else { return }
            let userInterestDocRef = self.userInterestCollection.document(user.uid)
            userInterestDocRef.getDocument { (document, error) in
                if let document = document, document.exists {
                    completion()
                } else {
                    let userInterest = UsersInterestModel(userId: user.uid)
                    self.userId = user.uid
                    do {
                        try userInterestDocRef.setData(from: userInterest)
                        completion()
                    } catch {
                        print("Error Writing userData to FireStore: \(error.localizedDescription)")
                    }

and when ever sign in with google executed , the listener just refresh the collection and that cause to reRender this View , and again navigation to HomeView.

@ViewBuilder private var footerView: some View {
        The8Button(buttonText: "Continue", buttonStyleType: interestViewModel.isUserInterestMoreThanThree ? .vibrantRed : .filledDisabled) {
            interestViewModel.storeSelectedInterestToFireStore { result in
                switch result {
                case .success(()):
                    isPreferenceSelected = true
                    if isPreferenceSelected {
                        router.navigate(to: .homeView(theEightHeaderViewModel))
                    }
                case .failure(let error):
                    debugPrint("error is: \(error.localizedDescription)")
                }
            }
        }
        .padding(.horizontal)
        .disabled(!interestViewModel.isUserInterestMoreThanThree)
    }