Android Dagger Hilt implementation with Retrofit2, MVVM LiveData, RecyclerView, GitHub API in Kotlin

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to my channel in today's video i'm going to show you how you can implement dagger health using cortland in this video i'm going to show you how you can use retrofit mvvm and make the github api call and display the result on the recycler view in this video i'm going to use glide library as well just to show you how you can cache images and displayed on the recycler view so let's begin by adding the dependencies in our project so first dependency i am adding a hilt dagger health second dependency i am adding for the retrofit and when we use the retrofit so we need a converter as well so the same as i mentioned i'm going to use the glide as well and this dependency we need for the uh hilt android compiler and this life cycle extension is for the view model i am going to add one more dependency that is for our project level uh dependency so that is going to be the hilt android gradle plugin so this plugin will be adding inside our project level gradle file so let's click apply and ok and let's go to our gradle gradle file and see this last dependency this plugin gradle plugin we need to cut it and open our project level gradle file and paste it there and change this implementation to class path that's it for this class and now let's go back to our gradle file and this implementation for the android hilt android compiler we need to make it to the capped and when we use capped then we need to apply that capped plugin inside our plugin block so we need to use cotton cap and with the dagger held we need to add another plugin that is dagger dot hilt dot android dot plugin and i am going to add another plugin that is for kotlin android extension that help me to import the layout id or the view ids and now sync and then let's let our project to be sync now our project is sync now let's open our main activity and let's inside our main activity and layout i am simply adding a recycler view so on this recycler view will make the api call to the right github api and then get the results and then displayed on this recycler view and at this point of time let's implement health first so let me create a separate package for dagger in injection and now i'm creating a separate object class and giving a name as the app module is the object class and it need to be annotated as the module and then another annotation is install in and inside it we need to pass singleton component dot class and this class will basically return us the object of retrofit so for retrofit we need a url so i am defining a base url this is the url we'll be hitting to a make the api call so this is the github url and now i'm creating a function that is going to get retrofit instance and this is going to return the retrofit object now we'll simply create our retrofit builder we'll pass the base url and we'll add a converter factory that is the json converter factory and as we have defined this inside the app module so we need to annotate this as the singleton and provides that's it now since we have the retro object so we need an interface as well so let's create a network package and then inside this will create a retro interface we'll give any name i'm giving retro service instance inside this will be defining this is the get api call so i'll use as get and then we need a url parameter as the repositories and we'll give a any name any function name let's call it get api get data from api and the query parameter it need we need to pass a query parameter that is the queue and some value now any string and then it is going to return us the call and some object so let's create a data class which is going to hold our data the data class will give any name i am giving as the recycler list and inside this it is going to keep a list of some data type let's define data type of another data model that is going to kept the name description and another that is going to be the string type and that is going to hold the honor honor is the another data class i need to create and define another data class owner and it is going to just hold one variable outer url that is another string type so we'll use this class and pass it inside our interface so our retro service interface is also ready now we'll get its instance from the app module so we'll define another function retrofit dot create and we'll pass our interface name and similarly we'll define it as the singleton and provides now next step is we need to create application class so we'll give any name i'm giving my uh fill tap so this this is the class and we need to annotate this class as the hilt android app and we need to extend it with the application that's it in previously integer example we have to write lot of code at this point of time but in health we don't need to write we remove lot of boilerplate cord here now we need a viewmodel so i'm creating another view model as i mentioned we are going to use the viewmodel and since we are using pure model so we need to annotate it with the hilt view model and now we'll simply extend it with the viewmodel inside our init block will be initializing our livedata and creating a function which is going to return a live data that is just to set up the observer on this data and another function which is going to just make the api call but before that we need a repository so let's create a another kotlin class and give it a name as the repository retro repository and now we need to inject as the constructor and this constructor is going to hold the instance of our retrence service interface and inside it will be creating another function this is going to actually make the api call so this is going to get a query uh which we need to pass it to our interface and live data just to send the call back to our main activity so and now we need to write these two functions and we'll call livedata.postvalue and now we'll use this retro repository inside our viewmodel so we need to inject that inside the constructor so inject constructor and we'll import this inject and that will be the retro repository instance and now we'll simply call repository dot make api call and pass some value that is just some keyword like ny and we'll pass our live data we'll call this function from our main activity so now let's go back to our main activity and initialize our recycler view so i'll be creating a separate function for initializer recycler view so with recyclerview we need to add a linear layout manager and then we need a adapter for our recycler view so at this point of time we don't have adapter so let's create a adapter as well so i'm giving a recycler view adapter you can give any name to your adapter class and now we'll implement it by extending it with the recyclerviewadapter dot adapter and we'll define it type for our viewholder write all these three functions and we need to define this class my viewholder now now we'll define a bind function inside it it should be a parenthesis and now we'll have a data so that is going to be the list data of type list recycler data and we'll be creating another function that is going to set the data to list this list data variable from the outside now we need to inflate our layout here and at this point of time we need a layout file so let's create a layout for how our recycler data will look like so i'm giving a name recycler row and i'm simply defining a few parameter inside it like an image view and text view so on left hand side there will be image on right hand side there will be a to text view so let's go back to our adapter class and pass our xml file name and now it is going to return us my view folder and we'll pass this view and this onbindviewholder will be calling this bind function and we'll be passing the data and that data we can get from the list data based on the position will be called and now i'll extract all the layouts all the views which you define inside the xml file so that is one image you and the two textures texture name and texture description and now i'll simply set the data to these text view and now image view need to have the images so i'll use the glide here will pass the url so that is inside the data dot honor dot avatar url dot into and we'll pass the image view now our adapter is ready so now let's use this inside our activity or with our recycler view so first we need to create an instance of it and now we'll initialize this instance cz and we'll set it to our recyclerviewadapter and we'll call this function from the offer activity now another function we need to define for our viewmodel so it's time to call our viewmodel and set the observers so if there is no data or null then we'll display a error toast to the user and if there is data then we'll simply set our data or the list to the adapter and then we'll call notify data set change and we'll call our view model init view model function from the on create and at last we need to call load list of data function from our main activity now almost we are ready but last step is we need to open our manifest file and add the internet permission and we need to initialize our application class so we define my health app so we need to define this here inside our application block this one i was referring here so we need to define it here now let's review it or app model class we have two function one is returning retron stance one is returning retro service and interface and this interface is having few functions and this is the data model class and this is our main activity where we are initializing our recycler view and view model and let's run our project and see so it's simply crash let's see what we missed now error logs there's something here in main activity something is missing while initializing our viewmodel okay so we missed to annotate our main activity so we need to define it as the android entry point now let's run our application and see there you go so we can see the results here so we made the api call getting the results displaying it on the recycler view with the using of glide library we can see the images as well so guys that's all for today's video thank you so much for watching if you have any question you can ask me in the comment section thank you you
Info
Channel: LearningWorldz
Views: 1,702
Rating: 4.9200001 out of 5
Keywords:
Id: al_uZ9Uh4ZI
Channel Id: undefined
Length: 25min 20sec (1520 seconds)
Published: Sat Jul 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.