Use Screen factories with Dependency Injection
mainIn multi-module projects, you can pass Screen factories through DI by erasing their types. This prevents module leakage while maintaining navigation capabilities.
Screen/eraseToAnyScreen(): Erases the type but preserves the container type (e.g.,AnyTabsScreenorAnyStackScreen), allowing for tab switching or stack changes.Screen/eraseToAnyModalScreen(): Erases the container type toUIViewController, ensuring the container type does not extend beyond the module boundary.
You can also share ScreenRoute objects (like ScreenWindowRoute) between modules to define complex navigation paths that can be executed by a central navigator.
// Erasing a factory for DI
struct Screens {
func homeScreen() -> AnyTabsScreen {
HomeScreen(services: services, screens: self).eraseToAnyScreen()
}
}
// Executing a shared route from DI
let screens: Screens
let navigator = ScreenNavigator()
navigator.navigate(to: screens.showRoomListRoute())