[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[taler-taler-android] 02/02: [wallet] simplify state exposed by NetworkM
From: |
gnunet |
Subject: |
[taler-taler-android] 02/02: [wallet] simplify state exposed by NetworkManager |
Date: |
Tue, 19 Sep 2023 17:18:33 +0200 |
This is an automated email from the git hooks/post-receive script.
torsten-grote pushed a commit to branch master
in repository taler-android.
commit 242bc31d9d6bdcdc4455255412a6a35cec27b5ab
Author: Torsten Grote <t@grobox.de>
AuthorDate: Tue Sep 19 17:18:23 2023 +0200
[wallet] simplify state exposed by NetworkManager
---
wallet/src/main/java/net/taler/wallet/MainActivity.kt | 11 ++---------
wallet/src/main/java/net/taler/wallet/NetworkManager.kt | 16 +++++++++++-----
2 files changed, 13 insertions(+), 14 deletions(-)
diff --git a/wallet/src/main/java/net/taler/wallet/MainActivity.kt
b/wallet/src/main/java/net/taler/wallet/MainActivity.kt
index bf1fd6d..a49890e 100644
--- a/wallet/src/main/java/net/taler/wallet/MainActivity.kt
+++ b/wallet/src/main/java/net/taler/wallet/MainActivity.kt
@@ -142,15 +142,8 @@ class MainActivity : AppCompatActivity(),
OnNavigationItemSelectedListener,
if (it) barcodeLauncher.launch(scanOptions)
})
- model.networkManager.networkStatus.observe(this) {
- ui.content.offlineBanner.visibility =
- if (it != null
- && it.hasCapability(NET_CAPABILITY_INTERNET)
- && it.hasCapability(NET_CAPABILITY_VALIDATED)) {
- GONE
- } else {
- VISIBLE
- }
+ model.networkManager.networkStatus.observe(this) { online ->
+ ui.content.offlineBanner.visibility = if (online) GONE else VISIBLE
}
}
diff --git a/wallet/src/main/java/net/taler/wallet/NetworkManager.kt
b/wallet/src/main/java/net/taler/wallet/NetworkManager.kt
index 2f64f4e..7d803fc 100644
--- a/wallet/src/main/java/net/taler/wallet/NetworkManager.kt
+++ b/wallet/src/main/java/net/taler/wallet/NetworkManager.kt
@@ -21,6 +21,8 @@ import android.content.Context.CONNECTIVITY_SERVICE
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
+import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET
+import android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED
import androidx.annotation.UiThread
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
@@ -28,22 +30,26 @@ import androidx.lifecycle.MutableLiveData
class NetworkManager(context: Context) : ConnectivityManager.NetworkCallback()
{
private val connectivityManager: ConnectivityManager
- private val _networkStatus = MutableLiveData<NetworkCapabilities?>(null)
- val networkStatus: LiveData<NetworkCapabilities?> = _networkStatus
+ private val _networkStatus = MutableLiveData(true)
+ val networkStatus: LiveData<Boolean> = _networkStatus
init {
connectivityManager = context.getSystemService(CONNECTIVITY_SERVICE)
as ConnectivityManager
- connectivityManager.registerDefaultNetworkCallback( this)
+ connectivityManager.registerDefaultNetworkCallback(this)
}
@UiThread
override fun onCapabilitiesChanged(network: Network, networkCapabilities:
NetworkCapabilities) {
super.onCapabilitiesChanged(network, networkCapabilities)
- _networkStatus.postValue(networkCapabilities)
+ _networkStatus.postValue(networkCapabilities.isOnline())
}
override fun onLost(network: Network) {
super.onLost(network)
_networkStatus.postValue(null)
}
-}
\ No newline at end of file
+
+ private fun NetworkCapabilities.isOnline(): Boolean {
+ return hasCapability(NET_CAPABILITY_INTERNET) &&
hasCapability(NET_CAPABILITY_VALIDATED)
+ }
+}
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.