Skip to content

Commit

Permalink
[AND-205] Improve network connectivity check in the NetworkStateProvi…
Browse files Browse the repository at this point in the history
…der. (#5538)

* [AND-205] Add delay before checking network state to ensure the connectivity manager is updated.

* [AND-205] Update CHANGELOG.md.

* [AND-205] Remove delay in network check and notify listeners about disconnect when there are no available networks.

---------

Co-authored-by: PetarVelikov <[email protected]>
  • Loading branch information
VelikovPetar and PetarVelikov authored Jan 3, 2025
1 parent f91c328 commit c87f2c0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
### 🐞 Fixed
- Fix `ChatClient::queryBlockedMembers` not working. [#5532](https://github.com/GetStream/stream-chat-android/pull/5532)
- Fix `ChatClient::blockUser` and `ChatClient::unblockUser` response parsing. [#5532](https://github.com/GetStream/stream-chat-android/pull/5532)
- Fix rare bug where the `ConnectivityState` is not updated to `Offline` after disconnecting the device from network. [#5538](https://github.com/GetStream/stream-chat-android/pull/5538)

### ⬆️ Improved

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ internal class NetworkStateProvider(
private val scope: CoroutineScope,
private val connectivityManager: ConnectivityManager,
) {

private val logger by taggedLogger("Chat:NetworkStateProvider")
private val lock: Any = Any()

private val availableNetworks: MutableSet<Network> = mutableSetOf()

private val callback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
availableNetworks.add(network)
notifyListenersIfNetworkStateChanged()
}

Expand All @@ -43,7 +46,12 @@ internal class NetworkStateProvider(
}

override fun onLost(network: Network) {
availableNetworks.remove(network)
notifyListenersIfNetworkStateChanged()
if (availableNetworks.isEmpty()) {
// No available networks, notify listeners about disconnection
listeners.onDisconnected()
}
}
}

Expand Down

0 comments on commit c87f2c0

Please sign in to comment.