diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index 61f00005..76b65833 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -14,6 +14,10 @@ - Added `Filter.contains` and `Filter.empty` +🐞 Fixed + +- [[#659]](https://github.com/GetStream/stream-chat-flutter/issues/659) Fixed unread count not updating correctly. + ## 2.2.1 🐞 Fixed diff --git a/packages/stream_chat/lib/src/client/client.dart b/packages/stream_chat/lib/src/client/client.dart index f9d10a92..9253b623 100644 --- a/packages/stream_chat/lib/src/client/client.dart +++ b/packages/stream_chat/lib/src/client/client.dart @@ -1366,16 +1366,7 @@ class ClientState { .where((event) => event.me != null && event.type != EventType.healthCheck) .map((e) => e.me!) - .listen((user) { - currentUser = currentUser?.merge(user) ?? user; - final totalUnreadCount = user.totalUnreadCount; - _totalUnreadCountController.add(totalUnreadCount); - - final unreadChannels = user.unreadChannels; - if (unreadChannels != null) { - _unreadChannelsController.add(unreadChannels); - } - }), + .listen((user) => currentUser = currentUser?.merge(user) ?? user), _client .on() .map((event) => event.unreadChannels) @@ -1441,6 +1432,7 @@ class ClientState { /// Sets the user currently interacting with the client /// note: this fully overrides the [currentUser] set currentUser(OwnUser? user) { + _computeUnreadCounts(user); _currentUserController.add(user); } @@ -1506,6 +1498,18 @@ class ClientState { _channelsController.add(newChannels); } + void _computeUnreadCounts(OwnUser? user) { + final totalUnreadCount = user?.totalUnreadCount; + if (totalUnreadCount != null) { + _totalUnreadCountController.add(totalUnreadCount); + } + + final unreadChannels = user?.unreadChannels; + if (unreadChannels != null) { + _unreadChannelsController.add(unreadChannels); + } + } + final _channelsController = BehaviorSubject>.seeded({}); final _currentUserController = BehaviorSubject(); final _usersController = BehaviorSubject>.seeded({}); diff --git a/packages/stream_chat/lib/src/core/models/own_user.dart b/packages/stream_chat/lib/src/core/models/own_user.dart index a65052f4..07c6cf30 100644 --- a/packages/stream_chat/lib/src/core/models/own_user.dart +++ b/packages/stream_chat/lib/src/core/models/own_user.dart @@ -17,7 +17,7 @@ class OwnUser extends User { this.devices = const [], this.mutes = const [], this.totalUnreadCount = 0, - this.unreadChannels, + this.unreadChannels = 0, this.channelMutes = const [], required String id, String? role, @@ -151,8 +151,8 @@ class OwnUser extends User { final int totalUnreadCount; /// Total unread channels by the user. - @JsonKey(includeIfNull: false) - final int? unreadChannels; + @JsonKey(includeIfNull: false, defaultValue: 0) + final int unreadChannels; /// Known top level fields. /// diff --git a/packages/stream_chat/lib/src/core/models/own_user.g.dart b/packages/stream_chat/lib/src/core/models/own_user.g.dart index ca4acdea..185b6dd0 100644 --- a/packages/stream_chat/lib/src/core/models/own_user.g.dart +++ b/packages/stream_chat/lib/src/core/models/own_user.g.dart @@ -17,7 +17,7 @@ OwnUser _$OwnUserFromJson(Map json) { .toList() ?? [], totalUnreadCount: json['total_unread_count'] as int? ?? 0, - unreadChannels: json['unread_channels'] as int?, + unreadChannels: json['unread_channels'] as int? ?? 0, channelMutes: (json['channel_mutes'] as List?) ?.map((e) => Mute.fromJson(e as Map)) .toList() ?? diff --git a/packages/stream_chat/test/src/client/client_test.dart b/packages/stream_chat/test/src/client/client_test.dart index a1a3fc96..b59faa38 100644 --- a/packages/stream_chat/test/src/client/client_test.dart +++ b/packages/stream_chat/test/src/client/client_test.dart @@ -2313,5 +2313,27 @@ void main() { )).called(1); verifyNoMoreInteractions(api.message); }); + + test( + '''setting the `currentUser` should also compute and update the unreadCounts''', + () { + final state = client.state; + final initialUser = OwnUser.fromUser(user); + + expect(state.currentUser, initialUser); + expect(state.totalUnreadCount, 0); + expect(state.unreadChannels, 0); + + final updateUser = initialUser.copyWith( + totalUnreadCount: 33, + unreadChannels: 33, + ); + state.currentUser = updateUser; + + expect(state.currentUser, updateUser); + expect(state.totalUnreadCount, 33); + expect(state.unreadChannels, 33); + }, + ); }); }