Simplify handling of nullability in `ConnectivityManagerNetworkMonitor`

pull/716/head
Simon Marquis 2 years ago committed by GitHub
parent 553f55f978
commit de6545f5d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -30,61 +30,61 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.flow.conflate
import Kotlinx.coroutines.flow.flowOf
import javax.inject.Inject import javax.inject.Inject
class ConnectivityManagerNetworkMonitor @Inject constructor( class ConnectivityManagerNetworkMonitor @Inject constructor(
@ApplicationContext private val context: Context, @ApplicationContext private val context: Context,
) : NetworkMonitor { ) : NetworkMonitor {
override val isOnline: Flow<Boolean> = callbackFlow { override val isOnline: Flow<Boolean> {
val connectivityManager = context.getSystemService<ConnectivityManager>() val connectivityManager = context.getSystemService<ConnectivityManager>()
?: return flowOf(false)
/** 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())
}
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( override fun onCapabilitiesChanged(
network: Network, network: Network,
networkCapabilities: NetworkCapabilities, networkCapabilities: NetworkCapabilities,
) { ) {
channel.trySend(connectivityManager.isCurrentlyConnected()) channel.trySend(connectivityManager.isCurrentlyConnected())
}
} }
}
connectivityManager?.registerNetworkCallback( connectivityManager.registerNetworkCallback(
Builder() Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.build(), .build(),
callback, callback,
) )
channel.trySend(connectivityManager.isCurrentlyConnected()) channel.trySend(connectivityManager.isCurrentlyConnected())
awaitClose { awaitClose {
connectivityManager?.unregisterNetworkCallback(callback) connectivityManager.unregisterNetworkCallback(callback)
}
} }
} .conflate()
.conflate()
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
private fun ConnectivityManager?.isCurrentlyConnected() = when (this) { private fun ConnectivityManager.isCurrentlyConnected() = when {
null -> false VERSION.SDK_INT >= VERSION_CODES.M ->
else -> when { activeNetwork
VERSION.SDK_INT >= VERSION_CODES.M -> ?.let(::getNetworkCapabilities)
activeNetwork ?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
?.let(::getNetworkCapabilities) else -> activeNetworkInfo?.isConnected
?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) } ?: false
?: false
else -> activeNetworkInfo?.isConnected ?: false
}
}
} }

Loading…
Cancel
Save