Skip to content

Instantly share code, notes, and snippets.

@hissain
Created May 5, 2020 18:25
Show Gist options
  • Select an option

  • Save hissain/d9c4651be3ca2b60358f6de97859047c to your computer and use it in GitHub Desktop.

Select an option

Save hissain/d9c4651be3ca2b60358f6de97859047c to your computer and use it in GitHub Desktop.
private fun loadWebView(){
val loader = Thread {
when {
MyReachability.hasServerConnected(this) -> runOnUiThread {
hideLoadingDialog()
Log.d(classTag, "Loading ${Constants.LANDING_SERVER}")
mWebView.loadUrl(Constants.LANDING_SERVER)
}
MyReachability.hasInternetConnected(this) -> runOnUiThread {
//showServerConnectionLost()
}
else -> runOnUiThread {
showInternetConnectionLost()
}
}
}
loader.start()
}
fun showInternetConnectionLost(){
try {
val dialog = AlertDialog.Builder(this, android.R.style.Theme_Material_Light_Dialog_Alert).create()
dialog.setTitle("No Internet Connection")
dialog.setMessage("Please make sure your Wi-Fi or mobile data is turned on, then try again.")
dialog.setCancelable(false)
dialog.setButton(DialogInterface.BUTTON_POSITIVE,"OK", DialogInterface.OnClickListener { _, _ -> finish() })
dialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Retry", DialogInterface.OnClickListener { _, _ ->
val hThread = HandlerThread("connectivity")
hThread.start()
Handler(hThread.looper).post {
if (MyReachability.hasInternetConnected(this)){
Handler(Looper.getMainLooper()).post{
dialog.dismiss()
webView?.reload()
}
}else{
Handler(Looper.getMainLooper()).postDelayed({showInternetConnectionLost()}, 500)
}
}
})
dialog.show()
} catch (e: Exception) {
Log.e(classTag, e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment