Code Monkey home page Code Monkey logo

Comments (7)

rundfunk47 avatar rundfunk47 commented on May 27, 2024 2

Oops. I realized there is no way to initialize NavigationStack with a coordinator yet. I've created an issue:

#16

from stinsen.

rundfunk47 avatar rundfunk47 commented on May 27, 2024 2

Hi! There's a version 2.0.0 now that supports deep-links. Please try it out and see if you can use it in your project :)

from stinsen.

rundfunk47 avatar rundfunk47 commented on May 27, 2024 1

Yes, that is intended behavior, since it hasn't appeared earlier in the tree. So your case I would probably write:

final class ACoordinator: NavigationCoordinatable {
    var navigationStack = NavigationStack()

    enum Route: NavigationRoute {
        case c
        case b
    }

    func resolveRoute(route: Route) -> Transition {
        switch route {
        case .c:
            return .modal(NavigationViewCoordinator(CCoordinator()).eraseToAnyCoordinatable())
        case .b:
            return .modal(NavigationViewCoordinator(BCoordinator()).eraseToAnyCoordinatable())
        }
    }

    @ViewBuilder
    func start() -> some View {
        AView()
    }
}

struct AView: View {

    @EnvironmentObject var aRouter: NavigationRouter<ACoordinator.Route>
    
    var body: some View {
	    VStack {
    		Button {
    			aRouter.route(to: .b)
    		} label: {
	    		Text("TestB")
    		}
    		Button {
    			aRouter.route(to: .c)
    		} label: {
	    		Text("TestC")
    		}
    	}
    }
}

and also define C in BCoordinator.

If this creates a too large ACoordinator, or if you want the views to be A->B->C instead of A->C when the user is pressing back button, you might be able to do something like this:

final class ACoordinator: NavigationCoordinatable {
    var navigationStack = NavigationStack()

    enum Route: NavigationRoute {
        case b(startWithC: Bool)
    }

    func resolveRoute(route: Route) -> Transition {
        switch route {
        case .b(let startWithC):
            return .modal(NavigationViewCoordinator(BCoordinator(startWithC: startWithC)).eraseToAnyCoordinatable())
        }
    }

    @ViewBuilder
    func start() -> some View {
        AView()
    }
}

struct AView: View {

    @EnvironmentObject var aRouter: NavigationRouter<ACoordinator.Route>
    
    var body: some View {
	    VStack {
    		Button {
    			aRouter.route(to: .b(false))
    		} label: {
	    		Text("TestB")
    		}
    		Button {
    			aRouter.route(to: .b(true))
    		} label: {
	    		Text("TestC")
    		}
    	}
    }
}

and in BCoordinator you can maybe init the NavigationStack with CCoordinator

from stinsen.

PangMo5 avatar PangMo5 commented on May 27, 2024 1

Thank you so much! I'll try it 😆

from stinsen.

rundfunk47 avatar rundfunk47 commented on May 27, 2024

Hi!

If you mean you need to navigate to every screen in the app through deep links, yes, that would be correct. You can also create a "tree", so for instance if your deep link needs to open a specific tab and navigate from that, that should also be possible.

from stinsen.

PangMo5 avatar PangMo5 commented on May 27, 2024

Thank you for your answer.

In fact, I don't need a tree, I just wanted to make Deep-links that can from the first screen to any screen using the coordinators that I made before.

So I tried to put several different coordinators in the first view.

But I don't know if it's the nature of EnvironmentObject.
In View created by Start() of ACoodinator, only ACoodinator works and attempts to access other coordinators, such as BCoodinator, results in the following error:

SwiftUI:0: Fatal error: No ObservableObject of type NavigationRouter<Route> found. 
A View.environmentObject(_:) for NavigationRouter<Route> may be missing as an ancestor of this view.

Codes are roughly like this.

final class ACoordinator: NavigationCoordinatable {
    var navigationStack = NavigationStack()

    enum Route: NavigationRoute {
        case b
    }

    func resolveRoute(route: Route) -> Transition {
        switch route {
        case .b:
            return .modal(NavigationViewCoordinator(BCoordinator()).eraseToAnyCoordinatable())
        }
    }

    @ViewBuilder
    func start() -> some View {
        AView()
    }
}

struct AView: View {

    @EnvironmentObject var aRouter: NavigationRouter<ACoordinator.Route>
    @EnvironmentObject var bRouter: NavigationRouter<BCoordinator.Route>
    
    var body: some View {
	    VStack {
    		Button {
    		// Works well
    			aRouter.route(to: .b)
    		} label: {
	    		Text("TestA")
    		}
    		Button {
    		// Crash
    			bRouter.route(to: .c)
    		} label: {
	    		Text("TestB")
    		}
    	}
    }
}

If this is intended, what structure should I fill out to do what I want?

from stinsen.

PangMo5 avatar PangMo5 commented on May 27, 2024

Thank you for your kind answer.

from stinsen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.