Skip to content

Isolate Navigation logic #124

@stephtelolahy

Description

@stephtelolahy

Actually The navigation if part of Core Module
This makes each feature aware of global navigation model. Which is respecting REDUX purity ie single source of truth.
The the inconvenient is that the NavigationStack has to synchronize its path and presented sheet with store state. This has to be done in two directions.

Idea

  • Create a specific Coordinator ObservableObject to manage syncronization

final class NavigationCoordinator: ObservableObject {
@published var path: [NavigationRoute] = []

private var cancellables: Set<AnyCancellable> = []
private let store: AppStore

init(store: AppStore) {
    self.store = store

    // Sync Redux state → path
    store.$state
        .map(\.navigationPath)
        .removeDuplicates()
        .receive(on: RunLoop.main)
        .sink { [weak self] newPath in
            guard let self = self else { return }
            if self.path != newPath {
                self.path = newPath
            }
        }
        .store(in: &cancellables)

    // Sync path → Redux (e.g. back gesture)
    $path
        .removeDuplicates()
        .receive(on: RunLoop.main)
        .sink { [weak self] newPath in
            guard let self = self else { return }
            if newPath != store.state.navigationPath {
                self.syncPathToStore(newPath)
            }
        }
        .store(in: &cancellables)
}

private func syncPathToStore(_ newPath: [NavigationRoute]) {
    let oldPath = store.state.navigationPath

    if newPath.count < oldPath.count {
        store.send(.pop) // User went back
    } else if newPath.count > oldPath.count {
        // (Optional) Allow programmatic pushing via NavigationLink?
        // Not recommended — all navigation should be explicit via store
    } else {
        // Same depth but different content
        store.send(.resetToHome)
    }
}

}

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