File tree Expand file tree Collapse file tree 1 file changed +17
-5
lines changed
core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/util Expand file tree Collapse file tree 1 file changed +17
-5
lines changed Original file line number Diff line number Diff line change @@ -35,18 +35,30 @@ import kotlinx.coroutines.flow.conflate
3535class ConnectivityManagerNetworkMonitor @Inject constructor(
3636 @ApplicationContext private val context : Context
3737) : NetworkMonitor {
38- override val isOnline: Flow <Boolean > = callbackFlow<Boolean > {
38+ override val isOnline: Flow <Boolean > = callbackFlow {
39+ val connectivityManager = context.getSystemService<ConnectivityManager >()
40+
41+ /*
42+ The callback's methods are invoked on changes to *any* network, not just the active
43+ network. So to check for network connectivity, one must query the active network of the
44+ ConnectivityManager.
45+ */
3946 val callback = object : NetworkCallback () {
4047 override fun onAvailable (network : Network ) {
41- channel.trySend(true )
48+ channel.trySend(connectivityManager.isCurrentlyConnected() )
4249 }
4350
4451 override fun onLost (network : Network ) {
45- channel.trySend(false )
52+ channel.trySend(connectivityManager.isCurrentlyConnected() )
4653 }
47- }
4854
49- val connectivityManager = context.getSystemService<ConnectivityManager >()
55+ override fun onCapabilitiesChanged (
56+ network : Network ,
57+ networkCapabilities : NetworkCapabilities
58+ ) {
59+ channel.trySend(connectivityManager.isCurrentlyConnected())
60+ }
61+ }
5062
5163 connectivityManager?.registerNetworkCallback(
5264 Builder ()
You can’t perform that action at this time.
0 commit comments