Android | Paging3 with Room DB, Dagger Hilt, MVVM, RecyclerView using 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 use room database with beijing also in this video i'm going to show you how you can make the basic implementation with the health also will display some data on the recycler view with the help of mvpm so in this video i'm going to insert some record and then retrieve data and display all the records on the recycler view so let's begin by adding some dependencies in our build.gradle file so here i have few dependencies already knotted down and i'm just simply adding inside the build.gradle file so these are the dependencies for room health view model and dagger so this first two dependencies are for the view model the next dependencies for the paging and next two dependencies are for the room database one is the room runtime one is the room compiler and next two dependencies are for the hilt so one is again hilt android and another is the hilt android compiler and these three plugins we need to add inside our build.gradle file so first plugin just to import the layout overview second since we are using the cap so we need to have this plugin here so i can show you inside the room compiler and that helps we are using and this is the hilt plugin and one more dependency we need to add inside our project level gradle file so i'll open and inside our dependencies we'll paste that dependency now we'll simply close it and think our project so project is sync now first i'm adding some packages so first package i am adding for the database and i'm creating a file you can create it with the any name and i'm giving name as the app database so first thing we need to annotate it with the database and we need to extend it with the rom database and then we need to mark this class as the abstract and inside this a notation will pass the entities that will go inside this square brackets and the version of the database since this is the first time i am giving was number one and export schema should be false and now inside the companion object that is similar to static in java so i am defining a instance of this class and now with the help of a function i'll return the instance of this app database class now we'll check if db instance is null then initialize it that is dom dot database builder and inside our class type that will go outside the same class name and inside this bracket first parameter is the application context second parameter the database class name that is again same app database and third parameter is the database name so it's up to you what name you want to give here and now we'll simply build it and return the db instance variable now we need a now so i'm creating another interface that is the abdo so inside the dow we need to first thing annotate it with the name of dao and inside this will write our query like query or insert update delete whatever you want to do all the query will go here so this is get all record that means it's retrieving the data so that will be annotated with the query and it will return this paging source and inside this integer and the model so i don't have any model right now or the data file or entity so let me write this query first select start from so table name so let's create an entity first so i'm choosing a name character data and i'm annotating it with the entity and we can give any table name so i'm giving it name as character and inside this bracket we'll define the parameters so what columns it's going to have so first column is the id that is the primary key that auto generate should be true for it and the column info will give any name like id and the variable will define it it is increased type and the default value is zero second parameter is column info as name name equal to name i'm giving the column name as the name and it will contains the string type of value so our entity is ready now we'll use the same entity and pass it inside the paging source and we know the table name now as we just now defined inside the character data and i'm just making it as a order by id and description so the records inserted at the end will retry first simple and inside our app database will define abstract function that will return just the app daw and inside our entities will define our entity class so we'll parse it here so wrap database is ready now i am defining another function which is the insert so so that we can insert some records inside the database then we'll use this select query to retrieve and display it in the paging now i'm creating another package that is for the dependency injection and inside this i'm creating my module class and as you know module class should be annotated with the module and then annotate with the install in and it should be a singleton component class and now we need to define a function which can return the instance of our database and it will return simply app database dot get app db instance and we'll pass this context and when we define any function inside the module then we need to annotate it as the singleton and provides and now i'm creating another function that will return just the instance of abdo and when we have the amp database instance and we can simply call get app down function which we define as the abstract and then we'll mark this function as the singleton and provides and when we have our app module then we need to have the application class as well so i'm creating an application class now and this is simple application class we just need to define and we need to enter a note it as a health android app and we need to extend it is the application that's it for this class but we need to declare this class inside our application tag otherwise this is not going to work so here we need to register it inside our manifest that's it for this class and now i need to create a repository class that will actually make a call to our room database and we need to inject its constructor and that that will hold the instance of app tau and we'll simply create a function one is to return the records of as a paging source so the same return type we defined inside our dao so that's why i'm keeping at the same signature for this function so app.get all records so that get at get all records have the same signature as the return time here is i'm creating another function which is to insert record so this can take as input character data and then amp.insert record and we'll pass this character data object so repository is also ready now now it's time to create a view model so let's create a separate package and inside this package i'm creating a separate class as main activity view model so first thing in this view model we need to annotate as the hiltview model and then we need to inject the repository inside the constructor and then we will extend it with the viewmodel class inside this will simply define a function that will be get all records and then it will be returning the flow paging so basing data and that will be the character data type so paging config will define the page size some records like 20 and the max size like 200 that's better another parameter will be the paging source factory that is our repository dot get records so that is the function which is going to return basic source and then low dot cash in your mobile scope so this function is still showing some error [Music] okay so this package is wrongly import i need to import it properly that should be the quarterly next routine flow this one is not the java util dot concurrent there you go and i'm creating another function that is to insert some dummy records so i'm just simply making a for loop so one two 500 so it will insert 500 records and i'm simply calling repository dot insert record and we'll pass our object of character data i'm going as named dummy and then we'll print this i for the index that's it so it will insert 500 records and then we'll retrieve all the 500 records on the recycler so now it's time to add a recycler view inside our main activity layout file now inside our main activity we will be initializing our recyclerview inside a separate function that is a good habit to create a separate function for all the functionality we'll be adding a linear layout manager i am simply adding a decoration that is the separator for our recycler view row now we need the adapter but we don't have any adapter so we need to create an adapter but we'll come to this point later before that let's initialize our view model as it's already ready so i'm just simply initializing my main activity view model inside our lifecycle scope dot launch when created inside this block we'll call view model dot get all records that collect latest and inside this block will simply submit it to the adapter so we don't have any adapter right now we'll come up to this point once we have the adapter so before that we'll call these two function from the oncreate and we'll mark this main activity as the android entry point so let's create our view adapter class so this is not going to be the simple adapter class this is going to be little different that is the pacing data adapter so we need to extend it with the paging data adapter inside that will be defining our model or the data type and then the view holder so we will create my view holder and will implement those two functions inside it on create view holder and on bind view folder and now we'll create our view holder class that is my view holder we just now gave the name above inside this constructor will extend it with the recyclerviewviewholder and then we will pass view inside its constructor so we'll pass this view and now we'll define a function the bind function and that will be holding the character data and now we need to have another class that is the diff util callback and that need to be extend diffutil dot item callback and that is going to hold the same data type character data and there will all write two functions our item the same our content the same and now we'll check the old value and the new value and similarly we'll check it here as well and now we'll pass the same class to our view holder or the paging data adapter and now inside our own create view holder will inflate our layout as usual we do it for the regular regular adapters so this is the same step will pass the context then inflate dot inflate and we'll pass our layout file so we don't have any layout right now so let's create a another layout file for our recycler row first thing we need to make it a wrap content and then we need to add some views inside it so i'm simply adding one text view because we just have one name to display we are inserting just name so we'll display just name so we'll pass the same class name here layout recycler row dot parent or comma false and we'll return this view as our my view holder instance of myviewholder and we'll pass this inflator now inside our onbindviewholder we'll simply call on bind holder.bind and then we need a data so get item and we'll pass the position we'll force it to not null and now inside the bind function will first retrieve the text view will set the name so character data dot name our adapter is ready now we'll simply set this adapter to our recycler view inside our main activity so we'll define it we'll initialize it here and we'll set this to our recyclerview adapter also we'll use the same adapter and submit data called the submit data function and we'll set the data and now i'm simply calling the insert function as well so that it can insert some dummy record at the same time and then we can retrieve and display all the records on a recycler view so let's that's it let's run our application and let's see how it look like there you go so you can see 500 records so it is as you see it here i insert some 500 records and it is this 500 so you can see the dummy 500 codes and it is crawling perfectly escape is crawling to infinite and why the fifth 500 records came on the top 501 index is because we are retrieving inside the decreasing mode you can see it's keep extending keep retrieving the reports up and down 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: 846
Rating: 4.875 out of 5
Keywords:
Id: 8EoGVJK0sj0
Channel Id: undefined
Length: 26min 20sec (1580 seconds)
Published: Mon Aug 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.