feat: Add support for messages filter in MessageListView and MessageListCore (#303)

* [MessageListView, MessageListCore] Add support for message filter

Signed-off-by: Sahil Kumar <[email protected]>

* [UI-Kit -> Pubspec] Remove `stream_chat_flutter_core` relative import

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-03-02 16:27:58 +01:00
committed by GitHub
parent a14e54591d
commit c5a9a42c45
2 changed files with 20 additions and 6 deletions
@@ -135,6 +135,7 @@ class MessageListView extends StatefulWidget {
this.systemMessageBuilder, this.systemMessageBuilder,
this.messageListBuilder, this.messageListBuilder,
this.errorWidgetBuilder, this.errorWidgetBuilder,
this.messageFilter,
this.customAttachmentBuilders, this.customAttachmentBuilders,
this.onMessageTap, this.onMessageTap,
this.onSystemMessageTap, this.onSystemMessageTap,
@@ -214,6 +215,9 @@ class MessageListView extends StatefulWidget {
/// of a connection failure. /// of a connection failure.
final ErrorBuilder errorWidgetBuilder; final ErrorBuilder errorWidgetBuilder;
/// Predicate used to filter messages
final bool Function(Message) messageFilter;
/// Attachment builders for the default message widget /// Attachment builders for the default message widget
/// Please change this in the [MessageWidget] if you are using a custom implementation /// Please change this in the [MessageWidget] if you are using a custom implementation
final Map<String, AttachmentBuilder> customAttachmentBuilders; final Map<String, AttachmentBuilder> customAttachmentBuilders;
@@ -281,6 +285,7 @@ class _MessageListViewState extends State<MessageListView> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MessageListCore( return MessageListCore(
messageFilter: widget.messageFilter,
loadingBuilder: widget.loadingBuilder ?? loadingBuilder: widget.loadingBuilder ??
(context) { (context) {
return Center( return Center(
@@ -64,6 +64,7 @@ class MessageListCore extends StatefulWidget {
this.showScrollToBottom = true, this.showScrollToBottom = true,
this.parentMessage, this.parentMessage,
this.messageListController, this.messageListController,
this.messageFilter,
}) : assert(loadingBuilder != null), }) : assert(loadingBuilder != null),
assert(emptyBuilder != null), assert(emptyBuilder != null),
assert(messageListBuilder != null), assert(messageListBuilder != null),
@@ -95,6 +96,9 @@ class MessageListCore extends StatefulWidget {
/// first message or the parent of the conversation. /// first message or the parent of the conversation.
final Message parentMessage; final Message parentMessage;
/// Predicate used to filter messages
final bool Function(Message) messageFilter;
@override @override
_MessageListCoreState createState() => _MessageListCoreState(); _MessageListCoreState createState() => _MessageListCoreState();
} }
@@ -106,6 +110,8 @@ class _MessageListCoreState extends State<MessageListCore> {
bool get _isThreadConversation => widget.parentMessage != null; bool get _isThreadConversation => widget.parentMessage != null;
OwnUser get _currentUser => streamChannel.channel.client.state.user;
int initialIndex; int initialIndex;
double initialAlignment; double initialAlignment;
@@ -121,13 +127,16 @@ class _MessageListCoreState extends State<MessageListCore> {
.map((threads) => threads[widget.parentMessage.id]) .map((threads) => threads[widget.parentMessage.id])
: streamChannel.channel.state?.messagesStream; : streamChannel.channel.state?.messagesStream;
bool defaultFilter(Message m) {
final isMyMessage = m.user.id == _currentUser.id;
final isDeletedOrShadowed = m.isDeleted || m.shadowed;
if (isDeletedOrShadowed && !isMyMessage) return false;
return true;
}
return StreamBuilder<List<Message>>( return StreamBuilder<List<Message>>(
stream: messagesStream?.map((messages) => messages stream: messagesStream?.map((messages) =>
?.where((e) => messages?.where(widget.messageFilter ?? defaultFilter)?.toList()),
(!e.isDeleted && e.shadowed != true) ||
(e.isDeleted &&
e.user.id == streamChannel.channel.client.state.user.id))
?.toList()),
builder: (context, snapshot) { builder: (context, snapshot) {
if (!snapshot.hasData) { if (!snapshot.hasData) {
return widget.loadingBuilder(context); return widget.loadingBuilder(context);