‫Welcome back.
‫We've done with the remote data source part.
‫Now let's move to the repository.
‫I'll create a new package named as repository.
‫And inside this package I'll create a new Java class.
‫I name it as Quiz Repository.
‫And the repository is responsible for interacting with API service interfaces and handling data retrieval
‫and operations.
‫Please go back to the previous sections, the mVVM plus retrofit in order to learn deeply about these
‫repositories.
‫But don't worry, I'll cover everything.
‫I'll start by creating Private Questions API and I name it as question API.
‫This is an instance that will be used to interact with the API service methods.
‫Then I create a constructor having this API and I'll remove this API because I need to define a new
‫one here equals to new retrofit instance.
‫This is the class that I created before dot get instance dot create.
‫So I'll use create method here and I'll pass questions API dot class.
‫What does this code mean.
‫New retrofit instance.
‫This creates an instance of your retrofit instance class that we created before, which is responsible
‫for providing a retrofit instance with a predefined base URL and Json converter.
‫Factory dot retrofit instance.
‫This method call gets the retrofit instance configured by your retrofit instance class and dot create
‫method.
‫This method creates an implementation of the API's interface and in this case it's a questions API.
‫The question API interface likely contains methods for making network requests to retrieve questions
‫from a web service, and this is what we've done before to get the questions from the API.
‫Another method to create is of type LiveData.
‫Did you remember our friend LiveData?
‫Our old friend?
‫So LiveData of type question list get questions from API.
‫In the repository we define the methods similar to the interfaces in retrofit and in room database.
‫And we talked about the repository in details in the previous section.
‫So please go back to the Retrofit Plus mVVM section.
‫Now I need to start with defining a mutable LiveData of type questions list, and I name it as data
‫equals to new mutable live data.
‫And the mutable live data is a class provided by the Android architecture components, and is commonly
‫used to hold and observe data changes.
‫Also, I advise you to go back to the live data lessons in order to learn more about this type of data.
‫But think about it.
‫Just a data structure used to hold and observe data changes.
‫And inside this get questions from API, I need to use call of type questions list and I'll name it
‫as response equals to questions API dot get questions.
‫And you can see I'm calling this method that returns a call of type question list Alt+.
‫Enter to import the class from the retrofit package.
‫Now I want from you to focus with me.
‫I'll use this response in order to get the list of questions.
‫Response dot enqueue or execute.
‫Which method these two methods are used to execute network requests.
‫Execute is a synchronous method used to make network requests in retrofit.
‫When you use execute, the network request is executed on the current thread, which is often the main
‫UI thread.
‫This means that the request blocks the current thread until it's completed.
‫It should be used with caution in Android, because running network requests synchronously on the main
‫thread can lead to application not responding, and are errors causing the user interface to freeze
‫during the request.
‫There is no need for a callback when using execute method, because the method returns a response object
‫directly, and you can see it here the enqueue method.
‫And this is what we're going to use and I.
‫Prefer using ink.
‫Ink is an asynchronous method used to make network requests in retrofit.
‫When you use ink.
‫Method, the network request is executed on the background thread and retrofit handles the request and
‫response asynchronously.
‫It's designed to be used in scenarios where you don't want to block the main UI thread while making
‫a network request, because blocking the main thread can lead to unresponsive and frozen user interfaces.
‫You provide a callback as an argument to an ink, so for that I'll use a new callback.
‫Questions list.
‫Also, I can use lambda, but here we are overriding two methods on response and on failure.
‫So this callback contains method like on response and on failure methods that will be invoked when the
‫request is successful or encounters an error, respectively.
‫I'll start with a questions list.
‫Question list equals to response dot body.
‫And here I need to add data dot set value to the list.
‫Here I am saving the data to the list because data is a mutable live data.
‫Okay, so I'm getting this response body, getting this list and storing inside this data this immutable
‫live data of type question list, getting all the list of questions.
‫Then the last step is to return this data.
‫This is our repository.
‫Now let's move to the ViewModel layer.