fix(llc): Fix unread count not updating when the current user is set.
Signed-off-by: xsahil03x <xdsahil@gmail.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<Map<String, Channel>>.seeded({});
|
||||
final _currentUserController = BehaviorSubject<OwnUser?>();
|
||||
final _usersController = BehaviorSubject<Map<String, User>>.seeded({});
|
||||
|
||||
@@ -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.
|
||||
///
|
||||
|
||||
@@ -17,7 +17,7 @@ OwnUser _$OwnUserFromJson(Map<String, dynamic> 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<dynamic>?)
|
||||
?.map((e) => Mute.fromJson(e as Map<String, dynamic>))
|
||||
.toList() ??
|
||||
|
||||
@@ -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);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user