Skip to content

Instantly share code, notes, and snippets.

@anitaa1990
Last active June 26, 2024 13:16
// We are using @AssistedInject instead
// of normal @Inject as we want
// to assist one dependency by our own
// at runtime and rest by hilt
@HiltViewModel(assistedFactory = MainViewModel.Factory::class)
class MainViewModel @AssistedInject constructor(
// dependency which is to be assisted by
// us should be annotated with @Assisted
@Assisted
private val query: String
) : ViewModel() {
// We define the factory of this viewmodel here.,
// So we need to annotate this factory interface
// with @AssistedFactory in order to let Hilt
// know about it
@AssistedFactory
interface Factory {
fun create(query: String): MainViewModel
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment