‫If we run our application, the app crashes.
‫If we go to the logcat, we have the errors here unable to start and cannot create an instance of the
‫contact view model, which is a runtime exception in Android.
‫Development.
‫Viewmodels are commonly used to manage and store UI related data in a lifecycle aware manner.
‫When working with Viewmodels, it's often necessary to provide constructor parameters to instantiate
‫them properly.
‫This is where a ViewModel factory comes into play.
‫Let's open the Contact View model class and here to understand better.
‫If your view model has a constructor with parameters, you cannot use the default constructor that the
‫ViewModel framework provides.
‫A view model factory allows you to pass the required parameters when creating an instance of the view
‫model.
‫So when you have any parameters inside the constructor of the view model, you should use a new class
‫called View Model Factory.
‫For that, I'll create a new Kotlin class.
‫I'll name it as View Model Factory.
‫Click enter if you need to perform custom initialization logic before creating a view model, a View
‫Model factory allows you to encapsulate this logic and separate it from the view model's responsibility.
‫But I'm creating this view model factory.
‫Just to solve this error cannot create an instance of the class view model, because my contact view
‫model contains in its primary constructor a repository parameter.
‫So I'm going to pass this repository parameter using the view model factory.
‫And in the traditional way that we used, with no parameters in the constructor of the prime of the
‫view model, it will not work, so this way will not work.
‫Back to the view model factory.
‫I'll pass the same parameter as the view model.
‫So it's a repository parameter.
‫You need to create this factory similar to the view model.
‫And I'll extend this view model factory from a view model provider dot factory.
‫And this interface allows us to override a function named as Oncreate.
‫So Alt+ insert override methods and choose the create.
‫Or you can simply type create and it will display for you.
‫Don't be confused by these writings.
‫These are similar to all view model providers and all view model factories and all applications.
‫It's just a check here.
‫If the model class is assignable from the contact view model class dot Java, then what I need to do,
‫I need to return contact a view model and pass the repository that it is passed as a parameter for the
‫primary constructor of this factory as t, otherwise throw illegal argument exception unknown view model.
‫Model class dot is assignable from this condition.
‫Checks if the specified model class is either the same class as contact view model, or a super class
‫or super interface of contact view model.
‫In other words, it checks if the requested view model is compatible with contact view model and the
‫return contact view model as the.
‫If the condition is true, this condition is true.
‫So the provided model class is a subclass or a type of contact view model.
‫Then it creates an instance of contact view model with the provided repository.
‫And you can see here I am providing the repository to the contact view model.
‫So the creation of the view model is done here in the create function of the view model factory.
‫So here is the creation of the view model.
‫So if the condition is true then create a contact view model with the repository and cast it to the
‫generic type T.
‫The as t is used for casting to the generic.
‫Generic type.
‫This assumes that T is subtype of the view model.
‫This code is common pattern used in a view model provider dot factory class to provide custom instantiation
‫logic for different view model classes.
‫If the requested view model is of type contact view model, it creates an instance of it where the provided
‫dependencies and in this case a repository.
‫If the condition is not met, it might throw an illegal argument exception or handle it in some other
‫way depending on the requirements of your application.
‫So view model provider dot factory allows you to create view models in your application with a custom
‫audience.
‫Now our application able to create view models with the repository parameter.
‫Let's move to the main activity.
‫And here I'm going to create a factory equals to view model factory class and pass the repository.
‫So the creation of the view models is done inside this factory.
‫And to get an instance from the factory to the view model and assign it to the contact view model,
‫it's very simple.
‫Just pass the factory parameter.
‫It's very simple.
‫You create an instance from factory, you pass the repository and then pass the factory as the second
‫parameter of the view model provider.
‫This is the way how you achieve and the creation of custom view models in Android.
‫I am teaching you the most complex and the advanced topics in Android applications.
‫Let's run our application and congratulations!
‫This is our application.
‫Enter your name.
‫I'll start with Jack.
‫The email is Jack at gmail.com.
‫Click save and you can see the new card view is inserted in the recycler view.
‫If I click on it, the update and delete texts are displayed in these buttons.
‫And this is what I was talking about.
‫The text of the buttons.
‫Also, I can update and delete the selected item.
‫For example Jack 15 update and you can see I am updating the record.
‫I'll insert another one.
‫John.
‫Click save and here is the second record.
‫Clear all.
‫I am clearing the database from all its records.
‫Congratulations guys!
‫We succeeded in building this amazing application that uses the room database with the mVVM architecture.