Skip to content

Instantly share code, notes, and snippets.

@LachlanMcKee
Last active June 3, 2021 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LachlanMcKee/0f5dffeb2f5c98815f3e1b46ee405464 to your computer and use it in GitHub Desktop.
Save LachlanMcKee/0f5dffeb2f5c98815f3e1b46ee405464 to your computer and use it in GitHub Desktop.
Scalable Jetpack Compose Navigation
// An interface is created to allow a feature module to add their Composable to the NavGraph.
// Defined within a library module
interface ComposeNavigationFactory {
fun create(builder: NavGraphBuilder, navController: NavHostController)
}
// An implementation of the interface, as well as a Dagger 2 module installed via hilt.
// Defined within a feature module
internal class Feature1ComposeNavigationFactory @Inject constructor() : ComposeNavigationFactory {
override fun create(builder: NavGraphBuilder, navController: NavHostController) {
builder.composable(
route = "feature1",
content = {
Feature1(
navController = navController
)
}
)
}
}
// Defined within the 'feature 1' module
@Module
@InstallIn(SingletonComponent::class)
internal interface ComposeNavigationFactoryModule {
@Singleton
@Binds
@IntoSet
fun bindComposeNavigationFactory(factory: Feature1ComposeNavigationFactory): ComposeNavigationFactory
}
// An example of a set of factories being used to construct a NavHost.
// Potentially defined within the app module
@AndroidEntryPoint
class ExampleActivity: AppCompatActivity {
@Inject
lateinit var composeNavigationFactories: @JvmSuppressWildcards Set<ComposeNavigationFactory>
@Composable
fun JetpackNavigationHiltApp() {
val navController = rememberNavController()
NavHost(navController, startDestination = "feature1") {
composeNavigationFactories.forEach { factory ->
factory.create(this, navController)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment