The Repository Pattern
Instead of the ViewModel directly calling the services for getting and storing data, it should delegate that task to another component, such as a repository.
With the Repository pattern, you can move the code in the ViewModel that handles the data layer into a separate class. This reduces the complexity of the ViewModel, making it easier to maintain and test. The repository will manage where the data is fetched and stored, just as if the local database or the network service were used to get or store data:
In your ViewModel, you can add a property for the repository:
class MovieViewModel(val repository: MovieRepository): ViewModel() { ... }
The ViewModel will get the movies from the repository, or it can listen to them. It will not know where you actually got the list from.
You can create a repository interface that connects to a data source, such as in the following example...