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

Is there any way to define Router object in single instance? #238

Open
canbalkaya opened this issue Nov 14, 2022 · 1 comment
Open

Is there any way to define Router object in single instance? #238

canbalkaya opened this issue Nov 14, 2022 · 1 comment

Comments

@canbalkaya
Copy link

I always define a Router object when a view model's initializing. I want to see my view controllers at Xcode preview, that's why I need to define Router objects at preview state. Is it possible?

@pauljohanneskraft
Copy link
Collaborator

pauljohanneskraft commented Feb 28, 2023

@canbalkaya Interesting idea - what exactly should happen, if a route is triggered? You might for example want to define something like this:

#if targetEnvironment(simulator)

private var routerCollection = [ObjectIdentifier: Any]()

extension UnownedRouter {

    static func preview<RouteType>() -> Self where Value == StrongRouter<RouteType> {
        let identifier = ObjectIdentifier(self)
        if let existing = routerCollection[identifier] as? StrongRouter<RouteType> {
            return .init(existing) { $0 }
        }
        let coordinator = BasicViewCoordinator<RouteType>(rootViewController: .init()) { route in
            print("prepareTransition(for:)", route)
            return .none()
        }
        let router = coordinator.strongRouter
        routerCollection[identifier] = router
        return .init(router) { $0 }
    }

}

#endif

Of course, you could make your previews even more sophisticated by creating the coordinator in a better way, but maybe this idea helps for a start? What do you think about it?

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