Handling Breaking News Response - MVVM News App

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to new video in this video we will finally make our network request and get the response of the breaking news and display that in our recyclerview so our architecture will be that we have that news repository so if you don't know mvvm that well we have that news repository the purpose of the repository is to get the data from our database and our remote data source so from retrofit from our api so in this news repository we will have a function that directly queries our API for the breaking news and in our news view model we have that instance of our news repository so from within the media the view model we will call the functions from our new repository and here will also handle the responses of our requests and then we will have live data objects that will notify all of our fragments about changes regarding these requests and if you're not familiar with all these terms then don't worry it will get clearer when we actually implement it so first of all we can go into our news repository and write a function to get the breaking news from our API and because the network function is a suspend function we also need to make this function a suspend function which will be called get breaking news this will take the country code which is a string and a page number which is an integer and that function will just call from our retrofit instance our API get breaking news and pass the country code and the page number here and then we can go into our news view model and here I will create a live data object which I will call Val breaking news that is immutable life data of type resource so our wrapper class that we defined in the last video of type news response and import resource here and I'll set that to a new mutable life data and I hope you remember that week where that resource class and the purpose of that so I will quickly open that up that is used to as a wrapper class for our generic take to type T here which is our network response so that is in our case our news response and depending on which state we omit here so either success error or loading we can handle that different in our fragment but just wait until I implement the function then it will get clear I promise first of all we need two more variables here one for our breaking news page because we will manage the pagination in this view model later on because if we would do that in the fragment the current page number would always reset if we rotate the device and the view model doesn't get destroyed when we rotate it so I will just initially set this breaking news page to 1 but later on we will implement some logic that will actually paginate our responses and now we can implement our function that executes our API call from the repository so function get breaking news it only takes a country code which is a string because we don't need the page number parameter here because we already have that breaking news page declared in our view model and that will be the place where we handle that so in this case we are fine with a single parameter and now because inside of our repository you can see that this is a suspend function that means that we have to call this function from another system so span function or from within a crew routine and we don't want to make this a suspense function because then we will just propagate that function to our fragment and then we would need to start the crew teen in the fragment and we don't want that so what we need to do is we need to start a crew routine in this function and in the view model the best way to do that is with the view model scope so let's write view model scope dot launch so if you don't know that the view model scope will make sure that this crew routine is stays only as long as our view model is alive and that is something you should always use a new new super model and your general view model of course and what we want to do in this function before we make the actual network all we want to omit the loading state to our life data because we now know that we are about to make the network call so we should omit that loading state so our fragment can handle that and that is very easy to to just use our breaking news live data and post a new value in this which is resource downloading and now we can make our actual response so you add value it spawns that is the variable we will save the response in then we've called our news repository and get our breaking news pass our country code and our breaking news page so in this line all the magic happens here we make our network response this is a spent function you can see that on that little arrow here and when their function is finished the crew teen will just continue with the next line and here it can be sure that the current network response is saved in that response object so in this case we can handle our response and for that I want to create a separate function in which we will later also handle the pagination but for now I will just create a function let's make it a private function actually private function handle breaking-news response will spawns and that takes a response of type response and newest response and we need to import response here from retrofit of course and this function will return a resource of type news response so in this handle breaking news response function we will now decide whether we want to omit the success state in our breaking news life data or the error state and for now this function will be very easy we will just check if our response is successful then we need to check if the body of our response is not equal to null which it is not when it is successful and if it does not know then we want to return a new resource dot success and I'll actually give this a name result response and pass our result response here and in other cases if the response is not successful we want to return resource dot error and pass our response dot message so I don't think that is anything I need to explain we just check if a response is successful if it is we return the the success state if it's not the arrow state and now it can call our breaking news live data and post a new value and he repairs our handle breaking news response and we need to pass our response that we got from our retrofit and if you now don't know what this life data object here is that is just used for our fragments so of fragments can subscribe to that life data as observers and whenever we post changes to the live data like here we post the loading State or here we post the response success or error State then our fragments will automatically get notified about that change and that is very useful when we later on rotate our device then we immediately get the current up-to-date data from our view model then we can go into our breaking news fragment and here I will create a light init var for our news adapter for the recyclerview adapter that is a news adapter of course and I will quickly write a function to set up our recyclerview private function setup recyclerview that function will set our new adapter to a new newest adapter and then we will call RV breaking news dot apply and set the adapter of that recyclerview to our new adapter and we want to set the layout manager layout manager to a new linear layout manager and pass activity here and then simply call that function in on view created and now we can use our view model here to call our breaking news live data and call observe on that so we basically subscribe to all the changes regarding their life data so whenever we get new breaking news then this observer will be called and we can handle that new response in our fragment and update our recycler view so as an as a lifecycle owner in a fragment we always need to pass the view lifecycle owner and as a second parameter we pass a new observer here and you can see the parameter of that observer is our resource of type news response so I will give this a name response and depending on what what state that response is we want to handle that differently so when response is resource dot success we need to import resource we shall start success in that case we want to hide our progress bar which is a function that I will create right now below here private function hide progress bar and that will just take our fed G nation progress bar from our breaking news fragment and call that visibility and set it to view dot invisible then we can copy that function and make the same for showing the progress bar show progress bar and set the visibility of that to visible of course then we can go back up here so the first thing that happens when we go to success response is we want to hide our progress bar because then we are not in the loading state anymore and then we can call our response dot data dot let so we check if the data is not equal to now and in that case we can give this a name noose response and here we just want to set our new adapter dot differ dot supplement list and pass our news response to articles then if our resource is a resource dot error in that case we want to hide our progress bar - because we're not loading anymore and we want to take our response that message and make that null check and I will give this name message and in here we just want to log in error tag import not check here want to import log come on like this and I will create a tag up here well tag is equal to breaking news fragment and then we can just write an error occurred and pass our message and in case it is a loading resource so is resource dot loading then we simply want to show our progress bar and what we also need to do is we need to go back into our news view model and actually call our get breaking news function of course because right now we are not even making the network request so nothing will happen and I will call this in the init block of our news view model so just call get breaking news here and I will pass us as a country code but you can choose your country of course you can find all the country codes that are available on the news API dot org website and to make that network call actually work we also need to add the internet permission of course in our manifest so let's open that up and add a user's permission internet here and if we now run our app and try it out then you see it is stopping and if we take a look in Lock ad we get a little error this socket failed EEPROM if you get that error then that is not a really bad error you can solve this error simply by uninstalling that app from your emulator and reinstalling it if you have started it before so in case you've done that and we can really try it out try out of our app is working it's loading and there we go here our breaking news and currently the pagination is not working we only have that single page but we can get our news we can display them in our recycler view those two news just don't have an image here so that is not something that has to do with Glide because for those other news it's working so yeah and also if we rotate our device we immediately get the data from our life data so it's working perfectly fine so I hope this video helped you to understand everything we did here if so please leave a like and comment below and also if there is anything I can improve on please let me know in the comments have a good day see you next video bye bye [Music] [Music]
Info
Channel: Philipp Lackner
Views: 12,607
Rating: 4.9597988 out of 5
Keywords: tutorial, android, development, learning, programming, programmer, kotlin, beginner, mvvm, clean architecture, architecture, skeleton, fecthing, fetching, network, networking, api, retrofit, room, database, remote, coroutine, coroutines, ktx
Id: TqGrjW3RY8o
Channel Id: undefined
Length: 14min 31sec (871 seconds)
Published: Fri May 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.