wip
This commit is contained in:
+143
-50
@@ -21,6 +21,7 @@ import 'package:stream_chat_flutter/src/stream_chat_theme.dart';
|
||||
import 'package:stream_chat_flutter/src/user_avatar.dart';
|
||||
import 'package:stream_chat_flutter/src/video_thumbnail.dart';
|
||||
import 'package:substring_highlight/substring_highlight.dart';
|
||||
import 'package:video_compress/video_compress.dart';
|
||||
|
||||
import '../stream_chat_flutter.dart';
|
||||
import 'stream_channel.dart';
|
||||
@@ -44,6 +45,8 @@ enum DefaultAttachmentTypes {
|
||||
|
||||
const _kMinMediaPickerSize = 360.0;
|
||||
|
||||
const _kMaxAttachmentSize = 20480; //20MB
|
||||
|
||||
/// Inactive state
|
||||
/// 
|
||||
/// 
|
||||
@@ -797,7 +800,6 @@ class MessageInputState extends State<MessageInput> {
|
||||
var status = await (Platform.isAndroid
|
||||
? Permission.storage.status
|
||||
: Permission.photos.status);
|
||||
print('status: ${status}');
|
||||
if (status.isPermanentlyDenied || status.isDenied) {
|
||||
if (await openAppSettings()) {
|
||||
setState(() {});
|
||||
@@ -844,55 +846,114 @@ class MessageInputState extends State<MessageInput> {
|
||||
}
|
||||
|
||||
void _addAttachment(Media medium) async {
|
||||
final attachment = _SendingAttachment(
|
||||
id: medium.id,
|
||||
);
|
||||
|
||||
setState(() {
|
||||
_attachments.add(attachment);
|
||||
});
|
||||
|
||||
final mediaFile = await medium.getFile();
|
||||
|
||||
final file = PlatformFile(
|
||||
path: mediaFile.path,
|
||||
bytes: mediaFile.readAsBytesSync(),
|
||||
);
|
||||
|
||||
final channel = StreamChannel.of(context).channel;
|
||||
setState(() {
|
||||
attachment
|
||||
..file = file
|
||||
..attachment = Attachment(
|
||||
localUri: file.path != null ? Uri.parse(file.path) : null,
|
||||
type: medium.mediaType == MediaType.image ? 'image' : 'video',
|
||||
);
|
||||
});
|
||||
|
||||
final url = await _uploadAttachment(
|
||||
file,
|
||||
medium.mediaType == MediaType.image
|
||||
? DefaultAttachmentTypes.image
|
||||
: DefaultAttachmentTypes.video,
|
||||
channel);
|
||||
|
||||
final fileType = medium.mediaType == MediaType.image
|
||||
? DefaultAttachmentTypes.image
|
||||
: DefaultAttachmentTypes.video;
|
||||
|
||||
if (fileType == DefaultAttachmentTypes.image) {
|
||||
attachment.attachment = attachment.attachment.copyWith(
|
||||
imageUrl: url,
|
||||
try {
|
||||
final attachment = _SendingAttachment(
|
||||
id: medium.id,
|
||||
);
|
||||
} else {
|
||||
attachment.attachment = attachment.attachment.copyWith(
|
||||
assetUrl: url,
|
||||
|
||||
setState(() {
|
||||
_attachments.add(attachment);
|
||||
});
|
||||
|
||||
final mediaFile = await medium.getFile();
|
||||
|
||||
var file = PlatformFile(
|
||||
path: mediaFile.path,
|
||||
size: (await mediaFile.length()) ~/ 1024,
|
||||
bytes: mediaFile.readAsBytesSync(),
|
||||
);
|
||||
|
||||
if (file.size > _kMaxAttachmentSize) {
|
||||
if (medium.mediaType == MediaType.video) {
|
||||
final mediaInfo = await _compressVideo(file);
|
||||
file = PlatformFile(
|
||||
name: file.name,
|
||||
size: mediaInfo.filesize,
|
||||
bytes: await mediaInfo.file.readAsBytes(),
|
||||
path: mediaInfo.path,
|
||||
);
|
||||
} else {
|
||||
Scaffold.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
'The file is too large to upload. The file size limit is 20MB',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final channel = StreamChannel.of(context).channel;
|
||||
setState(() {
|
||||
attachment
|
||||
..file = file
|
||||
..attachment = Attachment(
|
||||
localUri: file.path != null ? Uri.parse(file.path) : null,
|
||||
type: medium.mediaType == MediaType.image ? 'image' : 'video',
|
||||
);
|
||||
});
|
||||
|
||||
final url = await _uploadAttachment(
|
||||
file,
|
||||
medium.mediaType == MediaType.image
|
||||
? DefaultAttachmentTypes.image
|
||||
: DefaultAttachmentTypes.video,
|
||||
channel);
|
||||
|
||||
final fileType = medium.mediaType == MediaType.image
|
||||
? DefaultAttachmentTypes.image
|
||||
: DefaultAttachmentTypes.video;
|
||||
|
||||
if (fileType == DefaultAttachmentTypes.image) {
|
||||
attachment.attachment = attachment.attachment.copyWith(
|
||||
imageUrl: url,
|
||||
);
|
||||
} else {
|
||||
attachment.attachment = attachment.attachment.copyWith(
|
||||
assetUrl: url,
|
||||
);
|
||||
}
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
attachment.uploaded = true;
|
||||
});
|
||||
}
|
||||
} catch (e, s) {
|
||||
print(s);
|
||||
Scaffold.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Error adding the attachment: $e'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
setState(() {
|
||||
attachment.uploaded = true;
|
||||
});
|
||||
Future<MediaInfo> _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() {
|
||||
@@ -1270,9 +1331,10 @@ class MessageInputState extends State<MessageInput> {
|
||||
Positioned.fill(
|
||||
child: Container(
|
||||
child: VideoThumbnail(
|
||||
file: File(
|
||||
attachment.file.path,
|
||||
)),
|
||||
file: File(
|
||||
attachment.file.path,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
@@ -1501,7 +1563,6 @@ class MessageInputState extends State<MessageInput> {
|
||||
);
|
||||
if (res?.files?.isNotEmpty == true) {
|
||||
file = res.files.single;
|
||||
print('file.bytes?.length: ${file.bytes?.length}');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1513,6 +1574,26 @@ class MessageInputState extends State<MessageInput> {
|
||||
return;
|
||||
}
|
||||
|
||||
if (file.size > _kMaxAttachmentSize) {
|
||||
if (attachmentType == 'video') {
|
||||
final mediaInfo = await _compressVideo(file);
|
||||
file = PlatformFile(
|
||||
name: mediaInfo.title,
|
||||
size: mediaInfo.filesize,
|
||||
bytes: await mediaInfo.file.readAsBytes(),
|
||||
path: mediaInfo.path,
|
||||
);
|
||||
} else {
|
||||
Scaffold.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
'The file is too large to upload. The file size limit is 20MB',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final channel = StreamChannel.of(context).channel;
|
||||
final attachment = _SendingAttachment(
|
||||
file: file,
|
||||
@@ -1740,11 +1821,23 @@ class MessageInputState extends State<MessageInput> {
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user