diff --git a/lib/src/channel_list_view.dart b/lib/src/channel_list_view.dart index b7fbf408..45c53eb6 100644 --- a/lib/src/channel_list_view.dart +++ b/lib/src/channel_list_view.dart @@ -185,26 +185,25 @@ class _ChannelListViewState extends State child = widget.emptyBuilder(context); } - if (channels.isEmpty && widget.emptyBuilder == null) { - child = LayoutBuilder( - builder: (context, viewportConstraints) { - return SingleChildScrollView( - physics: AlwaysScrollableScrollPhysics(), - child: Stack( - children: [ - ConstrainedBox( - constraints: BoxConstraints( - minHeight: viewportConstraints.maxHeight, - ), - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Padding( - padding: const EdgeInsets.all(8.0), - child: StreamSvgIcon.message( - size: 136, - color: Color(0xffDBDBDB), - ), + if (channels.isEmpty && widget.emptyBuilder == null) { + child = LayoutBuilder( + builder: (context, viewportConstraints) { + return SingleChildScrollView( + physics: AlwaysScrollableScrollPhysics(), + child: Stack( + children: [ + ConstrainedBox( + constraints: BoxConstraints( + minHeight: viewportConstraints.maxHeight, + ), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Padding( + padding: const EdgeInsets.all(8.0), + child: StreamSvgIcon.message( + size: 136, + color: Color(0xffDBDBDB), ), ), Padding( diff --git a/lib/src/compress_video_service.dart b/lib/src/compress_video_service.dart new file mode 100644 index 00000000..f8d63e0c --- /dev/null +++ b/lib/src/compress_video_service.dart @@ -0,0 +1,19 @@ +import 'dart:async'; +import 'package:video_compress/video_compress.dart'; + +class CompressVideoService { + static final CompressVideoService instance = CompressVideoService._(); + + CompressVideoService._(); + + Future compressVideo(String path) async { + MediaInfo mediaInfo; + while (VideoCompress.isCompressing) { + await Future.delayed(Duration(seconds: 1)); + } + mediaInfo = await VideoCompress.compressVideo( + path, + ); + return mediaInfo; + } +} diff --git a/lib/src/message_input.dart b/lib/src/message_input.dart index 25cc12db..9a8b1f75 100644 --- a/lib/src/message_input.dart +++ b/lib/src/message_input.dart @@ -14,6 +14,7 @@ import 'package:media_gallery/media_gallery.dart'; import 'package:mime/mime.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:stream_chat/stream_chat.dart'; +import 'package:stream_chat_flutter/src/compress_video_service.dart'; import 'package:stream_chat_flutter/src/media_list_view.dart'; import 'package:stream_chat_flutter/src/message_list_view.dart'; import 'package:stream_chat_flutter/src/stream_chat_theme.dart'; @@ -865,7 +866,18 @@ class MessageInputState extends State { if (file.size > _kMaxAttachmentSize) { if (medium.mediaType == MediaType.video) { - final mediaInfo = await _compressVideo(file); + final mediaInfo = + await CompressVideoService.instance.compressVideo(file.path); + + if (mediaInfo.filesize / (1024 * 1024) > _kMaxAttachmentSize) { + Scaffold.of(context).showSnackBar( + SnackBar( + content: Text( + 'The file is too large to upload. The file size limit is 20MB. We tried compressing it, but it was not enough.', + ), + ), + ); + } file = PlatformFile( name: file.name, size: mediaInfo.filesize, @@ -929,33 +941,6 @@ class MessageInputState extends State { } } - Future _compressVideo(PlatformFile file) async { - print('file.size: ${file.size}'); - print('VideoCompress.isCompressing: ${VideoCompress.isCompressing}'); - if (VideoCompress.isCompressing) { - final compressCompleter = Completer(); - VideoCompress.compressProgress$.subscribe( - (event) { - print('event: ${event}'); - }, - onDone: () { - compressCompleter.complete(); - }, - onError: (e) { - compressCompleter.completeError(e); - }, - ); - await compressCompleter.future; - } - final mediaInfo = await VideoCompress.compressVideo( - file.path, - ); - - print('mediaInfo.filesize: ${mediaInfo.filesize / 1024}'); - - return mediaInfo; - } - CircleAvatar _buildGiphyIcon() { if (kIsWeb) { return CircleAvatar( @@ -1571,7 +1556,8 @@ class MessageInputState extends State { if (file.size > _kMaxAttachmentSize) { if (attachmentType == 'video') { - final mediaInfo = await _compressVideo(file); + final mediaInfo = await CompressVideoService.instance + .compressVideo(file.path, _kMaxAttachmentSize); file = PlatformFile( name: mediaInfo.title, size: mediaInfo.filesize, @@ -1800,23 +1786,11 @@ class MessageInputState extends State { } StreamSubscription _keyboardListener; - Subscription _videoCompressSubscription; - Completer _videoCompressCompleter = Completer(); @override void initState() { super.initState(); - VideoCompress.compressProgress$.subscribe( - (event) {}, - onDone: () { - _videoCompressCompleter.complete(); - }, - onError: (e) { - _videoCompressCompleter.completeError(e); - }, - ); - _focusNode = widget.focusNode ?? FocusNode(); _emojiNames = Emoji.all().map((e) => e.name);