Movies Database
Modifying the view model to use real data
First, we need to provide a MovieRepository for the view model to use.
We do this via constructor injection, passing it as a parameter to the constructor. When we create an instance of the view model, we'll need to pass one in. Now that the view model knows about a repository, we can get data from that repository by delegation.
show in full file app/src/main/java/com/androidbyexample/movies/MovieViewModel.kt
// ...
class MovieViewModel(
private val savedStateHandle: SavedStateHandle,
//): ViewModel() {
// val movies: List<Movie> = listOf(
// Movie("The Transporter", "Jason Statham kicks a guy in the face"),
// Movie("Transporter 2", "Jason Statham kicks a bunch of guys in the face"),
// Movie("Hobbs and Shaw", "Cars, Explosions and Stuff"),
// Movie("Jumanji - Welcome to the Jungle", "The Rock smolders"),
// )
private val repository: MovieRepository,
): ViewModel(), MovieRepository by repository {
val backStackFlow = savedStateHandle.getMutableStateFlow<List<Screen>>(
key = "back_stack",
initialValue = listOf(MovieList)
)
fun pushScreen(screen: Screen) {
backStackFlow.value += screen
}
fun popScreen() {
backStackFlow.value = backStackFlow.value.dropLast(1)
}
}
Using MovieViewModel: MovieRepository by repository like this generates implementation functions
and properties for those defined in MovieRepository that call the functions in the passed-in
repository. This is similar to explicitly writing code like
class MovieViewModel(
private val repository: MovieRepository,
): ViewModel() {
val moviesFlow = repository.moviesFlow
suspend fun getMovieWithCast(id: String) =
repository.getMovieWithCast(id)
suspend fun insert(actor: ActorDto) {
repository.insert(actor)
}
...
}
If any of the functions need modification, you can override them.
Note
At this point the code will no longer compile, as we've broken the way movies are exposed to the UI.
All code changes
CHANGED: app/src/main/java/com/androidbyexample/movies/MovieViewModel.kt
package com.androidbyexample.movies
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
//import com.androidbyexample.movies.data.Movie
import com.androidbyexample.movies.repository.MovieRepository
import com.androidbyexample.movies.screens.MovieList
import com.androidbyexample.movies.screens.Screen
class MovieViewModel(
private val savedStateHandle: SavedStateHandle,
//): ViewModel() {
// val movies: List<Movie> = listOf(
// Movie("The Transporter", "Jason Statham kicks a guy in the face"),
// Movie("Transporter 2", "Jason Statham kicks a bunch of guys in the face"),
// Movie("Hobbs and Shaw", "Cars, Explosions and Stuff"),
// Movie("Jumanji - Welcome to the Jungle", "The Rock smolders"),
// )
private val repository: MovieRepository,
): ViewModel(), MovieRepository by repository {
val backStackFlow = savedStateHandle.getMutableStateFlow<List<Screen>>(
key = "back_stack",
initialValue = listOf(MovieList)
)
fun pushScreen(screen: Screen) {
backStackFlow.value += screen
}
fun popScreen() {
backStackFlow.value = backStackFlow.value.dropLast(1)
}
}