From 5740e53adad7392a45590f59ae1f20fb1d1d1e0e Mon Sep 17 00:00:00 2001 From: Deven Joshi Date: Mon, 15 Nov 2021 18:00:45 +0530 Subject: [PATCH] added attachment actions modal builder --- .../lib/src/attachment/giphy_attachment.dart | 4 ++ .../lib/src/attachment/image_attachment.dart | 5 ++ .../lib/src/attachment/video_attachment.dart | 5 ++ .../lib/src/attachment_actions_modal.dart | 26 +++++++++++ .../lib/src/full_screen_media.dart | 23 ++-------- .../lib/src/gallery_header.dart | 46 +++++++++---------- .../lib/src/image_group.dart | 5 ++ 7 files changed, 71 insertions(+), 43 deletions(-) diff --git a/packages/stream_chat_flutter/lib/src/attachment/giphy_attachment.dart b/packages/stream_chat_flutter/lib/src/attachment/giphy_attachment.dart index fc0fb586..215ada51 100644 --- a/packages/stream_chat_flutter/lib/src/attachment/giphy_attachment.dart +++ b/packages/stream_chat_flutter/lib/src/attachment/giphy_attachment.dart @@ -18,6 +18,7 @@ class GiphyAttachment extends AttachmentWidget { this.onShowMessage, this.onReturnAction, this.onAttachmentTap, + this.attachmentActionsModalBuilder, }) : super( key: key, message: message, @@ -34,6 +35,8 @@ class GiphyAttachment extends AttachmentWidget { /// Callback when attachment is tapped final VoidCallback? onAttachmentTap; + final AttachmentActionsBuilder? attachmentActionsModalBuilder; + @override Widget build(BuildContext context) { final imageUrl = @@ -250,6 +253,7 @@ class GiphyAttachment extends AttachmentWidget { userName: message.user?.name, message: message, onShowMessage: onShowMessage, + attachmentActionsModalBuilder: attachmentActionsModalBuilder, ), ); }, diff --git a/packages/stream_chat_flutter/lib/src/attachment/image_attachment.dart b/packages/stream_chat_flutter/lib/src/attachment/image_attachment.dart index 41b8d039..239cf2a2 100644 --- a/packages/stream_chat_flutter/lib/src/attachment/image_attachment.dart +++ b/packages/stream_chat_flutter/lib/src/attachment/image_attachment.dart @@ -21,6 +21,7 @@ class ImageAttachment extends AttachmentWidget { this.onShowMessage, this.onReturnAction, this.onAttachmentTap, + this.attachmentActionsModalBuilder, }) : super( key: key, message: message, @@ -43,6 +44,8 @@ class ImageAttachment extends AttachmentWidget { /// Callback when attachment is tapped final VoidCallback? onAttachmentTap; + final AttachmentActionsBuilder? attachmentActionsModalBuilder; + @override Widget build(BuildContext context) => source.when( local: () { @@ -145,6 +148,8 @@ class ImageAttachment extends AttachmentWidget { userName: message.user?.name, message: message, onShowMessage: onShowMessage, + attachmentActionsModalBuilder: + attachmentActionsModalBuilder, ), ); }, diff --git a/packages/stream_chat_flutter/lib/src/attachment/video_attachment.dart b/packages/stream_chat_flutter/lib/src/attachment/video_attachment.dart index ce4ac804..40a1bff3 100644 --- a/packages/stream_chat_flutter/lib/src/attachment/video_attachment.dart +++ b/packages/stream_chat_flutter/lib/src/attachment/video_attachment.dart @@ -18,6 +18,7 @@ class VideoAttachment extends AttachmentWidget { this.onShowMessage, this.onReturnAction, this.onAttachmentTap, + this.attachmentActionsModalBuilder, }) : super( key: key, message: message, @@ -37,6 +38,8 @@ class VideoAttachment extends AttachmentWidget { /// Callback when attachment is tapped final VoidCallback? onAttachmentTap; + final AttachmentActionsBuilder? attachmentActionsModalBuilder; + @override Widget build(BuildContext context) => source.when( local: () { @@ -91,6 +94,8 @@ class VideoAttachment extends AttachmentWidget { userName: message.user?.name, message: message, onShowMessage: onShowMessage, + attachmentActionsModalBuilder: + attachmentActionsModalBuilder, ), ), ), diff --git a/packages/stream_chat_flutter/lib/src/attachment_actions_modal.dart b/packages/stream_chat_flutter/lib/src/attachment_actions_modal.dart index 96e040fd..89527a48 100644 --- a/packages/stream_chat_flutter/lib/src/attachment_actions_modal.dart +++ b/packages/stream_chat_flutter/lib/src/attachment_actions_modal.dart @@ -57,6 +57,32 @@ class AttachmentActionsModal extends StatelessWidget { /// Show delete option final bool showDelete; + /// Creates a copy of [MessageWidget] with specified attributes overridden. + AttachmentActionsModal copyWith({ + Key? key, + int? currentIndex, + Message? message, + VoidCallback? onShowMessage, + AttachmentDownloader? imageDownloader, + AttachmentDownloader? fileDownloader, + bool? showReply, + bool? showShowInChat, + bool? showSave, + bool? showDelete, + }) => + AttachmentActionsModal( + key: key ?? this.key, + currentIndex: currentIndex ?? this.currentIndex, + message: message ?? this.message, + onShowMessage: onShowMessage ?? this.onShowMessage, + imageDownloader: imageDownloader ?? this.imageDownloader, + fileDownloader: fileDownloader ?? this.fileDownloader, + showReply: showReply ?? this.showReply, + showShowInChat: showShowInChat ?? this.showShowInChat, + showSave: showSave ?? this.showSave, + showDelete: showDelete ?? this.showDelete, + ); + @override Widget build(BuildContext context) => GestureDetector( behavior: HitTestBehavior.translucent, diff --git a/packages/stream_chat_flutter/lib/src/full_screen_media.dart b/packages/stream_chat_flutter/lib/src/full_screen_media.dart index 6fe2b2a4..566f8f3f 100644 --- a/packages/stream_chat_flutter/lib/src/full_screen_media.dart +++ b/packages/stream_chat_flutter/lib/src/full_screen_media.dart @@ -33,10 +33,7 @@ class FullScreenMedia extends StatefulWidget { this.startIndex = 0, String? userName, this.onShowMessage, - this.showReply = true, - this.showShowInChat = true, - this.showSave = true, - this.showDelete = true, + this.attachmentActionsModalBuilder, }) : userName = userName ?? '', super(key: key); @@ -55,17 +52,7 @@ class FullScreenMedia extends StatefulWidget { /// Callback for when show message is tapped final ShowMessageCallback? onShowMessage; - /// Show reply option - final bool showReply; - - /// Show show in chat option - final bool showShowInChat; - - /// Show save option - final bool showSave; - - /// Show delete option - final bool showDelete; + final AttachmentActionsBuilder? attachmentActionsModalBuilder; @override _FullScreenMediaState createState() => _FullScreenMediaState(); @@ -212,10 +199,8 @@ class _FullScreenMediaState extends State StreamChannel.of(context).channel, ); }, - showReply: widget.showReply, - showShowInChat: widget.showShowInChat, - showSave: widget.showSave, - showDelete: widget.showDelete, + attachmentActionsModalBuilder: + widget.attachmentActionsModalBuilder, ), if (!widget.message.isEphemeral) GalleryFooter( diff --git a/packages/stream_chat_flutter/lib/src/gallery_header.dart b/packages/stream_chat_flutter/lib/src/gallery_header.dart index 638375eb..bc19979e 100644 --- a/packages/stream_chat_flutter/lib/src/gallery_header.dart +++ b/packages/stream_chat_flutter/lib/src/gallery_header.dart @@ -6,6 +6,12 @@ import 'package:stream_chat_flutter/src/stream_svg_icon.dart'; import 'package:stream_chat_flutter/src/theme/themes.dart'; import 'package:stream_chat_flutter_core/stream_chat_flutter_core.dart'; +typedef AttachmentActionsBuilder = Widget Function( + BuildContext context, + Attachment attachment, + AttachmentActionsModal defaultActionsModal, +); + /// Header/AppBar widget for media display screen class GalleryHeader extends StatelessWidget implements PreferredSizeWidget { /// Creates a channel header @@ -21,10 +27,7 @@ class GalleryHeader extends StatelessWidget implements PreferredSizeWidget { this.userName = '', this.sentAt = '', this.backgroundColor, - this.showReply = true, - this.showShowInChat = true, - this.showSave = true, - this.showDelete = true, + this.attachmentActionsModalBuilder, }) : preferredSize = const Size.fromHeight(kToolbarHeight), super(key: key); @@ -59,17 +62,7 @@ class GalleryHeader extends StatelessWidget implements PreferredSizeWidget { /// The background color of this [GalleryHeader]. final Color? backgroundColor; - /// Show reply option - final bool showReply; - - /// Show show in chat option - final bool showShowInChat; - - /// Show save option - final bool showSave; - - /// Show delete option - final bool showDelete; + final AttachmentActionsBuilder? attachmentActionsModalBuilder; @override Widget build(BuildContext context) { @@ -139,21 +132,26 @@ class GalleryHeader extends StatelessWidget implements PreferredSizeWidget { final galleryHeaderThemeData = StreamChatTheme.of(context).galleryHeaderTheme; + final defaultModal = AttachmentActionsModal( + message: message, + currentIndex: currentIndex, + onShowMessage: onShowMessage, + ); + + final effectiveModal = attachmentActionsModalBuilder?.call( + context, + message.attachments[currentIndex], + defaultModal, + ) ?? + defaultModal; + final result = await showDialog( useRootNavigator: false, context: context, barrierColor: galleryHeaderThemeData.bottomSheetBarrierColor, builder: (context) => StreamChannel( channel: channel, - child: AttachmentActionsModal( - message: message, - currentIndex: currentIndex, - onShowMessage: onShowMessage, - showReply: showReply, - showShowInChat: showShowInChat, - showSave: showSave, - showDelete: showDelete, - ), + child: effectiveModal, ), ); diff --git a/packages/stream_chat_flutter/lib/src/image_group.dart b/packages/stream_chat_flutter/lib/src/image_group.dart index 88b309fe..04fe8032 100644 --- a/packages/stream_chat_flutter/lib/src/image_group.dart +++ b/packages/stream_chat_flutter/lib/src/image_group.dart @@ -16,6 +16,7 @@ class ImageGroup extends StatelessWidget { this.onReturnAction, this.onShowMessage, this.onAttachmentTap, + this.attachmentActionsModalBuilder, }) : super(key: key); /// List of attachments to show @@ -39,6 +40,8 @@ class ImageGroup extends StatelessWidget { /// Callback for when show message is tapped final ShowMessageCallback? onShowMessage; + final AttachmentActionsBuilder? attachmentActionsModalBuilder; + @override Widget build(BuildContext context) => ConstrainedBox( constraints: BoxConstraints.loose(size), @@ -138,6 +141,7 @@ class ImageGroup extends StatelessWidget { userName: message.user?.name, message: message, onShowMessage: onShowMessage, + attachmentActionsModalBuilder: attachmentActionsModalBuilder, ), ), ), @@ -151,5 +155,6 @@ class ImageGroup extends StatelessWidget { message: message, messageTheme: messageTheme, onAttachmentTap: () => _onTap(context, index), + attachmentActionsModalBuilder: attachmentActionsModalBuilder, ); }