I have 2 viewModels -
- MainViewModel**
- StorageViewModel
StorageViewModel.kt
class StorageViewModel @ViewModelInject constructor(private val preferenceStorage:
PreferenceStorage, @ApplicationContext context: Context) : ViewModel() {
........
//save last played song
fun saveLastPlayedSong(song: Songs){
viewModelScope.launch {
protoDataStoreManager.saveLastPlayedSong(song)
}
}
}
Now, I want to call the saveLastPlayedSong function in MainViewModel
MainViewModel.kt
class MainViewModel @ViewModelInject constructor(
private val musicServiceConnection: MusicServiceConnection,
private val storageViewModel: StorageViewModel
) : ViewModel(){
.........
fun playOrToggleSong(
mediaItem: Songs, toggle: Boolean = false
)
{
//here, I want to call the function from StorageViewModel e.g
storageViewModel.saveLastPlayedSong(mediaItem)
}
}
How do I instantiate the "StorageViewModel" inside MainViewModel and whats the best way (Good Practice).
I'm using MVVM and Hilt.