Created
November 24, 2021 00:49
-
-
Save jamesonwilliams/49abb9fc7115f54f46ee0e88bb47c71d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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