Skip to content

Instantly share code, notes, and snippets.

@jamesonwilliams
Created November 24, 2021 00:49
Show Gist options
  • Save jamesonwilliams/49abb9fc7115f54f46ee0e88bb47c71d to your computer and use it in GitHub Desktop.
Save jamesonwilliams/49abb9fc7115f54f46ee0e88bb47c71d to your computer and use it in GitHub Desktop.
fun ConnectivityManager.observeAdapterConnectivity(): Flow<Boolean> {
return callbackFlow {
val callback = object: ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
trySendBlocking(true)
}
override fun onLost(network: Network) {
trySendBlocking(false)
}
}
registerDefaultNetworkCallback(callback)
awaitClose {
unregisterNetworkCallback(callback)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment