‫Congratulations, guys.
‫We finished all the parts of implementing groom database.
‫Now let's move to create the repository.
‫Repository is a class that is mainly used to manage multiple sources of data.
‫The repository class isolates the data source from the rest of the app, and provides a clean API for
‫data access to the rest of the app.
‫The repository can gather data from different data sources, different Rest APIs, cache local database
‫storage, and it provides the data to the rest of the app.
‫In our application, there is only one source of data, which is the room database, so we can skip
‫the repository step, but for demonstration purposes, I love and I like to follow the repository and
‫use repository in all my projects here.
‫Inside our project, I'll create a new package, I'll name it as repository, and also I'll create a
‫new package.
‫I'll name it as room.
‫I'll move these parts of the room database into this package.
‫Click okay refractor.
‫And here we go.
‫This is our main project package.
‫We have two folders the repository and the room.
‫Room contains the contact entity, contact Dao and the contact database.
‫This is for the room.
‫I am just making our codes more pretty and clean.
‫Now inside this repository package I'll create a new Kotlin class and I'll name it as Contact Repository.
‫Again, repository acts as a bridge between the ViewModel and the data source.
‫I'll start with passing here in the primary constructor private Val contact dao object.
‫So this parameter, the contact Dao is an instance of the contact Dao class or interface, which provides
‫the methods for interacting with the room database.
‫I'll use this instance the contact Dao to access all methods.
‫I'll start with val contacts equals to the contact dao dot.
‫Get all contacts in db.
‫Then I'll call all the methods that are presented here in the contact Dao inside the repository.
‫So suspend function insert contact which is of type contact class.
‫And here I'll use return dao dot insert contact and pass the contact parameter.
‫I'm passing here the contact that it is passed as a parameter.
‫Also I need to return as long and the same way.
‫I'll call the other three functions the delete contact, return dao contact, dao dot, delete contact
‫and pass the contact object update, return Dao dot update and pass the contact.
‫Also use the suspend function delete all in order to return Dao dot delete all.
‫This is the repository.
‫In summary, the Contact repository class provides a clean API for the ViewModel to interact with the
‫underlying room database through the contacts Dao.
‫It encapsulates the logic related to data operations, allowing the ViewModel to focus on handling UI
‫related logic.
‫The use of coroutines for suspending functions is common when dealing with room database operations.
‫To ensure proper, concurrent handling, so whenever you use the repository, you need to call all the
‫functions in the DAOs.