‫Hello developers, and welcome back.
‫In this video we're going to learn about LiveData in Viewmodels.
‫But there is a very important note here in the Build.gradle JS file.
‫If we hover at data binding equals to true, we can notice that if you plan to use data binding in a
‫Kotlin project, you should apply the Kotlin capped plugin.
‫So we can't use KSP with data binding.
‫So I'm going to switch KSP to capped.
‫And again guys if we go to Developer.android.com slash build slash migrate to KSP and we scroll down
‫to the list of supported libraries and open it.
‫We can notice that these are the libraries that are supported with KSP.
‫And data binding is not one of them.
‫So there are limited libraries that are supported with KSP.
‫I advise you if you are planning to use many libraries in your project and not listed in this list,
‫use capped instead of KSP and always refer to this list in order to check if your library is supported
‫with KSP or not.
‫So I'm going to go back from KSP to capped plugin here.
‫I need to remove this id and add id Kotlin capped and to add the dependency for the capped we need to
‫go to Kotlin lang org slash doc slash capped and we scroll down to use this plugin.
‫So copy it and go to build.gradle dot css file at the level of the project.
‫And remove this KSP and add this capped Kotlin sync.
‫Now and we have an error unresolved reference KSP.
‫Yes, this is due to this line using the room version.
‫So I'm going to replace it with capped.
‫Try again.
‫And congratulations.
‫Capped is added to your project okay now if we scroll up there is no error here.
‫And if we go to the contact view model and try to add add Bindable and you can check if you are using
‫KSP Bindable annotation will not work because KSP is not supporting data binding later on.
‫If KSP supports data binding, I advise you to use KSP, but for now use capped with data binding.
‫I'll start with add bindable and I need to add two variables the input name and the input email val
‫input name equals to mutable live data of type string.
‫And with this question mark.
‫But what does this mean?
‫Bindable.
‫This annotation is often used in conjunction with the data binding library in Android.
‫It indicates that changes to the underlying data.
‫In this case, the input name property should trigger updates in the UI.
‫This is typically used in two way data binding scenarios, and since I'm going to use the edit text,
‫I'm going to use two way data binding.
‫The input name equals to mutable live data.
‫This line declares a read only property named input name, and initializes it with an instance of mutable
‫live data of type string.
‫Mutable live data is a class provided by the Android architecture components that can hold and emit
‫values, and in this case it holds a nullable string as indicated by the string question mark, so it
‫can contain null values.
‫With this set up, you can observe changes to the input name using live data observers, and since it's
‫a mutable live data and not live data, live data is read only mutable live data.
‫You can also update its value programmatically.
‫The use of string question mark suggest that the data can be either a non-null string or null, in the
‫same way I'm going to use with the input email.
‫So val input email equals to mutable live data.
‫It's of type string.
‫And here we go.
‫I'll annotate it with Bindable.
‫And it's very.
‫Also I'm going to check if this button is a delete or update button.
‫Also the text.
‫So val save or update button text equals to mutable live data.
‫It's of type string and can't be nullable.
‫Of course I'm going to add at Bindable annotation and I'm going.
‫To use another text here val clear all or delete button text equals to mutable live data type string
‫add Bindable annotation.
‫So I created four variables of type mutable live data, input name and input email or get it from the
‫edit text save or update button.
‫Text I'm changing the text of the button either to update or save if you are saving or updating, and
‫here clear all or delete button text in the same way, I'm going to clear all, delete all the records,
‫or delete a specific contact from the database.
‫Now let's initialize these variables that I need to use in the inside the init block.
‫So save or update button text dot value.
‫You use value with mutable live data in order to access and change the value of the string inside the
‫mutable live data.
‫So here I'm accessing this value and set it to save.
‫So whenever you are creating a new contact, you are going to get an option for saving and not updating.
‫And this is logic clear all or delete button dot value equals to clear all.
‫Okay this is how we add live data to ViewModel.