‫Welcome back.
‫We created the remote data source and the repository, so we finished the model layer.
‫Now let's move to the view model layer in the Mvvm Model View ViewModel Architecture Pattern.
‫The view model is a crucial component that acts as a bridge between the UI, which is the view and the
‫data, which is the model.
‫It's responsible for managing and exposing data to the UI while abstracting away the underlying business
‫logic and data source interactions.
‫Let's create a new new package and I'll name it as view model.
‫And inside this package I'll create a new class called Main Activity View Model.
‫View Model exposes data to the UI components using live data and other observable mechanisms.
‫Live data is a data holder class that can be observed by UI components When data changes in the view
‫model, live data notifies the UI allowing it to interact and update accordingly.
‫To tell Android studio that this class will act as a view model, I'll extend it from Android view model
‫Again.
‫Guys, I want from you to refer to the view model lessons in order to have more information and deep
‫information about view models.
‫But as a quick review, we have a view model in Android and Android view model.
‫What is the difference between these two view models?
‫Android view model and the View model are both classes provided by the Android architecture components
‫for use in the Mvvm architecture pattern, but they serve slightly different purposes and have different
‫use cases.
‫The basic view model, it's not Android specific and doesn't have a reference to the Android application
‫or context.
‫It designed to be used in conjunction with live data to expose data to UI components, example activity
‫and fragments in a lifecycle aware manner.
‫Typically ViewModel instances are created using the ViewModel provider and don't require access to Android
‫specific components.
‫We use ViewModel for holding and managing data related to the UI, such as configuration changes and
‫data that should survive across screen rotations.
‫It's suitable for non Android specific logic like data transformation and presentation logic.
‫But since I'm using Viewmodels in Android applications that we need to access Android specific components,
‫I'm going to use Android view model here.
‫Android View model is a subclass of a view model that is aware of Android application context.
‫It's intended for cases where the view model needs to access Android specific components such as resources
‫preferences or the application context and why I'm using it here, I'm going to pass an instance of
‫application to the repository.
‫So if we go back to the repository and here the repository weights from us to pass an application instance.
‫So in order to pass an application instance, I'm going to use an Android view model.
‫So Android view model is suitable when you need to perform Android specific tasks with a view model
‫like accessing resources, working with preferences, performing database operations that require a
‫context object.
‫I'll create an instance from the movie repository.
‫I'll name it as repository, and here I'll create a constructor.
‫Having this repository, I'll remove this movie repository from the constructor parameters.
‫And here I need to pass new movie repository.
‫I am accessing the application so that you can see in the constructor of the main activity view model
‫class.
‫There is an application inserted here and I'm going to use it to pass and create a movie repository
‫instance.
‫This is the purpose of using Android view model, this application instance.
‫Then I'm going to implement the live data.
‫I'll create a method, get all movies and the returning type of this method would be a list of movies
‫included inside live data.
‫Live data of list of movie Alt+ Enter to import the list class and return Here I'm going to call the
‫repository instance.
‫So Repository dot Get mutable live data and the mutable live data of this repository is a list of movie
‫you can see.
‫I'm calling this method in the view model.