blonsky95 / DigiCoachAndroid

0 stars 0 forks source link

Check when no internet connection - handle connection loss #76

Closed blonsky95 closed 4 years ago

blonsky95 commented 4 years ago

So either at app launch or during any operation while using app - handle it gracefully

blonsky95 commented 4 years ago

https://stackoverflow.com/questions/19050444/how-to-handle-with-no-internet-and-lost-connection-in-android

blonsky95 commented 4 years ago

There is a class in Utils now which returns if connected to Internet (isConnectedToInternet(context)) - it runs a different block of code if device is below or above API 23

However findings in behaviour:

As many of the operations involve Firebase Firestore operations, it is important to understand its behaviour when encountered with no Internet connection.

If a query is made, a local copy is stored. When there is Internet it does it all over again, it doesn't use it as a cache (it seems). So when there is no internet, and a query is made, if it has a copy of it, it will run that, else it will retuns a docs file of size 0. In the code I am using a check for docs size to know if there is an internet connection issue (User login - authentication, and Library query for store exercises)

if (docs.isEmpty) {

                val toastMsg: String =
                    if (com.tatoe.mydigicoach.Utils.isConnectedToInternet(application)) {
                        "User doesn't exist"
                    } else {
                        "There is no Internet connection, connect to Internet to login"
                    }
                isDoingBackgroundTask.value = false

                Toast.makeText(
                    activity, toastMsg,
                    Toast.LENGTH_SHORT
                ).show()

            } else {

For cloud backup and download I am simply making an internet check before starting the operation, if the operation starts and then internet is removed it might stay with the "Talking to cloud" dialog for longer but that is unlikely.