Tgo1014
01/27/2023, 12:31 PMViewModelLifecycleScope {}
with hiltViewModel()
but it fails. Is there any way I can have different instances of the same ViewModel
? I've a pager with the same screens and I would need each of them to have independent instances of the same ViewModel
yschimke
01/27/2023, 1:18 PMTgo1014
01/27/2023, 1:34 PMActivity
and manually constructing instances of the `ViewModel`s passing the dependencies as parameters, it's super ugly 😕 I was following this Issue Tracker about scoping to the composable and thought this would solve the problem but apparently some Hilt work is need as well.Roudy Korkis Kanaan
01/27/2023, 1:40 PMTgo1014
01/27/2023, 1:42 PMTgo1014
01/27/2023, 1:43 PMTgo1014
01/27/2023, 1:43 PMViewModelLifecycleScope {
val viewModel = viewModel<MusicScreenViewModel>(
factory = MusicScreenViewModel.Factory(
getAllPlaylistsFlowUseCase,
getFontSizeFlowUseCase,
changeTextSizeUseCase,
getMusicByIdUseCase,
getMusicByIdFlowUseCase,
updateMusicToneUseCase,
addMusicToPlaylistUseCase,
changeScrollDurationUseCase
)
)
MusicScreen(MusicRouteArgs(playlist.musicList[page].id), adManager, viewModel)
}
Roudy Korkis Kanaan
01/27/2023, 1:45 PMAlbert Chang
01/27/2023, 4:46 PMhiltViewModel()
? Shouldn’t viewModel()
work in this case? IIRC the only difference between viewModel()
and hiltViewModel()
is that hiltViewModel()
scopes the view model to the back stack entry (and enables the view model to retrieve nav arguments), which isn’t needed in your case.yschimke
01/27/2023, 5:06 PMhiltViewModel
is your want dependencies injected without passing them around?
viewModel() does support back stack and nav arguments (through saved state handle) IIRC.Albert Chang
01/27/2023, 6:00 PMviewModel()
does support hilt view models as long as you annotated the activity or the fragment with @AndroidEntryPoint
. See https://developer.android.com/jetpack/compose/libraries#hilt.Albert Chang
01/27/2023, 6:03 PMIan Lake
01/27/2023, 6:55 PMviewModel
, it takes four parameters:
1. The viewModelStoreOwner
- this controls the scope of the ViewModel.
2. A key
which what determines the uniqueness of the ViewModel in its store (this defaults to the class name of the ViewModel, which is why when you ask for a ViewModel with the same class, you get the same instance back)
3. A factory
which controls how the ViewModel is instantiated. If you don't define one, the defaultViewModelFactory
from the viewModelStoreOwner
is used
4. The extras
which control what information, such as arguments, are passed to your factory
. If you don't define one, the defaultViewModelCreationExtras
from the viewModelStoreOwner
are used.
So if the viewModelStoreOwner
is your @AndroidEntryPoint
annotated activity or fragment, then that annotation is already generating the code that sets the correct defaultViewModelFactory
and defaultViewModelCreationExtras
for you.
All hiltViewModel
does is literally one line - setting the correct Hilt factory for you, even if the viewModelStoreOwner
you provide isn't an @AndroidEntryPoint
annotated activity or fragmentIan Lake
01/27/2023, 6:57 PMhiltViewModel
to allow overriding that extras
parameter so you can put your own per page argument in. That plus setting a page aware key
would certainly be enoughTgo1014
01/30/2023, 8:27 AMviewModel()
instead of hiltViewModel()
@Ian Lake the link you posted to Hilt source code actually has a key
param, which is not in the latest Hilt version. What version is that? Some unreleased version of Hilt?
Edit: it's the new alpha versions of hilt compose, I'll try passing a keyTgo1014
01/30/2023, 8:45 AMhiltViewModel()
in the latest alpha provides injected VMs without me having to pass the args manually. Thanks everyone for the discussion and the help here! 🎉
val viewModel = hiltViewModel<MusicScreenMD3ViewModel>(key = music.id.toString())