Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flexible nested navigation #42

Closed
mbarashkov opened this issue Dec 21, 2021 · 2 comments
Closed

Flexible nested navigation #42

mbarashkov opened this issue Dec 21, 2021 · 2 comments

Comments

@mbarashkov
Copy link

Imagine the following navigation pattern:

/screen1/:id1
/screen1/:id1/:childId1
/screen1/:id1/:childId1/subchildId1
...

And I want all these paths to be routed to a single screen (it's like going down a filesystem).
I've tried doing
screen1/* - this catches all these navigations but doesn't give me parameters
screen1/*/:id - doesn't work
:id - doesn't work for nested navigations either

Hence I'm taking a temporary solution of forking the library and making RouteInformation.matchedPath public to analyze it in code (which is not the best way to solve this, of course).

What do you think would be the proper way to support this (fairly common I guess) scenario?
Thanks!

@frzi
Copy link
Owner

frzi commented Dec 21, 2021

This is actually something matchedPath should help with in the future. However it's currently an internal property due to lack of testing. I have not yet tested whether it always returns what I want 😄

You can achieve similar behaviour without needing access to it the property:

struct FileExplorerRoot: View {
	@EnvironmentObject private var navigator: Navigator

	var body: some View {
		Route { route in
			let pathComponents = String(navigator.path[route.path.endIndex...]).split(separator: "/")
			FileExplorer(path: pathComponents)
		}
	}
}

Note this only works if FileExplorerRoot is somewhere inside another Route. If it's not and it's directly inside the Router instead, you can just use let pathComponents = navigator.path.split(separator: "/") instead 😄

Regardless, there's probably not a better way than using .split(separator: "/") on the path. It's your best bet at the moment.

The view should also always re-render when the path changes.

@mbarashkov
Copy link
Author

Thank you for the explanation!

@frzi frzi closed this as completed Dec 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants