|
1 | 1 | package crypto.delta.exchange.openexchange |
2 | 2 |
|
3 | 3 | import android.os.Bundle |
| 4 | +import android.util.Log |
| 5 | +import androidx.appcompat.app.AlertDialog |
4 | 6 | import androidx.appcompat.app.AppCompatActivity |
| 7 | +import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork |
5 | 8 | import com.google.android.material.bottomnavigation.BottomNavigationView |
6 | 9 | import crypto.delta.exchange.openexchange.utils.setupWithNavController |
| 10 | +import io.reactivex.android.schedulers.AndroidSchedulers |
| 11 | +import io.reactivex.disposables.Disposable |
| 12 | +import io.reactivex.schedulers.Schedulers |
| 13 | +import kotlinx.coroutines.CoroutineScope |
| 14 | +import kotlinx.coroutines.Dispatchers |
| 15 | +import kotlinx.coroutines.launch |
| 16 | +import kotlinx.coroutines.withContext |
| 17 | + |
7 | 18 |
|
8 | 19 | class MainActivity : AppCompatActivity() { |
| 20 | + private var connectivityDisposable: Disposable? = null |
| 21 | + private var internetDisposable: Disposable? = null |
| 22 | + private var alertDialog: AlertDialog? = null |
9 | 23 |
|
10 | 24 | override fun onCreate(savedInstanceState: Bundle?) { |
11 | 25 | super.onCreate(savedInstanceState) |
@@ -40,4 +54,61 @@ class MainActivity : AppCompatActivity() { |
40 | 54 | intent = intent |
41 | 55 | ) |
42 | 56 | } |
| 57 | + |
| 58 | + override fun onResume() { |
| 59 | + super.onResume() |
| 60 | + |
| 61 | + connectivityDisposable = ReactiveNetwork.observeNetworkConnectivity(applicationContext) |
| 62 | + .subscribeOn(Schedulers.io()) |
| 63 | + .observeOn(AndroidSchedulers.mainThread()) |
| 64 | + .subscribe { connectivity -> |
| 65 | + Log.d(TAG, connectivity.toString()) |
| 66 | + val state = connectivity.state() |
| 67 | + val name = connectivity.typeName() |
| 68 | + Log.i(TAG, String.format("state: %s, typeName: %s", state, name)) |
| 69 | + } |
| 70 | + |
| 71 | + internetDisposable = ReactiveNetwork.observeInternetConnectivity() |
| 72 | + .subscribeOn(Schedulers.io()) |
| 73 | + .observeOn(AndroidSchedulers.mainThread()) |
| 74 | + .subscribe { isConnectedToInternet -> |
| 75 | + CoroutineScope(Dispatchers.Main).launch { |
| 76 | + withContext(Dispatchers.Main) { |
| 77 | + if (isConnectedToInternet) { |
| 78 | + if (null != alertDialog) { |
| 79 | + if (alertDialog!!.isShowing) { |
| 80 | + alertDialog!!.dismiss() |
| 81 | + } |
| 82 | + } |
| 83 | + } else { |
| 84 | + alertDialog = |
| 85 | + AlertDialog.Builder(this@MainActivity, R.style.AlertDialogStyle) |
| 86 | + .setTitle("No Internet!!") |
| 87 | + .setMessage("Check your internet connectivity!") |
| 88 | + .setIcon(android.R.drawable.ic_dialog_alert) |
| 89 | + .setCancelable(false) |
| 90 | + .show() |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + Log.i(TAG, isConnectedToInternet.toString()) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + override fun onPause() { |
| 100 | + super.onPause() |
| 101 | + safelyDispose(connectivityDisposable) |
| 102 | + safelyDispose(internetDisposable) |
| 103 | + } |
| 104 | + |
| 105 | + private fun safelyDispose(disposable: Disposable?) { |
| 106 | + if (disposable != null && !disposable.isDisposed) { |
| 107 | + disposable.dispose() |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + companion object { |
| 112 | + private const val TAG = "ReactiveNetwork" |
| 113 | + } |
43 | 114 | } |
0 commit comments