From de6545f5d7822cfa9ac3bc2263f51498863f80ca Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Mon, 8 May 2023 10:22:13 +0200 Subject: [PATCH] Simplify handling of nullability in `ConnectivityManagerNetworkMonitor` --- .../util/ConnectivityManagerNetworkMonitor.kt | 82 +++++++++---------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/util/ConnectivityManagerNetworkMonitor.kt b/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/util/ConnectivityManagerNetworkMonitor.kt index 982f47c6e..001b6d4c9 100644 --- a/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/util/ConnectivityManagerNetworkMonitor.kt +++ b/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/util/ConnectivityManagerNetworkMonitor.kt @@ -30,61 +30,61 @@ import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.conflate +import Kotlinx.coroutines.flow.flowOf import javax.inject.Inject class ConnectivityManagerNetworkMonitor @Inject constructor( @ApplicationContext private val context: Context, ) : NetworkMonitor { - override val isOnline: Flow = callbackFlow { + override val isOnline: Flow { val connectivityManager = context.getSystemService() + ?: return flowOf(false) - /** - * The callback's methods are invoked on changes to *any* network, not just the active - * network. So to check for network connectivity, one must query the active network of the - * ConnectivityManager. - */ - val callback = object : NetworkCallback() { - override fun onAvailable(network: Network) { - channel.trySend(connectivityManager.isCurrentlyConnected()) - } + return callbackFlow { - override fun onLost(network: Network) { - channel.trySend(connectivityManager.isCurrentlyConnected()) - } + /** + * The callback's methods are invoked on changes to *any* network, not just the active + * network. So to check for network connectivity, one must query the active network of the + * ConnectivityManager. + */ + val callback = object : NetworkCallback() { + override fun onAvailable(network: Network) { + channel.trySend(connectivityManager.isCurrentlyConnected()) + } + + override fun onLost(network: Network) { + channel.trySend(connectivityManager.isCurrentlyConnected()) + } - override fun onCapabilitiesChanged( - network: Network, - networkCapabilities: NetworkCapabilities, - ) { - channel.trySend(connectivityManager.isCurrentlyConnected()) + override fun onCapabilitiesChanged( + network: Network, + networkCapabilities: NetworkCapabilities, + ) { + channel.trySend(connectivityManager.isCurrentlyConnected()) + } } - } - connectivityManager?.registerNetworkCallback( - Builder() - .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) - .build(), - callback, - ) + connectivityManager.registerNetworkCallback( + Builder() + .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) + .build(), + callback, + ) - channel.trySend(connectivityManager.isCurrentlyConnected()) + channel.trySend(connectivityManager.isCurrentlyConnected()) - awaitClose { - connectivityManager?.unregisterNetworkCallback(callback) + awaitClose { + connectivityManager.unregisterNetworkCallback(callback) + } } - } - .conflate() + .conflate() @Suppress("DEPRECATION") - private fun ConnectivityManager?.isCurrentlyConnected() = when (this) { - null -> false - else -> when { - VERSION.SDK_INT >= VERSION_CODES.M -> - activeNetwork - ?.let(::getNetworkCapabilities) - ?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) - ?: false - else -> activeNetworkInfo?.isConnected ?: false - } - } + private fun ConnectivityManager.isCurrentlyConnected() = when { + VERSION.SDK_INT >= VERSION_CODES.M -> + activeNetwork + ?.let(::getNetworkCapabilities) + ?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) + else -> activeNetworkInfo?.isConnected + } ?: false }