Hilt integration¶
Success
To use the getScreenModel
you should first import cafe.adriel.voyager:voyager-hilt
(see Setup).
@Inject¶
Add @Inject
annotation to your ScreenModel
.
class HomeScreenModel @Inject constructor() : ScreenModel {
// ...
}
Call getScreenModel()
to get a new instance.
class HomeScreen : AndroidScreen() {
@Composable
override fun Content() {
val screenModel = getScreenModel<HomeScreenModel>()
// ...
}
}
@AssistedInject¶
Add @AssistedInject
annotation to your ScreenModel
and provide a ScreenModelFactory
annotated with @AssistedFactory
.
class PostDetailsScreenModel @AssistedInject constructor(
@Assisted val postId: Long
) : ScreenModel {
@AssistedFactory
interface Factory : ScreenModelFactory {
fun create(postId: Long): PostDetailsScreenModel
}
}
Call getScreenModel()
and use your factory to create a new instance.
data class PostDetailsScreen(val postId: Long): AndroidScreen() {
@Composable
override fun Content() {
val screenModel = getScreenModel<PostDetailsScreenModel, PostDetailsScreenModel.Factory> { factory ->
factory.create(postId)
}
// ...
}
}
Sample¶
Info
Sample code here.
Navigator scoped ScreenModel¶
class HomeScreen : Screen {
@Composable
override fun Content() {
val navigator = LocalNavigator.currentOrThrow
val screenModel = navigator.getNavigatorScreenModel<HomeScreenModel>()
// ...
}
}