Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network error handler code refactoring #95

Open
Harsh2098 opened this issue Feb 23, 2019 · 1 comment
Open

Network error handler code refactoring #95

Harsh2098 opened this issue Feb 23, 2019 · 1 comment

Comments

@Harsh2098
Copy link
Collaborator

A) Change nested ifs in all fragments to if-else-if ladder.

if (!ConnectionUtils.getConnectionInfo(context)) {
     uiThread { networkDownHandler.onNetworkDownError(resources.getString(R.string.error_check_internet)) }
} else if (!ConnectionUtils.isReachableByTcp(Constants.HOST, Constants.PORT)) {
     uiThread { networkDownHandler.onNetworkDownError(resources.getString(R.string.error_server_down)) }
} else {
    // Do actual thing
}

B) Apart from having RETRY button in that non cancellable (.setCancelable(false)) dialog box which you would be displaying from MainActivity, you can automatically detect if internet is available again by doing this:-

  1. Add new module to dagger (it'll make MainActivity.kt short)
@Module
class ConnectivityModule {
    @Provides
    @BingoApplicationScope
    internal fun getConnectivityManager(context: Context) = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

    @Provides
    @BingoApplicationScope
    fun getNetworkRequest() = NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
            .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).build()
}
  1. Inject these 2 things in MainActivity:-
@Inject
    lateinit var connectivityManager: ConnectivityManager

    @Inject
    lateinit var networkRequest: NetworkRequest
  1. In MainActivity.kt in onResume()
    connectivityManager.registerNetworkCallback(networkRequest, networkCallback)
  2. In MainActivity.kt in onPause()
    connectivityManager.unregisterNetworkCallback(networkCallback)
  3. Create networkCallback object like this:-
networkCallback = object : ConnectivityManager.NetworkCallback() {

            override fun onAvailable(network: Network) {
                super.onAvailable(network)
                runOnUiThread {
                    // Do something ....
                }
            }

            override fun onLost(network: Network) {
                super.onLost(network)
                // Do something as network lost
            }
        }
@Harsh2098
Copy link
Collaborator Author

Part B in the above mentioned comment has been implemented.

@Harsh2098 Harsh2098 added this to the Testing Phase milestone Jan 16, 2020
@delta delta locked as too heated and limited conversation to collaborators Oct 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants