Skip to content

Unable to create a child navigation coordinator that shares the navigationstate from the parent #4

Description

@muralikrish91

To have a child coordinator that can manage the navigation state as well, this navigation path of the parent has to be shared with the child as well.
In this case theoretically the child should have its own screen type but can still manage the navigation using the same NavigationPath as the parent.

The NavigationModifier should be able to do this,

func body(content: Content) -> some View {
        NavigationStack(path: $state.path) { [weak coordinator] in
            content.navigationDestination(for: AnyHashable.self) {
                if let screen = $0 as? Coordinator.Screen {
                    coordinator?.destination(for: screen)
                } else if let child = coordinator?.childCoordinators {
                    child.destination(for: $0)
                }
            }
        }.coordinateSpace(name: CoordinateSpace.navController)
    }

The CoordinatorImpl should have a modified state

func navigationState<T: Coordinator>(for coordinator: T) -> NavigationState {
        let id = ObjectIdentifier(coordinator)
        let parentId = ObjectIdentifier(coordinator.parentCoordinator ?? coordinator)
        if let existing = navigationStateDict[id] {
            return existing
        } else if let existingParent = navigationStateDict[parentId] {
            return existingParent
        } else {
            let new = NavigationState()
            navigationStateDict[id] = new
            return new
        }
    }

This childCoordinator in practical terms can be more that one, if we decide to split each screen into a coordinator. So we will have an Array of childCoordinators which can have its own Screen and destination functions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions