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 001b6d4c9..a21965565 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 @@ -36,48 +36,50 @@ import javax.inject.Inject class ConnectivityManagerNetworkMonitor @Inject constructor( @ApplicationContext private val context: Context, ) : NetworkMonitor { - override val isOnline: Flow { + override val isOnline: Flow = callbackFlow { val connectivityManager = context.getSystemService() - ?: return flowOf(false) - - return callbackFlow { + ?: run { + channel.trySend(false) + channel.close() + return@callbackFlow + } - /** - * 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()) - } + /** + * 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 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 {