‫In this video, we are going to implement the Mvvm pattern.
‫Hello my friends.
‫Again, we are going to implement the Mvvm by implementation by what this means.
‫So first of all, let us remove the app presenter and everything related to the presenter because we
‫are updating the previously created app with MVP pattern.
‫We have seen the MVC and then we updated the app to configure and cover the MVP pattern and now we are
‫going to implement the Mvvm to this application.
‫So our application is very simple.
‫It's just a button.
‫The user click, it displays the app name from the database.
‫Okay, so you can think about database as a class, as a database, SQL SQLite, you can think about
‫it as networking, cloud storage and whatever you want.
‫So I'm representing it by the simple way.
‫I'm not making it a very complex to understand it.
‫Now search for Google, Search on Google for ViewModel, you will get life cycle and this is the Android
‫developer site and these are the implementation you see there is an optional and annotation processors
‫and some for tests, some for for dependencies injection.
‫But till now we need to get these two and later on I will add a new dependency.
‫And I will see.
‫I will show you the differences and the errors that may counter you while developing.
‫And it's very simple to solve it.
‫So first of all, we need the life cycle view model and the live data.
‫Okay.
‫I will click sync now and everything should work fine.
‫So we have implemented the Library of View model and the live data.
‫Some people asked me why we need to use live data.
‫We were going to lecture it and see the differences between live data and mutable live data.
‫So don't worry, we are going step by step.
‫So I like making comments, as I told you, in order to review the code and see see every step by step.
‫Now let's create the view model class.
‫So first of all, we have implemented the dependency as a first step.
‫Second step, let's create an app view model, a view model class that extends the view model.
‫So this is the link, this is a view model link.
‫So this class plays the role of view model.
‫Okay, so if we go back to the course, you will see the view model link between the view and the model.
‫So this class will play the role of connecting the database with the view.
‫So first the third step is connecting the view model with database.
‫Every time, every thing that we have seen in the previous patterns, we need to make connections.
‫We need to connect the presenter with the model and then connect it with the database.
‫Sorry to the main activity.
‫And in the same way we are going to connect the the view model with database.
‫But remember, guys, we are working as an example of the vending machine.
‫The person who stands on the vending machine, insert the coin, request the water bottle, and then
‫the water bottle goes down.
‫But the the vending machine is not responsible for delivering it to you or to you or and they didn't
‫care about if you took the water bottle or not.
‫And if you took it or your brother or your friend or any person on the will take Will took it.
‫So.
‫It is not responsible for delivering.
‫It's just viewing and it's just getting the data.
‫But you are responsible and the same responsible for getting the water bottle and you are the listening.
‫So in this way the main activity will listen for the live data and request the request data from view
‫model.
‫We the first step is creating live data.
‫So the third step is connecting view model with database and request creating new object inserting data.
‫The fourth step is live data and creating the live data.
‫So what is live data?
‫Live data.
‫We have two types the live data and the immutable live data.
‫We see here that the there is in the methods that are available for live data we cannot post or set.
‫So.
‫The post and set value are the things that that characterize the mutable live data.
‫So the mutable live data allows you to set and change and post and get a post and set the live data
‫so you are able to to change the live data.
‫For our example, we need to use the mutable live data because I'm going to set and and post the data.
‫Own the life.
‫The mutable live data.
‫Okay, so live data.
‫Think about it as a the vending machine that is displaying what?
‫What it is getting so and displaying what it released.
‫Okay.
‫So it is keeping updating.
‫It is from the from its name.
‫It is live data.
‫It's getting the latest data available from the from the database and the from the database and data
‫source.
‫The fifth step is connecting ViewModel with the main activity.
‫It's just showing you how to connect it.
‫But it is not connection by means of connection.
‫It's just displaying the displaying the live data.
‫It's just a vending machine that released a bottle of water and it's telling every person that it is
‫available to to, to get the the bottle that it is released.
‫So just telling us that the bottle is released, you are going to get the bottle from this vending machine.
‫So.
‫We that the Livedata and the ViewModel.
‫I need you to understand this idea.
‫The Livedata, which is inside the ViewModel and the ViewModel is not responsible for delivering you
‫the data.
‫It is just displaying and just getting the data and stored it inside the live data and the mutable live
‫data.
‫The main activity you in the example is responsible for getting the responsible for getting the released
‫water bottle.
‫And in this activity and this example, the main activity is responsible to get the the data from the
‫live data through the ViewModel.
‫So the main activity is its responsibility is listening to the live data, not the live, not the ViewModel
‫responsible for delivering the data.
‫It is just making the, the making the connection and displaying the rules.
‫I need to add the lifecycle extensions.
‫Okay.
‫Because here we have seen that the ViewModel providers needs the dependency.
‫So it's very important to to see.
‫The and solve the errors that may encounter you and another error that ViewModel.
‫Providers is deprecated.
‫So what is the solution?
‫The solution is very simple.
‫I will show you in next couple of minutes.
‫Now.
‫I'm telling.
‫The ViewModel, provided it is from its name.
‫It is displaying it, providing the ViewModel of this activity and the ViewModel of.
‫And we are going to to to say and to define where is our ViewModel.
‫And so we get view app ViewModel and we are telling that app ViewModel plays the role as a ViewModel.
‫So instead of using deprecated things or methods, we are going to solve this error by removing the
‫as the ViewModel provider and put this the same way and insert a new.
‫So we are creating a new ViewModel provider and passing this.
‫The owner of it is this activity and then we are telling that our ViewModel, our class that plays the
‫role of ViewModel is called App ViewModel.
‫Guys, it's very simple.
‫Just I hope you understand it.
‫And the third, the last step is listening and observing the changes to the live data.
‫As I told you in the example, the vending machine just releasing the water bottle, but it is not its
‫responsibility to deliver it to you.
‫So in the same way that the ViewModel gets the data, but it is not responsible for delivering it to
‫the main activity.
‫So the main activity responsibility is to get and listen to the changes and observing the changes to
‫the live data.
‫So app ViewModel dot mutable live data.
‫So we are getting we are accessing the mutable live data and we are observing any change inside this
‫mutable live data.
‫So any change occurs in the live data.
‫Go and do this.
‫What we are going we are going to do is updating the text view to any changes.
‫So text view, dot set changes.
‫We are decoupling.
‫We are decoupling the main activity.
‫And we are the main activity from the connections from the UI logic.
‫So the main activity now, it's not responsible for the for connection with the database and it is not
‫responsible for making the connections and ensure where to get the data from.
‫It's responsibility is just listening for any changes that occurs in the live data that is presented
‫in the View model.
‫Now let's run our application and see our magic.
‫And in this way we have correctly implemented the mvvm pattern.
‫And congratulations, guys.
‫It's a very amazing pattern and I hope you understand it correctly.
‫We are going to learn about view binding.
‫Hello my friends again.
‫View binding is a feature that allows you to more easily write code that interacts with views.
‫Once a view binding is enabled in the module, it generates a binding class for each XML layout file
‫presented in that module.
‫An instance of binding class contains a direct references to all views that have an ID in the corresponding
‫layout.
‫In most cases, view binding replaces find view by ID.
‫What does this mean?
‫In order to create.
‫We see in the in the app, in order to create any widget like button or text, we need to instantiate
‫it and we need to write oncreate like find view by ID and then add the ID of the widget.
‫So we go to the layout here and we see that there is an ID of it, an ID of the text, view ID for the
‫button and the ID for the text, and the linkage between the Java Java file and the XML file is done
‫by using the file view by ID.
‫And this is the linkage.
‫The view.
‫But the why we are going to use the view binding and what is the usage of it?
‫Think about making an app that contains about around 20 or 30 widgets inside a single layout, and you
‫have many layouts.
‫So you are around making 100 or 200 views with widgets.
‫Findviewbyid, Findviewbyid.
‫And the very important thing that you didn't.
‫Use the view binding in fragments also.
‫So.
‫What are the differences from a view by ID?
‫So one person told me that, okay, I write 100 times.
‫You find view by ID.
‫What is the problem?
‫The problem is it the view binding is Null safety.
‫Since view binding creates direct references to views, there is no risk of a null pointer exception
‫due to an invalid invalid view ID and also type safety.
‫The fields in each binding class have type A types matching the views they reference in XML file.
‫This means there is no risk of class cast exception.
‫So let's go and implement and transform this code to view binding.
‫We have many, many things and many libraries to make view binding, but the most popular is butterknife.
‫But there is a small bad news about it because this tool is now deprecated.
‫So if you go to GitHub slash Jack Wharton and Butter Knife, you will get that this tool is now deprecated.
‫Please switch to view binding.
‫So let's go to view binding.
‫They are telling us that about the view binding and the view binding is a tool and feature and a part
‫of Android jetpack.
‫So let's go and implement the view binding view binding.
‫I will copy this.
‫The set up instructions here telling us that you need to to enable the view binding build option in
‫the model view.
‫So back to Android studio we go to the to here the the module and we paste it underneath the compile
‫option.
‫So I will increase the size here and paste it.
‫And I will click on sync now.
‫Now our application is ready to use the view binding.
‫So we go to the layout and ensure that every every widget has an ID.
‫So button here it has an ID of button and text view here has an id text of view.
‫In order to use correctly the view binding, you need to ensure that every widget, every single widget
‫has an ID.
‫And it's very important guys, because many people make problems and errors with this and we go up and
‫we we click on alt+ Enter.
‫We see here.
‫Alt+ Enter override resources.
‫Sorry.
‫No.
‫So maybe it will not show you the the option of converting it.
‫Alt+ enter.
‫So it's very simple, guys.
‫No need.
‫You have this constraint layout we need to insert here the tag of of layout the tag of it.
‫So.
‫Not here.
‫I will insert it here.
‫Okay.
‫So I need to surround our the our layout with a layout tag, and then I will.
‫I will.
‫And the layout.
‫Okay, So our layout, the previous layout is it is here and it is surrounded by the layout and it is
‫called like this.
‫Okay.
‫These two lines ensure that Android studio will end the Android application and view binding will understand
‫that it is a layout and ready to be view binded.
‫Okay.
‫In this way we correctly implement the view binding.
‫Now let's go to our main activity.
‫This is the documentation and we need to make binding classes.
‫So go to main activity.
‫I will remove this and I will remove every widget and oncreate also.
‫I will.
‫They will comment it like this in order to see the on click.
‫And here I need to make a class for binding.
‫So back to the main activity and the documentation of your binding.
‫We see that the private result profile binding.
‫This is the example and we see that the name of the layout is result profile.
‫So result, profile, binding.
‫I will name it as this.
‫So the main activity main activity binding we need to add binding.
‫I have added like this data binding.
‫Okay.
‫Main activity binding.
‫Or activity name.
‫I can name it as you want.
‫Activity main binding.
‫And I will name it as binding.
‫I will remove this set content view and equal to.
‫Activity.
‫Main binding dot inflate and I'm going to inflate the get layout inflator.
‫So the layout inflator will view view equal to binding dot get.
‫Ruth.
‫Okay.
‫Or you can or you can make it as set content view to view.
‫Okay, so how to solve the get root error?
‫We go to the dependencies.
‫The gradle build gradle and I need to make the data binding because I need to show you the capabilities
‫of the data binding and the view binding.
‫So.
‫I will sink now.
‫And my target of this application is to to show you the power of using bind, bind, the view binding
‫and data binding now.
‫Everything is working fine now in order to call any widget inside the layout, I just type binding and
‫the button I need to set on Click Listener a new view on Click Listener and the method that we were
‫calling when we use the app.
‫Button as app ViewModel.
‫Get app name again if you need to to call any widget inside a inside the layout.
‫We need to paste and write binding.
‫So binding.
‫I need to call the text view and update the text view with the set.
‫Text with the string.
‫Now our application has implemented the view binding.
‫Now let's run the application and see what happens.
‫And let's run it to see how our code works.
‫It's telling us there is a duplicate error attribute.
‫Okay, let's solve this by removing the duplicate errors from here.
‫I will remove this because it's fine above.
‫This attribute.
‫And the tools also is founded here.
‫So we need to remove this or this.
‫Let's run the application and see how it works.
‫And this is our application and it's working fine.
‫So this is the benefit of using the view binding.
‫This is the small benefit of using view binding and data binding.
‫Now let's learn what is the big benefit from using this type of patterns and this type of view binding?
‫You remember that our application is using here.
‫We unclick the app view model dot get name and then the app view model will go to the mutable live data
‫and observe for any changes and set the text to view for any changes.
‫Okay.
‫This no view binding allows you not to write all of these codes.
‫It's just simple writing inside the XML.
‫So the also the view binding will bind the data also.
‫Think about that.
‫XML will make the logic and listening for any changes, so this text view will listen for any changes
‫inside the view model.
‫How to do this.
‫We go up here underneath the layout and we put data and we close it like this.
‫Okay.
‫And this this field inside XML will contain the data and we are going to tell the XML that hey, go
‫and observe and listen for any changes inside the the text, the text view and the view the view model.
‫How to do this.
‫We start by typing variable and the name of the variable is called view model.
‫I will name it as this because I will show you and this indicates that we are going to get the data
‫from the view model and it's observing the view model.
‫So.
‫This is the package.
‫Start by the package master coding patterns applications and I need to specify where is the the view
‫model.
‫If we go to the hierarchy, we see that the app view model inside the UI package.
‫So the UI and I will set the app view model where it is.
‫Okay, it's very simple.
‫This is the location of the View model.
‫Now we scroll down to the text view because we need to update it and make it updated to the to the live
‫data, the mutable live data.
‫We start by making Add.
‫So this will allow you not to use the observe patterns and the observing the live data inside the main
‫activity or the Java classes.
‫You can do it here by typing Add and the view model and the app get app name or you can specify the
‫the view, the live data.
‫What is the name of the live data?
‫We go to the app view model.
‫And it is called mutable live data.
‫So back to here, and I will name it as mutable.
‫Mutable live data.
‫And the mutable live data.
‫And it is not accessible because we didn't specify here as it is public.
‫I make it as public and mean the ViewModel.
‫It will be solved.
‫Now let's close it and let's see how to use this.
‫So let's go back to the main activity and remove the observed pattern.
‫Okay.
‫This is the observing.
‫Listening to the live data and I will remove it because we have implemented the ViewModel and data binding
‫here.
‫And I'm telling that this text view, the text view with with ID, text view will be listening to any
‫changes occurs to the mutable live data that we have specified here.
‫Now let's assign to the main activity the.
‫Observing.
‫And let's say that hey, main activity, the XML will do the observing stuff.
‫So binding dot.
‫Set the ViewModel to.
‫Let me see if we have set it.
‫Okay.
‫Binding dot set to ViewModel.
‫I'm telling that app ViewModel is doing this.
‫And inside.
‫Binding dot.
‫Set.
‫Life cycle owner to this.
‫Okay, I'm telling.
‫Here.
‫Listening and observing the changes I am telling the activity or the
‫view binding will do the job of observing.
‫If you're binding.
‫With.
‫Text of view.
‫Okay.
‫So the text of view will be listening to the live data by doing this.
‫Now let's remove also the binding with the let's update the binding with the button and let's remove
‫the set on click listener.
‫And so the app will be a directly directly understand that it is going to do the work.
‫So our button is when it is clicked.
‫It is calling the app view model dot get app name back to the XML.
‫Let's see how to write it.
‫We will put here onClick listener, onClick attribute, and we will do as the same before up at and
‫put two curly braces and we will use the lambda which is telling that we are going to use a function.
‫And what is the name of the function?
‫I will go here.
‫It is an app view model dot get app name.
‫So this is the view model and I'm getting the app name.
‫The variable above is a view model which is representing this.
‫So no need to put app view model.
‫We will use the app, the variable name here and it is called the view model.
‫So because we this method is inside the view model, we can use it and we need to add get app name.
‫Okay.
‫In this way we have implemented, we have added the view binding and data binding to our application.
‫So our text view will be updated directly from the view model and the bottom will be directly updated
‫and getting the function to do by calling the view model dot get name.
‫Again, I will remove the binding from here.
‫So our application after making a clean architecture, no need for many codes inside the main activity.
‫It's just making 3 or 5 lines of code, so no need to make more and much complex, complex and not clean
‫architectures and not clean codes inside the main activity.
‫So let's run our application and see how our learning of clean architecture and these patterns were.
‫And like acts and behave.
‫So I will click the button and it's giving, giving me the name.
‫It's a clean architecture.
‫You see guys that our main activity not using and not hardcoded.
‫It is just simple and clean architecture and it's very easy.
‫So I hope you benefit from this course.
‫I suggest to make more practice and you open Android studio and start building your own apps by this
‫architectures and patterns.
‫And there is a homework.
‫I will post it in the next in the next videos, so I hope you write it down correctly.
‫So thank you guys.
‫I hope you benefit and understand clean architecture and architecture patterns and how to write clean
‫codes.
‫See you next Videos.
‫Please subscribe to our YouTube channel and rate us five stars on Udemy and download Master Coding application.
‫See you in next videos till next videos.