No Internet Checking - MVVM News App

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to new video so I've gotten a lot of messages from you that the new sub is crashing when the user doesn't have internet for example and that is true I have to admit that I totally forgot about that to check that so this video will be about checking if the user has internet connection or not and some other kinds of exceptions but the main part will be about checking if he has internet or not because that cannot be done that easily I mean we could just take all of our network requests here and wrap them around to try and catch and we will do that actually in this video but then we cannot really detect what kind of error it is so we won't be able to detect if that is actually an exception that occurred due to no internet connection so what we will do we will write a function that actually checks for us if the user is connected to the Internet and then we will create a function for the get breaking-news function the search news function that makes basically a safe API call and that handles all those exceptions for us so I will start to write a function in our view model here that checks if we are currently connected to the internet or not so if the user has an inter connection or not and that will be let's make it a private function has internet connection that will return a boolean if the user has internet or not of course and what we need for this function or what we need in this function is the connectivity manager and since that is a system service that we caress the context we can't simply call this inside of the view model here but we need the context for this and we cannot check this or we cannot implement this function in the activity class because we need it inside of the view model class so how are we going to solve that problem well the super bad practice would be to pass the activity context to the news view model constructor never do that that is a super bad practice because that is the exact reason why we have that view model that we separate the activity data from the DUI and also if you would use the activity context in the view model and the activity gets destroyed then you cannot simply that context anymore so we are definitely not going to do that but what we can do is we can use the application context because we know that the application context lives as long as our whole application to us then we are sure that it doesn't get destroyed while we use our app and for that we shouldn't inherit from view model here instead we should inherit from Android view model which is just the same as view model but inside an Android view model we can use the application context and you can see we need to pass an application parameter here and we don't have an application class yet so let's quickly create that go into our main package create a new class new Gotham file class and I will call this news application select class here and that is just a one-line class that inherits from application then we shouldn't forget to add this application class to our manifest file so let's quickly open that up and instead of our application tag we want to add a name attribute with our newest application class then go back to our useful model and here we want to reference an application in our constructor so I will call this F which is an application we will pass this app in the constructor of the Android view model and then we are good to go now we can use the application context instead of this view model class so let's scroll down to our has internet connection function and here we need to get a reference to our connectivity manager so we will wide well the connectivity manager and set it equal to get applications as you can see that function is only available in the Android view model not in the normal view model in the generic we have to pass our news application class so that Android actually knows which application class we want to refer to and then we can call that get system service afterwards and pass context dot connectivity service so this connectivity manager will just be used to detect if the user is currently connected to the internet or not and right now Kotlin doesn't know that this is actually a connectivity manager because this get system servers function just returns an object here so it could be anything that's why we need to cast that to a connectivity manager so we write as connectivity manager afterwards and if you look up in the Internet to about how to check for internet connection Android then you often find the solution of writing connectivity manager dot active network in whoo you can see that is deprecated and that has a function is connected I mean that looks very easy and that is also very easy but as you can see that is deprecated and it is deprecated since API level 26 I think and that's why we will use that function but only for a PA level below 26 and for the API levels above that we will have our own function or own functionality to check that so let's remove that again and that's why I will check if our build that version dot SDK end is bigger than or equal to filtered version code stud M which is API 26 in that case we cannot use that active network info function or that variable instead we have to just get the active network available from our connectivity manager so let's call it like this and set it to connectivity manager dot active network you can see that is not abrogated here and if that returns null then we know that we don't have internet so we can directly return false here and you can also see that we get an error here if we hold on to this line we are missing permissions on the the access network state permissions so let's go to our manifest file and change that add a user's permission here duplicate that line with ctrl deep and change this to access network state then we can go back and the error should begun as you can see then we want to get the network capabilities of the of that active network variable so well capabilities that is just needed to to check that network state and set it to connectivity manager dot get network capabilities and has our active network here and we also want to check if that is now and if it is we want to directly return false because then we don't have internet connection and with those capabilities we now have access to the different types of network and we can check if those are available or not so for example to check if Wi-Fi connection is available or not so what I want to do here is return when one expression and in here I want to check if capabilities that has transferred and I want to check for transport underscore Wi-Fi and you can see we have to import that let's press Alt + Enter to do that and in case we have that Wi-Fi connection then we want to return true of course because then we have internet connection and then we simply want to do the same for two other types of network one is transport cellular and one is transport Ethernet and in all other cases so in the else block we simply want to return false and what we now also want to consider is the case that our Android version is below Android marshmallow so in that case instead of the else block here we want to call our connectivity manager the our active network info which is deprecated here but not for API below 23 I think it is actually said it's 26 I think but yeah it's a be a lot 23 it doesn't really matter but below that this is not deprecated here we can make that now check here and call dot run and instead of this one block we simply return when expression we check the type of that active network info in case that is type Wi-Fi I also have to import that pressing Alt + Enter then we want to return true in case its type mobile we want to return true any case its type either net then we also want to return true and in all other cases we return false so as you can see that is very similar to what we do above here but it's just another function with that active network info here and we also need to make sure to return false in this case outside of these if statements and that is it for this function here so basically whenever you need to check if your user is currently connected to the internet or not then you can use exactly that code here what I'm going to do next here is to write a function to make a safe breaking-news call and a safe search news call that basically catch all possible exceptions and return those as an error message from our resource class so we have in our util package as you know that resource class here and whenever we will get an exception here inside of our view model class we will simply return this error class here instead of this yield class with the according error message so for example if we check for internet connection and it is not available then we will post this error class inside of our life data object with the message no internet connection so we can show that in our fragment let's go back to our newsroom model and start writing this function here that will be a private suspend function safe breaking-news call that will take the country code and in here we will first of all used our breaking news live data here and post the value that we are currently loading it we saw start loading then we want to open a try and catch block and we want to catch throwables so T is a throwable and for the the try block here we simply can copy this content from our get breaking news function here I will cut this right here and paste it inside of our try block and now we we only want to EXCI cute this code so we only want to make that actual request if we have internet connection so what we should do here before is we should check if has internet connection just use our created function here and then we can use that code and put it in there and in case we don't have internet connections so in the else block we want to use our breaking news life data dot post value and post an arrow resource so resource dot error and here we have to pass an error message I will just choose no internet connection and in case we have internet connection but there is still an error in our get breaking news with our request so basically just if this get breaking news function throws an exception that can happen then we want to catch that exception inside of this catch block here so I will open up a ran block here when T is an IO exception which can happen with retrofit then we want to use breaking news of live data dot post value also post an error resource resource dot error and that will just be a network failure if we have an IO exception and in other cases that must be a conversion error for retrofit so that just means that the conversion from Jason to your cotton object failed at some part which shouldn't happen in our app here but anyways just to show you how to catch that exception breaking news that post value resource start error and here we simply write conversion error and that is it for our safe breaking news call now we can simply copy that function for our search news call so we will have a safe search news call that doesn't take the country code instead it will check our search query and here we want to replace that breaking news with our search News live data copy this and paste it everywhere where we have that breaking news variable here here here and here then we want to replace that country code with our search query and that breaking news page with our search news page and we also want to replace this handle breaking news response with channel search news response of course but the rest should actually stay the same so now we can actually scroll up to our gap breaking news function or our search news function here and replace that code inside of this function block with safe breaking news call as our country code and that is it and we do the same for our search news function we delete all that code because we have that inside of our save search news call function and pass our search query here and the last thing we need to do is we need to actually extend our view model Factory so we are actually able to pass this application as a parameter to our view model constructor let's do this let's open this new view model provider factory here and you can see that throws an error and here we simply want to pass our app which is an application and pass that to our new sphere model and then we can go into our newest activity where we create that provide a factory here and here we also need to pass the current application and since we are in sort of an activity here we can just get this by writing application and what I think is also useful is to actually display a toast if we have an error so actually notify the user about that if he has no internet connection for example if we want to do that we can simply open up our breaking news fragment here and scroll down to where we handled that error so that is right here you can see right now we are just logging something and I want to replace that with the toast jealous top make text pass our activity as a context and simply write an error occurred and pass our error message tell us that length long and call that show then we can copy that line and paste it in our search news fragment right here where we have that log statement paste it in import toast and that should be everything we need to do and if you take a look now in my emulator I have airplane mode activated so we don't have internet connection right now if we run that app now it should show us a toast that says we don't have internet connection so I run the app let's see and there it is an error occurred no internet connection and that will of course also display the corresponding toasts for the other errors we caught here so for the IO exception and the conversion error I talked about so I hope this helped you guys to understand how you can check for that Internet connectivity because really a lot of people ask me about that that I include this in this mvvm series and I really agree that this is an important feature that your app should have I just personally totally forgot about that sorry for that but I hope you know how to do it now have a good day see you next video bye bye [Music] [Music]
Info
Channel: Philipp Lackner
Views: 11,417
Rating: 4.9772725 out of 5
Keywords: tutorial, android, development, learning, programming, programmer, kotlin, beginner, mvvm, clean architecture, architecture, internet, connection, connectivity, coroutines, coroutine, room, retrofit, network, request, error handling
Id: X4c2ZWG_ihU
Channel Id: undefined
Length: 17min 1sec (1021 seconds)
Published: Mon May 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.