Merge branch 'master' into feature/notifications
This commit is contained in:
+105
-55
@@ -16,6 +16,8 @@ import 'package:stream_chat_flutter/src/user_avatar.dart';
|
||||
import '../stream_chat_flutter.dart';
|
||||
import 'stream_channel.dart';
|
||||
|
||||
typedef FileUploader = Future<String> Function(File, Channel);
|
||||
|
||||
/// Inactive state
|
||||
/// 
|
||||
/// 
|
||||
@@ -68,11 +70,17 @@ class MessageInput extends StatefulWidget {
|
||||
this.maxHeight = 150,
|
||||
this.keyboardType = TextInputType.multiline,
|
||||
this.disableAttachments = false,
|
||||
this.doImageUploadRequest,
|
||||
this.doFileUploadRequest,
|
||||
this.initialMessage,
|
||||
}) : super(key: key);
|
||||
|
||||
/// Message to edit
|
||||
final Message editMessage;
|
||||
|
||||
/// Message to start with
|
||||
final Message initialMessage;
|
||||
|
||||
/// Function called after sending the message
|
||||
final void Function(Message) onMessageSent;
|
||||
|
||||
@@ -88,14 +96,25 @@ class MessageInput extends StatefulWidget {
|
||||
/// If true the attachments button will not be displayed
|
||||
final bool disableAttachments;
|
||||
|
||||
/// Override image upload request
|
||||
final FileUploader doImageUploadRequest;
|
||||
|
||||
/// Override file upload request
|
||||
final FileUploader doFileUploadRequest;
|
||||
|
||||
@override
|
||||
_MessageInputState createState() => _MessageInputState();
|
||||
_MessageInputState createState() => _MessageInputState(
|
||||
doFileUploadRequest: doFileUploadRequest,
|
||||
doImageUploadRequest: doImageUploadRequest,
|
||||
);
|
||||
}
|
||||
|
||||
class _MessageInputState extends State<MessageInput> {
|
||||
final List<_SendingAttachment> _attachments = [];
|
||||
final _focusNode = FocusNode();
|
||||
final List<User> _mentionedUsers = [];
|
||||
FileUploader doImageUploadRequest;
|
||||
FileUploader doFileUploadRequest;
|
||||
|
||||
TextEditingController _textController;
|
||||
bool _inputEnabled = true;
|
||||
@@ -103,6 +122,14 @@ class _MessageInputState extends State<MessageInput> {
|
||||
bool _typingStarted = false;
|
||||
OverlayEntry _commandsOverlay, _mentionsOverlay;
|
||||
|
||||
_MessageInputState({
|
||||
this.doImageUploadRequest,
|
||||
this.doFileUploadRequest,
|
||||
}) {
|
||||
doImageUploadRequest ??= _uploadImage;
|
||||
doFileUploadRequest ??= _uploadFile;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SafeArea(
|
||||
@@ -617,8 +644,6 @@ class _MessageInputState extends State<MessageInput> {
|
||||
|
||||
final channel = StreamChannel.of(context).channel;
|
||||
|
||||
final bytes = await file.readAsBytes();
|
||||
|
||||
final attachment = _SendingAttachment(
|
||||
file: file,
|
||||
type: type,
|
||||
@@ -628,28 +653,7 @@ class _MessageInputState extends State<MessageInput> {
|
||||
_attachments.add(attachment);
|
||||
});
|
||||
|
||||
String url;
|
||||
final filename = file.path.split('/').last;
|
||||
|
||||
if (type == FileType.image) {
|
||||
final res = await channel.sendImage(
|
||||
MultipartFile.fromBytes(
|
||||
bytes,
|
||||
filename: filename,
|
||||
contentType: MediaType.parse(lookupMimeType(filename)),
|
||||
),
|
||||
);
|
||||
url = res.file;
|
||||
} else {
|
||||
final res = await channel.sendFile(
|
||||
MultipartFile.fromBytes(
|
||||
bytes,
|
||||
filename: filename,
|
||||
contentType: MediaType.parse(lookupMimeType(filename)),
|
||||
),
|
||||
);
|
||||
url = res.file;
|
||||
}
|
||||
final url = await _uploadAttachment(file, type, channel);
|
||||
|
||||
attachment.url = url;
|
||||
|
||||
@@ -658,6 +662,46 @@ class _MessageInputState extends State<MessageInput> {
|
||||
});
|
||||
}
|
||||
|
||||
Future<String> _uploadAttachment(
|
||||
File file,
|
||||
FileType type,
|
||||
Channel channel,
|
||||
) async {
|
||||
String url;
|
||||
if (type == FileType.image) {
|
||||
url = await doImageUploadRequest(file, channel);
|
||||
} else {
|
||||
url = await doFileUploadRequest(file, channel);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
Future<String> _uploadImage(File file, Channel channel) async {
|
||||
final filename = file.path.split('/').last;
|
||||
final bytes = await file.readAsBytes();
|
||||
final res = await channel.sendImage(
|
||||
MultipartFile.fromBytes(
|
||||
bytes,
|
||||
filename: filename,
|
||||
contentType: MediaType.parse(lookupMimeType(filename)),
|
||||
),
|
||||
);
|
||||
return res.file;
|
||||
}
|
||||
|
||||
Future<String> _uploadFile(File file, Channel channel) async {
|
||||
final filename = file.path.split('/').last;
|
||||
final bytes = await file.readAsBytes();
|
||||
final res = await channel.sendFile(
|
||||
MultipartFile.fromBytes(
|
||||
bytes,
|
||||
filename: filename,
|
||||
contentType: MediaType.parse(lookupMimeType(filename)),
|
||||
),
|
||||
);
|
||||
return res.file;
|
||||
}
|
||||
|
||||
Widget _buildSendButton(BuildContext context) {
|
||||
return IconTheme(
|
||||
data:
|
||||
@@ -727,7 +771,7 @@ class _MessageInputState extends State<MessageInput> {
|
||||
channel.cid,
|
||||
);
|
||||
} else {
|
||||
message = Message(
|
||||
message = (widget.initialMessage ?? Message()).copyWith(
|
||||
parentId: widget.parentMessage?.id,
|
||||
text: text,
|
||||
attachments: _getAttachments(attachments).toList(),
|
||||
@@ -804,40 +848,46 @@ class _MessageInputState extends State<MessageInput> {
|
||||
});
|
||||
|
||||
if (widget.editMessage != null) {
|
||||
_textController = TextEditingController(text: widget.editMessage.text);
|
||||
|
||||
_typingStarted = true;
|
||||
_messageIsPresent = true;
|
||||
|
||||
widget.editMessage.attachments.forEach((attachment) {
|
||||
if (attachment.type == 'image') {
|
||||
_attachments.add(_SendingAttachment(
|
||||
type: FileType.image,
|
||||
url: attachment.imageUrl ??
|
||||
attachment.assetUrl ??
|
||||
attachment.thumbUrl ??
|
||||
attachment.ogScrapeUrl,
|
||||
uploaded: true,
|
||||
));
|
||||
} else if (attachment.type == 'video') {
|
||||
_attachments.add(_SendingAttachment(
|
||||
type: FileType.video,
|
||||
url: attachment.assetUrl,
|
||||
uploaded: true,
|
||||
));
|
||||
} else if (attachment.type != 'giphy') {
|
||||
_attachments.add(_SendingAttachment(
|
||||
type: FileType.any,
|
||||
url: attachment.assetUrl,
|
||||
uploaded: true,
|
||||
));
|
||||
}
|
||||
});
|
||||
_parseExistingMessage(widget.editMessage);
|
||||
} else if (widget.initialMessage != null) {
|
||||
_parseExistingMessage(widget.initialMessage);
|
||||
} else {
|
||||
_textController = TextEditingController();
|
||||
}
|
||||
}
|
||||
|
||||
void _parseExistingMessage(Message message) {
|
||||
_textController = TextEditingController(text: message.text);
|
||||
|
||||
_typingStarted = true;
|
||||
_messageIsPresent = true;
|
||||
|
||||
message.attachments?.forEach((attachment) {
|
||||
if (attachment.type == 'image') {
|
||||
_attachments.add(_SendingAttachment(
|
||||
type: FileType.image,
|
||||
url: attachment.imageUrl ??
|
||||
attachment.assetUrl ??
|
||||
attachment.thumbUrl ??
|
||||
attachment.ogScrapeUrl,
|
||||
uploaded: true,
|
||||
));
|
||||
} else if (attachment.type == 'video') {
|
||||
_attachments.add(_SendingAttachment(
|
||||
type: FileType.video,
|
||||
url: attachment.assetUrl,
|
||||
uploaded: true,
|
||||
));
|
||||
} else if (attachment.type != 'giphy') {
|
||||
_attachments.add(_SendingAttachment(
|
||||
type: FileType.any,
|
||||
url: attachment.assetUrl,
|
||||
uploaded: true,
|
||||
));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_commandsOverlay?.remove();
|
||||
|
||||
Reference in New Issue
Block a user