rebeloper / NavigationKit

🧭 SwiftUI navigation done right

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use of multiple .pushesAsRoot in same View navigates to random Views

saumilshah200191 opened this issue · comments

Hello @rebeloper,

I am exploring NavigationKit and there is one problem happening in my project. Let me explain you the problem first:

I have created one RootView as main View and added two Text with .pushesAsRoot property with which should navigate to two different Views but it is navigating wrong View if there are multiple .pushesAsRoot used in same View.

Here is the video of the problem:

Multiple.pushesAsRoot.mp4

Code snippets:

@main
struct NavigationDemoApp: App {
    var body: some Scene {
        WindowGroup {
            NavigationView{
                // main view
                RootView()
            }
            .rootable()
        }
    }
}

RootView with two .pushesAsRoot()

struct RootView: View {
    
    var body: some View {
        VStack{
            Text("Root Controller")
                .font(.title)
                .padding(.bottom, 100)
            
            VStack(alignment: .center, spacing: 50){
                // Push button 1
                Button {
                } label: {
                    Text("Push to Child 1")
                        .foregroundColor(.white)
                        .frame(width: 200, height: 50)
                        .pushesAsRoot(Child1())
                }
                .background(Color.blue)
                .clipShape(Capsule())
                .padding(.bottom)
                
                //Push button 2
                Button {
                } label: {
                    Text("Push to AfterLogin 1")
                        .foregroundColor(.white)
                        .frame(width: 200, height: 50)
                        .pushesAsRoot(AfterLogin1())
                }
                .background(Color.blue)
                .clipShape(Capsule())
            }
        }
    }
}

Child1 View

struct Child1: View {
    
    var body: some View {
        VStack{
            
            Text("Child 1")
                .font(.title)
                .padding(.bottom, 100)
            
            // Push button
            Button {
            } label: {
                Text("Push to Child 2")
                    .foregroundColor(.white)
                    .frame(width: 200, height: 50)
                    .pushes(Child2())
            }
            .background(Color.blue)
            .clipShape(Capsule())
            .padding(.bottom)
            
            // Pop to root button
            Button {
            } label: {
                Text("Pop to Root View")
                    .foregroundColor(.white)
                    .frame(width: 200, height: 50)
                    .dismissesToRoot()
            }
            .background(Color.red)
            .clipShape(Capsule())
        }
        .navigationBarHidden(true)
    }
}

AfterLogin1 View

struct AfterLogin1: View {
    
    var body: some View {
        VStack{
            
            Text("AfterLogin 1")
                .font(.title)
                .padding(.bottom, 100)
            
            // Push button
            Button {
            } label: {
                Text("Push to AfterLogin 2")
                    .foregroundColor(.white)
                    .frame(width: 200, height: 50)
                    .pushes(AfterLogin2())
            }
            .background(Color.blue)
            .clipShape(Capsule())
            .padding(.bottom)
            
            // Pop to root button
            Button {
            } label: {
                Text("Pop to Root View")
                    .foregroundColor(.white)
                    .frame(width: 200, height: 50)
                    .dismissesToRoot()
            }
            .background(Color.red)
            .clipShape(Capsule())
        }
        .navigationBarHidden(true)
    }
}

Let me know the solution or guide me what's the problem in the code? Thanks!

Thanks for reporting this. Have fixed it in 0.3.0
https://github.com/rebeloper/NavigationKit#%EF%B8%8F-pop