Android Kotlin Dagger2 Implementation using Retrofit2, MVVM, RecyclerView using GitHub API.

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 i'll show you how you can access the retrofit instance in your view model i am going to make a api call to the github and then we will retrieve data and display it on the recycler view so let's begin by adding the dependencies what we required for implementation so for dependency we need the dagger and the com.google.dagger1 click ok the second dependency we need the dagger compiler this is same com.google.dagger next dependency we need for the retrofit and this should be the com dot square up retrofit two when we using the retrofit so we need the converter json this should be the same square up retrofit two and next dependency we need the glide and the last dependency we need the life cycle extension that is for the view model click app apply and then okay and let's see the build.gradle file so we can see all the dependencies over there but look for the dagger compiler and instead of implementation we need to use capped here and when you make this change kept and if you click on sync now you'll see this error capped is not recognized in that case go at the top of your build.gradle and add this id kotlin hyphen capped and i'm adding another plugin that is kotlin iphone android hyphen extensions so this we required to avoid uh find view by id so whenever we need to import any view i'll show you later in my project when i'm going to implement so now you see kept error is gone now and on the main activity currently i'm adding just a recycler view just to make the api call and get the data and displayed on the recycler view in the next step i'm adding some package one for dependency injection network dagger and another package i'm adding for model so inside the dependence injection we need to create some classes so first class will be creating retro module you can give any name to your module class and we will annotate it with the module and here we'll be implementing will be creating some functions which we are going to inject in our viewmodel so i'm first creating a function sorry the url which we are going to hit to the api and now i'm creating a function get retrofit instance and this function is going to return us the retrofit instance and inside this we are going to use this base url so we'll pass this base url here and since we added the json so we'll be use that json dependency here and then we'll call build and we'll annotate this function as a singleton also a provides so when we call this function as provide then we can inject this function wherever we want and we need another interface that is going to call a retro service interface and this is the same interface where we pass our method and we create a function and if there is any query parameter in the url then we define that and we complete our url by passing it inside the get and the query it's going to be here as a query parameter and it's going to be the string type and then it will return call and we need a model here so we'll create a data class over there inside the model folder or package in this class i'm giving name that recycler list and we'll define it as the data class and now we'll see what kind of data we are retrieving so it's the items list of items so it's gonna be the array or we can see a list of certain types so we'll define another data class we'll give it as name recycler data and define the variables that is the name first variable second variable we are going to parse description so we'll keep it and another is the inside honor so i'll create another class data class as honor and from honor we'll be using editor url so that is again the string type so our data and model class is ready now we'll pass the same inside our retro service instance and we'll import these now we'll go back to our retro module and create another function which is going to return us the instance of retro service interface so i'm creating a separate function for it it will return this retro service interface and we'll annotate with the singleton and provides so that we can inject this function as well inside our viewmodel or wherever we want to access now we'll create another class i mean interface that is retro component so this is the main component where we define what classes where in which classes we want to access our module and this is again the singleton and we'll annotate it as a component and we'll pass over modules inside this square bracket so we just now created retro modules we'll define it here and here we'll create a function that is inject and we'll define where we want to access so we are going to access our module inside of viewmodel so let's create a viewmodel first and we'll define this you view model inside our component if if you don't want to use hair view model then you can directly use the main activity so you can directly access that insect inside our main activity so it's totally up to you let's implement our view model now so now i'll be injecting that retro service interface so we'll be creating an instance of m service and that is the retro service interface so you see we pass inside our component or view model and that's the reason we can access this inside the viewmodel only and this is the as usual live data which we are going to use it in our activity so i'm just simply creating and initializing and then creating a function to return the slide data and i'm creating a function which is going to make the api call so now we don't need to create a retrofit instance here as we can directly access our retro service interface so we'll directly call our uh the function which we define inside our retro service interface and we'll pass the query parameter as the value atl you can pass anything then it's going to return as the retro recycler list so i'll pass the same and import it it should be outside and now we'll simply call nq function then we'll write these two functions on failure and on response in case of one failure we'll simply return null for now in case of success we'll check if the response is successful then return data and data should be response dot body in case of it's not successful it will simply return the null and here we need to initialize our application class so when we will create our application class we'll come back here so this viewmodel implementation is not yet completed i'll show you later before that we'll implement our main activity so that we can initialize our recycler view as you see i can directly able to access recyclerview without find view by id so that is the reason i have added that extension kotlinik android extension dependency in our build.gradle and now at this point of time we need a adapter so let's create a separate adapter for our recycler view we'll come back to our main activity first let us implement our adapter now implement all the three functions here we'll inflate our layout so we need to create our layout file so let's create our xml in this layout file i'm simply adding an image on the left hand side and the two text view on the right hand side of the row so we'll call a url to get the image and then we'll display the name and the description on textviews now we'll pass the class name layout name here and parent comma false we'll use the same name but we passed in our layout file and here also you can see i'm directly able to access image view it is all because of we pass the kotlin extension inside our build.gradle so we don't need to call find view by id again and again so this image you can see and i was talking about this extension so kotlin iphone android hyphen extension so when you add this dependency here uh plug in here then you don't need to call find view by id and we'll use glide here just to get the image and display down the image here and we need a function from where we can set the data from the outside we can update this list data so our adapter is ready now so let's use this adapter in our main activity now i'm creating another function that is for the view model so here we will be implemented where he will be initialize our view model and we'll use the view model name class name here inside get and we'll set the received data to the adapter so that's the reason we created this set update data function over there and then we'll call our adapter dot modify data set change in case of else so when we don't receive the data at that time we'll display the task to the user so if for any reason if api is not able to get the data then we'll display the test message error in getting the data and now at last we'll call make api call of our viewmodel now the last step we need to create another class that is our application class now we'll implement this application class oncreate function this is the first function which calls and our application launch and here we'll be initializing our retro component class so we already created a component class for our dagger now we'll be initializing it here so this dagger retro component is created automatically we did not created it so it should be created at the compile time so if it's you don't see this then clean or rebuild your project so you will be able to see this class here and creating a function which will return this retro component object that's it for this class but this class only called when we register it in our manifest so let's register this class in our manifest inside the application tag you need to call android column name and pass this class name you see it automatically appearing and since we are going to make the api call so we need to use the internet permissions so i am adding that too and as i said we'll come back here once we create our application class so let's call this application class in our view model so we'll be simply injecting our view model by accessing the gets retro component so you see now first we created our module where we have a url and we noted it with the module and then we created two function inside and we can access these two function anywhere inside our viewmodel so here we are accessing that hemp service instance of our retro service interface class i mean if you want to access directly the retrofit you can inject it inside our view model now it is the simple service interface as usual this is our module class where we inject where we want to access our uh retro module if you want to access inside our main activity then pass here main activity instead of main view model and then we initialize it inside our view model and we created our application class here we initialize our actual component when we did that then we registered it inside our manifest file and added the permission so let's run our application and see how it look like there you go so we are successfully able to implement make the api call get the data on a recycler view so everything looks fine to me 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
Info
Channel: LearningWorldz
Views: 2,321
Rating: 4.826087 out of 5
Keywords:
Id: _B0skaOiVCU
Channel Id: undefined
Length: 29min 31sec (1771 seconds)
Published: Fri Apr 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.