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

Support of defaultArgs in the Screen.getViewModel() method #251

Closed
nvkleban opened this issue Nov 9, 2023 · 2 comments
Closed

Support of defaultArgs in the Screen.getViewModel() method #251

nvkleban opened this issue Nov 9, 2023 · 2 comments
Labels
di: hilt "Hilt" dependency injection issue

Comments

@nvkleban
Copy link
Contributor

nvkleban commented Nov 9, 2023

Problem:
I want my ViewModels to be able to receive through Hilt:

  1. SavedStateHandle
  2. Dependencies like Usecases, Repositories etc.
  3. Screen arguments

Solution:
Default args for a savedStateHandle is one of the currently recommended ways to pass navigation arguments to the ViewModel.

I've looked into code and it seems like it would be pretty easy to add defaultArgs support into Voyager.

So it will look something like:

@Composable
public inline fun <reified T : ViewModel> Screen.getViewModel(
    viewModelProviderFactory: ViewModelProvider.Factory? = null,
    defaultArgs: Bundle? = null
): T {
   ...

   val defaultCreationExtras = if (defaultArgs != null) {
       val mutableCreationExtras = MutableCreationExtras(hasDefaultViewModelProviderFactory.defaultViewModelCreationExtras)
       val currentDefaultArgs = mutableCreationExtras[DEFAULT_ARGS_KEY]
       if (currentDefaultArgs != null) defaultArgs.putAll(currentDefaultArgs)
       mutableCreationExtras[DEFAULT_ARGS_KEY] = defaultArgs
       mutableCreationExtras
   } else {
       hasDefaultViewModelProviderFactory.defaultViewModelCreationExtras
   }
        
   val provider = ViewModelProvider(
       store = viewModelStore,
       factory = factory,
       defaultCreationExtras = defaultCreationExtras
   )

   ...
}
Screen(index: Int): AndroidScreen() {
    ...
    val viewModel: HiltDetailsViewModel = getViewModel(defaultArgs = bundleOf("index", index))
    ...
}
@HiltViewModel
class HiltDetailsViewModel @Inject constructor(
    private val savedStateHandle: SavedStateHandle,
    private val repository: SomeRepository,
    private val index: Int = requireNotNull(savedStateHandle["index"])
) : ViewModel() {
   ...
}

I can make PR if this solution looks ok.

And by the way looks like Dagger team finally made a solution for a @AssistedInject issue, I found it in the Dagger documentation but some APIs not released yet.

@DevSrSouza DevSrSouza added the di: hilt "Hilt" dependency injection issue label Apr 8, 2024
@akhbulatov
Copy link

It would be useful

@nvkleban
Copy link
Contributor Author

I'll close it as we have AssistedInject that currently works with Voyager

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
di: hilt "Hilt" dependency injection issue
Projects
None yet
Development

No branches or pull requests

3 participants