aboutsummaryrefslogtreecommitdiff
path: root/shared/src/androidMain
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2023-09-17 21:56:55 -0600
committerIván Ávalos <avalos@disroot.org>2023-09-17 23:51:33 -0600
commitedbd2c7713a0ba4e7e7a3ba6d59d16861ea4eb23 (patch)
tree885ca095c993c7a661303d215d9be0a6271ba3ea /shared/src/androidMain
parent7aec305729b872d668df45eae4821b106c1a20cb (diff)
downloadetbsa-trackermap-mobile-edbd2c7713a0ba4e7e7a3ba6d59d16861ea4eb23.tar.gz
etbsa-trackermap-mobile-edbd2c7713a0ba4e7e7a3ba6d59d16861ea4eb23.tar.bz2
etbsa-trackermap-mobile-edbd2c7713a0ba4e7e7a3ba6d59d16861ea4eb23.zip
- [shared] Implement network state monitoring
- [android] UI reacts to network state - [ios] UI reacts to network state
Diffstat (limited to 'shared/src/androidMain')
-rw-r--r--shared/src/androidMain/AndroidManifest.xml4
-rw-r--r--shared/src/androidMain/kotlin/mx/trackermap/TrackerMap/controllers/NetworkController.kt55
2 files changed, 58 insertions, 1 deletions
diff --git a/shared/src/androidMain/AndroidManifest.xml b/shared/src/androidMain/AndroidManifest.xml
index 568741e..f0f34af 100644
--- a/shared/src/androidMain/AndroidManifest.xml
+++ b/shared/src/androidMain/AndroidManifest.xml
@@ -1,2 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
-<manifest /> \ No newline at end of file
+<manifest xmlns:android="http://schemas.android.com/apk/res/android">
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+</manifest> \ No newline at end of file
diff --git a/shared/src/androidMain/kotlin/mx/trackermap/TrackerMap/controllers/NetworkController.kt b/shared/src/androidMain/kotlin/mx/trackermap/TrackerMap/controllers/NetworkController.kt
new file mode 100644
index 0000000..eecd7de
--- /dev/null
+++ b/shared/src/androidMain/kotlin/mx/trackermap/TrackerMap/controllers/NetworkController.kt
@@ -0,0 +1,55 @@
+package mx.trackermap.TrackerMap.controllers
+
+import android.content.Context
+import android.net.ConnectivityManager
+import android.net.Network
+import android.net.NetworkCapabilities
+import android.net.NetworkRequest
+import android.os.Build
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.asStateFlow
+import mx.trackermap.TrackerMap.Injectable
+
+actual class NetworkController(context: Context): Injectable {
+ private val networkRequest = NetworkRequest.Builder().build()
+ private val connectivityManager: ConnectivityManager
+ private val _networkAvailable = MutableStateFlow<Boolean?>(null)
+ actual val networkAvailable = _networkAvailable.asStateFlow()
+
+ private val networkCallback = object: ConnectivityManager.NetworkCallback() {
+ override fun onCapabilitiesChanged(
+ network: Network,
+ networkCapabilities: NetworkCapabilities
+ ) {
+ super.onCapabilitiesChanged(network, networkCapabilities)
+ _networkAvailable.value = checkNetworkAccess(networkCapabilities)
+ }
+
+ override fun onLost(network: Network) {
+ super.onLost(network)
+ _networkAvailable.value = false
+ }
+ }
+
+ init {
+ connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
+ _networkAvailable.value = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ checkNetworkAccess(connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork))
+ } else {
+ connectivityManager.activeNetworkInfo?.isConnected == true
+ }
+
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ connectivityManager.registerDefaultNetworkCallback(networkCallback)
+ } else {
+ connectivityManager.registerNetworkCallback(networkRequest, networkCallback)
+ }
+ }
+
+ private fun checkNetworkAccess(capabilities: NetworkCapabilities?) =
+ capabilities != null
+ && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
+ && if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
+ } else true
+} \ No newline at end of file