‫Hello, my friends.
‫In this video, we will start creating the ViewModel.
‫The ViewModel is the main point of Mvvm application.
‫The primary responsibility of a ViewModel is to provide data to the view so that the view can put that
‫data on on the screen.
‫It also allows the user to interact with data and changes the data.
‫The other key responsibility of the ViewModel is to encapsulate the interaction logic for the view,
‫but it doesn't mean that all the logic of the application should go into the View model.
‫Let's jump into Android studio and start creating the ViewModel for our note taking application.
‫I will create a new package here.
‫I will name it as.
‫The view model.
‫And inside it, I will create a new Kotlin class.
‫I will name it as.
‫Note.
‫View Model.
‫Click okay and enter.
‫And here we go.
‫This is our class.
‫This acts.
‫As a view model class.
‫So the view model.
‫This view model will contain in its primary constructor two parameters.
‫The first one is an app which is an application, and the second parameter is the node repository.
‫So private Val node repository it's of type node repository.
‫I will go to new line here and new line here.
‫I need to import the application so you can see this import and I need to extend the.
‫Android Application.
‫ViewModel.
‫Android.
‫ViewModel.
‫Okay, so I am telling Android studio that this class will act as a view model for this application.
‫Okay.
‫Now inside this curly braces, I need to put the functions.
‫I have five functions in the repository.
‫I need to make use of them by calling them function, add note and took in parameter type note and equal
‫to.
‫I need to import the note.
‫Equal to a view model scoop dot launch.
‫This is the coroutine.
‫And as I told you in the next.
‫Uh, sections.
‫We're going to talk deeply about the Coroutines and insert node.
‫You can see that this will act as an Add node and we'll go to the view model scope and we'll insert
‫the node by calling the repository.
‫So if we go back to the repository here, we can see that the insert node function is called.
‫Now you can see that from the view model I'm calling the methods in the repository and from the repository
‫I'm calling the methods in the.
‫The a O you can see.
‫So again the view model add note will be calling the insert node in the repository and the insert node
‫will call the node dao here in the Dao.
‫Okay.
‫Another function.
‫It is the delete node in the same way it took into the parameter node.
‫But note object view model, scope, dot launch.
‫And inside this coroutine I will use the node repository dot dot delete node function.
‫Okay.
‫Again, I need to create the third function, which is the update function.
‫Update.
‫Note.
‫Note It's type note inside it.
‫I will start a viewmodels scope dot launch and inside it I will create note repository, dot, update,
‫note.
‫And here we go.
‫This is the note.
‫Please concentrate with me, guys.
‫Now, since we're not going to use suspend functions here with these two queries inside the Dao Also,
‫these are the two queries and you can see that we didn't use suspend function in the repository.
‫I'm not going to I'm not going to create a coroutine here with these two functions.
‫So I will create get all nodes equal to node repository, dot get all nodes.
‫You can see how it's simple now, but this is a coroutine.
‫I'm launching it in the background thread function search.
‫Note it took parameter.
‫A query is of type string and equal to.
‫No repository, dot search node and query.
‫I'm executing this query.
‫Okay, so this is our view model.
‫This is our view node to view model for our Mvvm architecture.
‫It took in parameter an application variable and a repository.
‫And this repository will be used in order to execute the exact methods that we we will use them with
‫the coroutines they because they are suspend functions.
‫And here we didn't use any of coroutines because they are normal normal functions and they will be linked
‫with these queries.
‫Okay, so this is our view model.
‫And one note, since we're going to use a custom view model that extends from Android view model that
‫passes the node repository and application, we need to interact with this view model through a view
‫model factory that we're going to use and.