fix(core): Channel doesn't auto display again after being hidden.

Signed-off-by: xsahil03x <xdsahil@gmail.com>
This commit is contained in:
Sahil Kumar
2023-05-02 18:52:13 +05:30
committed by xsahil03x
parent 2bd8defe6d
commit 42e1112c50
3 changed files with 32 additions and 8 deletions
@@ -1,3 +1,8 @@
## Upcoming
- [[#1356]](https://github.com/GetStream/stream-chat-flutter/issues/1356) Channel doesn't auto display again after being
hidden.
## 6.0.0
- Updated dependencies to resolvable versions.
@@ -387,12 +387,20 @@ class StreamChannelState extends State<StreamChannel> {
(it) => it.user.id == channel.client.state.currentUser?.id,
);
if (read != null &&
!(channel.state!.messages
.any((it) => it.createdAt.compareTo(read.lastRead) > 0) &&
channel.state!.messages
.any((it) => it.createdAt.compareTo(read.lastRead) <= 0))) {
_futures.add(_loadChannelAtTimestamp(read.lastRead));
if (read == null) return;
final messages = channel.state!.messages;
final lastRead = read.lastRead;
final hasNewMessages =
messages.any((it) => it.createdAt.isAfter(lastRead));
final hasOldMessages =
messages.any((it) => it.createdAt.isBeforeOrEqualTo(lastRead));
// Only load messages if the unread message is in-between the messages.
// Otherwise, we can just load the channel normally.
if (hasNewMessages && hasOldMessages) {
_futures.add(_loadChannelAtTimestamp(lastRead));
}
}
}
@@ -449,3 +457,9 @@ class StreamChannelState extends State<StreamChannel> {
return child;
}
}
extension on DateTime {
bool isBeforeOrEqualTo(DateTime other) {
return isBefore(other) || isAtSameMomentAs(other);
}
}
@@ -101,14 +101,19 @@ class StreamChannelListEventHandler {
/// we are currently watching.
///
/// By default, this moves the channel to the top of the list.
void onMessageNew(Event event, StreamChannelListController controller) {
void onMessageNew(Event event, StreamChannelListController controller) async {
final channelCid = event.cid;
if (channelCid == null) return;
final channels = [...controller.currentItems];
final channelIndex = channels.indexWhere((it) => it.cid == channelCid);
if (channelIndex <= 0) return;
if (channelIndex <= 0) {
// If the channel is not in the list, It might be hidden.
// So, we just refresh the list.
await controller.refresh(resetValue: false);
return;
}
final channel = channels.removeAt(channelIndex);
channels.insert(0, channel);