rebeloper / NavigationKit

🧭 SwiftUI navigation done right
174 stars 15 forks source link

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

Closed saumilshah200191 closed 3 years ago

saumilshah200191 commented 3 years ago

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:

https://user-images.githubusercontent.com/76034194/103065442-99c6db00-45dc-11eb-8b6b-9df56509f0f5.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!

rebeloper commented 3 years ago

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