‫Hello, my friends.
‫After we've created the room database, it's time to create the repository.
‫So we will start creating a new class that acts as a repository that handle the data operations.
‫Creating a repository is a clean way to provide clear distinctions between how an application retrieves
‫data and how it uses or displays that data.
‫In our application, we have only one source, which is the room database.
‫So the repository will deals, will deal with the room database and the local data source.
‫In the next projects, we will see that there is this repository will deal with more than source, the
‫local data source and the remote data source, which is from the Json files from internet and using
‫the retrofit.
‫So we'll use retrofit with Mvvm and room with Mvvm, we will see the repository, how it will manage
‫the receiving and the displaying and the usage of data.
‫So let's jump into Android studio and start creating the repository for this project.
‫Now let's create a new package here.
‫And I will name it as a repository.
‫So create a new package.
‫I will name it as repository, and inside it I will create a new Kotlin class.
‫I will name it as node repository.
‫Click okay or enter.
‫And here we go.
‫This is our class node repository.
‫It will took, as we learned before in the MVM project in the previous MVM project, we need to pass
‫the database so private val db it's of type.
‫Data node database.
‫Okay, so.
‫I am passing the database here, and if we open the database here, we see that it is a room database.
‫Okay, so I need to pass a room database here and I will start with the functions if I click on the
‫Dow.
‫Here we open it.
‫We have five functions insert, update, delete and two queries.
‫Okay, One for getting all notes and one for getting all notes.
‫That for the search.
‫Function.
‫So we have five functions.
‫I told you in the previous videos and in the previous application that we need to create five similar
‫functions that are connected with these Dao functions.
‫So I will create a similar, a similar function to this insert node in the Dao inside the repository.
‫So I will start with suspend function because we are dealing with coroutines and we need to insert the
‫node using the background thread.
‫I need to pass an object of type node equal to db this db and here alt+ enter to import the package.
‫Master coding.
‫Note Taking App.
‫Model Note.
‫This is because it's a separate.
‫Class in a separate package.
‫DB dot get nodo dot insert note.
‫Okay, so I am accessing the database, connecting with the Dao and you can see that this function is
‫turned into yellow because we are going to, we use it here inside the repository insert note and it's
‫a note here.
‫This.
‫Okay.
‫Also, I need to use the suspend function delete node took in the parameter node.
‫Object and equal to db also dot get.
‫Note dow dot delete, note and note.
‫So I'm deleting deleting this note using the dow function called delete.
‫Now let's connect the update node function in the same way I need to use suspend function.
‫Update Node and node.
‫Object equal to db dot get node D0 dot update node and passing the node.
‫These are the three suspend functions.
‫If we look closer here, we don't have suspend functions here.
‫I told you that they are queries and they can be executed in the main thread.
‫So I need to create two functions here but not suspend functions.
‫Function Get all nodes.
‫And equal to DB dot get the node dot get all nodes.
‫I'm referring to this.
‫Function and the third or the fifth function called search note.
‫Note it took into the parameter query here, which is a string.
‫That's not null.
‫Equal to DB dot get node dot search node and I pass the query.
‫Okay, so the repository.
‫Its function is to link with the Dao.
‫If we are using the.
‫Room database and detect from where we need to insert, update, delete and do the function.
‫So we are going to link all the Deo functions to the repository and create similar functions to it.