From 5ea185567ada08f3d9852677d0b9fa057cb05b46 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Fri, 11 Aug 2023 17:58:48 +0530 Subject: [PATCH] feat(ui): Add support for listening errors in AttachmentPicker. Signed-off-by: Sahil Kumar --- .../stream_attachment_picker.dart | 69 ++++++++++++++----- ...stream_attachment_picker_bottom_sheet.dart | 3 + .../message_input/stream_message_input.dart | 1 + 3 files changed, 56 insertions(+), 17 deletions(-) diff --git a/packages/stream_chat_flutter/lib/src/message_input/attachment_picker/stream_attachment_picker.dart b/packages/stream_chat_flutter/lib/src/message_input/attachment_picker/stream_attachment_picker.dart index 6c8f1f59..94a81412 100644 --- a/packages/stream_chat_flutter/lib/src/message_input/attachment_picker/stream_attachment_picker.dart +++ b/packages/stream_chat_flutter/lib/src/message_input/attachment_picker/stream_attachment_picker.dart @@ -698,6 +698,7 @@ Widget mobileAttachmentPickerBuilder({ ThumbnailFormat attachmentThumbnailFormat = ThumbnailFormat.jpeg, int attachmentThumbnailQuality = 100, double attachmentThumbnailScale = 1, + ErrorListener? onError, }) { return StreamMobileAttachmentPickerBottomSheet( controller: controller, @@ -721,10 +722,15 @@ Widget mobileAttachmentPickerBuilder({ mediaThumbnailQuality: attachmentThumbnailQuality, mediaThumbnailScale: attachmentThumbnailScale, onMediaItemSelected: (media) async { - if (selectedIds.contains(media.id)) { - return controller.removeAssetAttachment(media); + try { + if (selectedIds.contains(media.id)) { + return await controller.removeAssetAttachment(media); + } + return await controller.addAssetAttachment(media); + } catch (e, stk) { + if (onError != null) return onError.call(e, stk); + rethrow; } - return controller.addAssetAttachment(media); }, ); }, @@ -736,8 +742,15 @@ Widget mobileAttachmentPickerBuilder({ optionViewBuilder: (context, controller) { return StreamFilePicker( onFilePicked: (file) async { - if (file != null) await controller.addAttachment(file); - return Navigator.pop(context, controller.value); + try { + if (file != null) await controller.addAttachment(file); + return Navigator.pop(context, controller.value); + } catch (e, stk) { + Navigator.pop(context, controller.value); + if (onError != null) return onError.call(e, stk); + + rethrow; + } }, ); }, @@ -749,10 +762,17 @@ Widget mobileAttachmentPickerBuilder({ optionViewBuilder: (context, controller) { return StreamImagePicker( onImagePicked: (image) async { - if (image != null) { - await controller.addAttachment(image); + try { + if (image != null) { + await controller.addAttachment(image); + } + return Navigator.pop(context, controller.value); + } catch (e, stk) { + Navigator.pop(context, controller.value); + if (onError != null) return onError.call(e, stk); + + rethrow; } - return Navigator.pop(context, controller.value); }, ); }, @@ -764,10 +784,17 @@ Widget mobileAttachmentPickerBuilder({ optionViewBuilder: (context, controller) { return StreamVideoPicker( onVideoPicked: (video) async { - if (video != null) { - await controller.addAttachment(video); + try { + if (video != null) { + await controller.addAttachment(video); + } + return Navigator.pop(context, controller.value); + } catch (e, stk) { + Navigator.pop(context, controller.value); + if (onError != null) return onError.call(e, stk); + + rethrow; } - return Navigator.pop(context, controller.value); }, ); }, @@ -787,6 +814,7 @@ Widget webOrDesktopAttachmentPickerBuilder({ ThumbnailFormat attachmentThumbnailFormat = ThumbnailFormat.jpeg, int attachmentThumbnailQuality = 100, double attachmentThumbnailScale = 1, + ErrorListener? onError, }) { return StreamWebOrDesktopAttachmentPickerBottomSheet( controller: controller, @@ -814,13 +842,20 @@ Widget webOrDesktopAttachmentPickerBuilder({ }.where((option) => option.supportedTypes.every(allowedTypes.contains)), }, onOptionTap: (context, controller, option) async { - final attachment = await StreamAttachmentHandler.instance.pickFile( - type: option.type.fileType, - ); - if (attachment != null) { - await controller.addAttachment(attachment); + try { + final attachment = await StreamAttachmentHandler.instance.pickFile( + type: option.type.fileType, + ); + if (attachment != null) { + await controller.addAttachment(attachment); + } + return Navigator.pop(context, controller.value); + } catch (e, stk) { + Navigator.pop(context, controller.value); + if (onError != null) return onError.call(e, stk); + + rethrow; } - return Navigator.pop(context, controller.value); }, ); } diff --git a/packages/stream_chat_flutter/lib/src/message_input/attachment_picker/stream_attachment_picker_bottom_sheet.dart b/packages/stream_chat_flutter/lib/src/message_input/attachment_picker/stream_attachment_picker_bottom_sheet.dart index bc53e4db..4546bce6 100644 --- a/packages/stream_chat_flutter/lib/src/message_input/attachment_picker/stream_attachment_picker_bottom_sheet.dart +++ b/packages/stream_chat_flutter/lib/src/message_input/attachment_picker/stream_attachment_picker_bottom_sheet.dart @@ -69,6 +69,7 @@ Future showStreamAttachmentPickerModalBottomSheet({ List allowedTypes = AttachmentPickerType.values, List? initialAttachments, StreamAttachmentPickerController? controller, + ErrorListener? onError, Color? backgroundColor, double? elevation, BoxConstraints? constraints, @@ -117,6 +118,7 @@ Future showStreamAttachmentPickerModalBottomSheet({ if (isWebOrDesktop) { return webOrDesktopAttachmentPickerBuilder.call( context: context, + onError: onError, controller: controller, allowedTypes: allowedTypes, customOptions: customOptions?.map( @@ -131,6 +133,7 @@ Future showStreamAttachmentPickerModalBottomSheet({ return mobileAttachmentPickerBuilder.call( context: context, + onError: onError, controller: controller, allowedTypes: allowedTypes, customOptions: customOptions, diff --git a/packages/stream_chat_flutter/lib/src/message_input/stream_message_input.dart b/packages/stream_chat_flutter/lib/src/message_input/stream_message_input.dart index ab36c3be..f33bbf6a 100644 --- a/packages/stream_chat_flutter/lib/src/message_input/stream_message_input.dart +++ b/packages/stream_chat_flutter/lib/src/message_input/stream_message_input.dart @@ -815,6 +815,7 @@ class StreamMessageInputState extends State Future _onAttachmentButtonPressed() async { final attachments = await showStreamAttachmentPickerModalBottomSheet( context: context, + onError: widget.onError, allowedTypes: widget.allowedAttachmentPickerTypes, initialAttachments: _effectiveController.attachments, );