‫Welcome back.
‫Now let's continue with the view model.
‫And here I'll add the functions in order to allow the layout to interact with using the data binding.
‫With this view model here I'll start creating a new function named as insert.
‫And I'm going to take into the parameter a contact object equals to a view model.
‫Scope dot launch here why I'm using ViewModel scope.
‫Because I'm using coroutines.
‫I'll lecture everything, but just follow me repository dot insert method.
‫And I'm going to use the contact that I passed as a parameter.
‫This function called insert, is designed for inserting a contact entity into some data repository equals
‫to a view model.
‫Scoop dot launch.
‫This part of code uses the Kotlin coroutines to launch a new coroutine within the scope of the view
‫model.
‫The view model scope is a predefined coroutine scope tied to the lifecycle of the view model.
‫It automatically cancels all its coroutines when the view model is cleared or destroyed, helping to
‫avoid potential memory leaks.
‫Repository dot insert contact.
‫Inside the coroutine block.
‫It calls the insert function on repository object, passing the contact parameter.
‫The repository is an instance of a class responsible for handling data operations related to the database
‫and the room database.
‫We created this repository before, which is the contact repository.
‫And I'm calling this function.
‫And you can see this function turns into yellow because I use it into the ViewModel.
‫So I need to use the insert delete update and delete all functions in the repository.
‫And you can see I used ViewModel scoop dot launch.
‫In order to use it with the insert function and call the insert function from the repository.
‫Because insert function is suspend function and uses coroutines to do the insert job in the background
‫and without freezing the UI thread.
‫Putting all together the insert function is a concise and a coroutine based way to delegate the task
‫of inserting, and you contact object to a repository, taking advantage of the structured concurrency
‫provided by the ViewModel scoop.
‫This approach helps manage the background tasks related to data operations in a clean and lifecycle
‫aware manner.
‫Commonly used in Android app development with a ViewModel and LiveData architecture.
‫So I want from you to refer to the coroutines section in order to understand and deeply the coroutines
‫and the way I'm using here with the ViewModel.
‫Also, you can understand that since I'm creating a suspend function in the repository, I need to call
‫it from the ViewModel scooped launch.
‫I call this for executing the background tasks like repository dot insert function.
‫In the same way, I'm going to call the second function, which is the delete.
‫So here function delete contact object equals to a ViewModel scoop dot launch.
‫Since delete function is a suspend function, I need to call it inside the ViewModel scoop repository
‫dot delete and I'm going to pass this contact parameter.
‫Also, I need to reset the buttons, so this functionality for deleting the contact is done by calling
‫this delete function from the contact dot repository.
‫Here I'm resetting the buttons and the fields so input name dot value equals to null input email dot
‫value equals to null and is update or delete equals to false.
‫Set the save or update button text dot value to save because I'm going to save and clear all or delete
‫button text dot value equals.
‫To clear all these two variables will be clear when running the application and see them directly.
‫Don't worry, let's continue with the functions.
‫I'll add the update.
‫I call this update function here.
‫So function update.
‫In the same way I need to pass a contact object equals to a ViewModel scope dot launch and the repository
‫dot update and passing the contact, resetting the buttons and the fields I'll.
‫Copy this and paste it here in the update save and clear.
‫All this is the update function.
‫Now let's move to the last function which is delete all function.
‫Clear all equals to view model scope dot launch and the repository dot delete all.
‫So I need to use view model scoop dot launch in order to call all functions starting with the spend
‫keyword.
‫Because it's a coroutine.
‫Here I am using one variable inside the view model for detecting if it is a concise delete for a specific
‫contact or delete all.
‫Also, this is true for update or delete.
‫So either I'm going to update the specified contact or delete it with the same button.
‫So we need to detect it as an update or delete operation.
‫For that I'm going to scroll down to here and create a function named as Save or Update.
‫This will detect if it is a save or update functionality.
‫I'll start if is update or delete.
‫It is true.
‫This is a boolean variable.
‫Then go and make an update user or contact to update or delete.
‫Dot name equals to input name dot value.
‫And this double exclamation mark.
‫The value property of a mutable live data contains the current value wrapped in a nullable type, which
‫is a string in this case.
‫So if we go back up, we can see it's an input name and wrapped inside the string.
‫So I'm going to access its value and the input name dot value double exclamation mark.
‫In Kotlin.
‫None.
‫Null assertion operation is referred as double exclamation mark.
‫It asserts that the value is non null.
‫In this context, it's used to assert that the input name dot value is not null if it happens to be
‫null, a null pointer exception will be thrown.
‫So here I'm assuming and asserting that the input name dot value is not null.
‫In the same way, I'm going to assert that for the email.
‫So contact to update or delete dot email equals to input email dot value double exclamation mark.
‫And here I'm going to use the update functionality and update the user or the contact to update or delete.
‫Else I'm going to insert a new contact val name equals to input name dot value.
‫And also I'm asserting it to null val email equals to input email dot value.
‫And I'm calling the insert function passing the contact as the new contact.
‫And here the id will be zero.
‫It will be auto incremented by the room database and passing the name and the email to it.
‫Then I'm going to reset the name and the email input name.
‫And the email dot value equals to null.
‫And here we go.
‫So here I'm creating a function save or update because I'm working with one button.
‫If the user choose to update.
‫So it will go and execute update.
‫If the user choose to be an insert function, it will be calling the insert function here that we created
‫before.
‫This is the insert function.
‫I need to change the contact to var contact to update or delete.
‫So I need to change it.
‫So if we scroll down I need to use Alt+ enter change to var and here change to var okay.
‫Here we go.
‫Now I'm going to create a similar function here named as clear all or delete.
‫So function clear all or delete checking.
‫If it is update then or it's a delete then go and execute delete function.
‫If it is not then go and clear all.