From d1decb39baa066fd7c3090d99a440d3f5a741e4a Mon Sep 17 00:00:00 2001 From: davidoskky Date: Sun, 21 Aug 2022 19:08:01 +0200 Subject: [PATCH 1/2] Store the connectivity manager as class variable --- .../github/ln_12/library/ConnectivityStatus.kt | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/androidMain/kotlin/com/github/ln_12/library/ConnectivityStatus.kt b/src/androidMain/kotlin/com/github/ln_12/library/ConnectivityStatus.kt index ea0ae6e..81efe08 100644 --- a/src/androidMain/kotlin/com/github/ln_12/library/ConnectivityStatus.kt +++ b/src/androidMain/kotlin/com/github/ln_12/library/ConnectivityStatus.kt @@ -16,7 +16,7 @@ import kotlinx.coroutines.withContext actual class ConnectivityStatus(private val context: Context) { actual val isNetworkConnected = MutableStateFlow(false) - private val connectivityManager: ConnectivityManager? = null + private var connectivityManager: ConnectivityManager? = null private val networkCallback = object : ConnectivityManager.NetworkCallback() { override fun onAvailable(network: Network) { Log.d("Connectivity status", "Network available") @@ -49,13 +49,14 @@ actual class ConnectivityStatus(private val context: Context) { actual fun start() { try { - val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager - + if (connectivityManager == null) { + connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // API 24 and above - connectivityManager.registerDefaultNetworkCallback(networkCallback) + connectivityManager!!.registerDefaultNetworkCallback(networkCallback) - val currentNetwork = connectivityManager.activeNetwork + val currentNetwork = connectivityManager!!.activeNetwork if(currentNetwork == null) { isNetworkConnected.value = false @@ -72,10 +73,10 @@ actual class ConnectivityStatus(private val context: Context) { } }.build() - connectivityManager.registerNetworkCallback(networkRequest, networkCallback) + connectivityManager!!.registerNetworkCallback(networkRequest, networkCallback) @Suppress("DEPRECATION") - val currentNetwork = connectivityManager.activeNetworkInfo + val currentNetwork = connectivityManager!!.activeNetworkInfo @Suppress("DEPRECATION") if(currentNetwork == null || ( From 2e76c0601041a5f2c90883b18ef1fa23afb525cd Mon Sep 17 00:00:00 2001 From: davidoskky Date: Mon, 22 Aug 2022 14:22:55 +0200 Subject: [PATCH 2/2] Use safe calls rather than non null assertions --- .../kotlin/com/github/ln_12/library/ConnectivityStatus.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/androidMain/kotlin/com/github/ln_12/library/ConnectivityStatus.kt b/src/androidMain/kotlin/com/github/ln_12/library/ConnectivityStatus.kt index 81efe08..8c3b2ce 100644 --- a/src/androidMain/kotlin/com/github/ln_12/library/ConnectivityStatus.kt +++ b/src/androidMain/kotlin/com/github/ln_12/library/ConnectivityStatus.kt @@ -54,9 +54,9 @@ actual class ConnectivityStatus(private val context: Context) { } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // API 24 and above - connectivityManager!!.registerDefaultNetworkCallback(networkCallback) + connectivityManager?.registerDefaultNetworkCallback(networkCallback) - val currentNetwork = connectivityManager!!.activeNetwork + val currentNetwork = connectivityManager?.activeNetwork if(currentNetwork == null) { isNetworkConnected.value = false @@ -73,10 +73,10 @@ actual class ConnectivityStatus(private val context: Context) { } }.build() - connectivityManager!!.registerNetworkCallback(networkRequest, networkCallback) + connectivityManager?.registerNetworkCallback(networkRequest, networkCallback) @Suppress("DEPRECATION") - val currentNetwork = connectivityManager!!.activeNetworkInfo + val currentNetwork = connectivityManager?.activeNetworkInfo @Suppress("DEPRECATION") if(currentNetwork == null || (