‫Welcome back.
‫We created the repository and the room database.
‫Now let's move to the view model.
‫The purpose of the view model is to acquire and keep the information that is necessary for an activity
‫or a fragment.
‫It acts as a link between the model and the view.
‫It's responsible for transforming the data from the model, and it keeps data streams to the view.
‫The activity or the fragment should be able to observe changes in the view model.
‫View models usually expose this information via live data or Android data binding.
‫Let's jump into Android Studio and start creating this view model.
‫Inside Android Studio.
‫I'll create a new package and I'll name it as View Model.
‫And inside this package I'll create a new class Kotlin class and I'll name it as Contact View Model.
‫Again, a view model is a component designed to store and manage UI related data.
‫View models are part of the Android Architecture component, and are used to separate the UI related
‫logic from the UI controller, which is an activity or a fragment.
‫And here inside this class, the contact view model, I need to create a primary constructor with a
‫single parameter repository.
‫So private val repository.
‫It's of type contact repository.
‫And the repository is a class responsible for handling data operations related to the user entities
‫such as fetching and updating the user data.
‫And we created before which is the contact repository insert, delete, update and delete all.
‫Then I need to tell Android Studio that this class will act as a view model, so I'll inherit from view
‫model Android x dot lifecycle dot view model class.
‫And here I'll add this constructor invocation.
‫The view model.
‫This is a base class provided by the Android architecture components.
‫View models are used to store and manage UI related data in a lifecycle conscious way.
‫Surviving configuration changes like screen rotations and avoiding memory leaks.
‫In the newer versions of Android Studio, there is no need to implement that dependency for this view
‫model, but I need to add the dependency here just in case.
‫We need to create other types of view models like view model providers and other classes, and to prevent
‫any possible errors.
‫So I advise you to go to Developer.android.com slash jetpack slash Android X slash releases slash lifecycle
‫and search for life cycle and the dependency for the view model.
‫So I'll copy this view model and go to the Gradle dot css file and paste it here.
‫Also I need to get the life cycle version, copy this and paste it here.
‫Also there I'm going to use the view model utility for compose.
‫No not the compose.
‫I'm going to use the live data.
‫So copy the live data and paste it here.
‫So we've added the view model dependency and the live data dependency sync now.
‫And here we go.
‫Now another thing I need to inherit from is the observable interface.
‫So add here observable.
‫This indicates that the class implements the observable interface in Android development.
‫The observable interface is often used in conjunction with the observer pattern.
‫It allows objects, the observers to subscribe and receive notifications about changes in the observed
‫object's state, so please refer to the previous sections in order to learn about the view models and
‫the observables.
‫Here I have an error because I imported the wrong observable class.
‫Make sure that the observable is implemented from the Android data binding package, and to do that,
‫we're going to enable the data binding in our Gradle scripts inside the Android scope.
‫I need to add build features, and I need to enable the data binding equals to true sync the project.
‫And now we're able to add the correct observable class.
‫So remove this observable and add observable.
‫And make sure that you are implementing the correct interface from the Android data binding class.
‫By removing this and this.
‫Again, importing the correct package and observable interface, make sure and.
‫Android X data binding.
‫Observable.
‫Android X lifecycle ViewModel.
‫Many people asked me about observables and they made this mistake.
‫So please pay attention to this correct implementation of the correct observable interface.
‫Okay I'll start with val users equals to repository which is the parameter here dot users or contacts
‫okay.
‫Then private var is update or delete.
‫This is a boolean variable and the private late initiate var contact to update or delete.
‫It's of type contact.
‫And here we go.
‫I'll change the users to contacts.
‫It's better in the next video we'll continue with the ViewModel.
‫And I want from you to focus with me because we're going to introduce the ViewModel with the data binding.