From 701d5d3c27744f3223b4331ecd0e8ccd83d335e0 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 12 Mar 2021 17:47:09 +0100 Subject: [PATCH] File update system --- lib/helpers/conversations_helper.dart | 6 +-- lib/models/new_conversation_message.dart | 16 ++++--- lib/ui/screens/conversation_screen.dart | 54 +++++++++++++++--------- 3 files changed, 45 insertions(+), 31 deletions(-) diff --git a/lib/helpers/conversations_helper.dart b/lib/helpers/conversations_helper.dart index cbd1b1c..054c4c7 100644 --- a/lib/helpers/conversations_helper.dart +++ b/lib/helpers/conversations_helper.dart @@ -309,12 +309,12 @@ class ConversationsHelper { }, ); - // Check for image - if (message.hasImage) request.addPickedFile("image", message.image); + // Check for file + if (message.hasFile) request.addBytesFile("file", message.file); //Send the message APIResponse response; - if (!message.hasImage) + if (!message.hasFile) response = await request.exec(); else response = await request.execWithFiles(); diff --git a/lib/models/new_conversation_message.dart b/lib/models/new_conversation_message.dart index 63d2d08..9fa8b8d 100644 --- a/lib/models/new_conversation_message.dart +++ b/lib/models/new_conversation_message.dart @@ -1,4 +1,4 @@ -import 'package:image_picker/image_picker.dart'; +import 'package:comunic/models/api_request.dart'; import 'package:meta/meta.dart'; /// New conversation message model @@ -10,14 +10,16 @@ import 'package:meta/meta.dart'; class NewConversationMessage { final int conversationID; final String message; - final PickedFile image; + final BytesFile file; - NewConversationMessage( - {@required this.conversationID, @required this.message, this.image}) - : assert(conversationID != null), - assert(image != null || message != null); + NewConversationMessage({ + @required this.conversationID, + @required this.message, + this.file, + }) : assert(conversationID != null), + assert(file != null || message != null); bool get hasMessage => message != null; - bool get hasImage => image != null; + bool get hasFile => file != null; } diff --git a/lib/ui/screens/conversation_screen.dart b/lib/ui/screens/conversation_screen.dart index 9f1c9fd..b555a06 100644 --- a/lib/ui/screens/conversation_screen.dart +++ b/lib/ui/screens/conversation_screen.dart @@ -6,6 +6,7 @@ import 'package:comunic/helpers/server_config_helper.dart'; import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/lists/conversation_messages_list.dart'; import 'package:comunic/lists/users_list.dart'; +import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/conversation.dart'; import 'package:comunic/models/conversation_message.dart'; import 'package:comunic/models/new_conversation_message.dart'; @@ -17,6 +18,7 @@ import 'package:comunic/ui/widgets/scroll_watcher.dart'; import 'package:comunic/utils/files_utils.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/list_utils.dart'; +import 'package:comunic/utils/log_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; @@ -48,12 +50,20 @@ class _ConversationScreenState extends SafeState { ConversationMessagesList _messages; UsersList _usersInfo = UsersList(); ErrorLevel _error = ErrorLevel.NONE; - bool _isMessageValid = false; + bool _isSendingMessage = false; TextEditingController _textEditingController = TextEditingController(); ScrollWatcher _scrollController; _OlderMessagesLevel _loadingOlderMessages = _OlderMessagesLevel.NONE; + String get textMessage => _textEditingController.text; + + bool get _isMessageValid => + textMessage.length >= + ServerConfigurationHelper.config.conversationsPolicy.minMessageLen && + textMessage.length < + ServerConfigurationHelper.config.conversationsPolicy.maxMessageLen; + @override void initState() { super.initState(); @@ -213,21 +223,31 @@ class _ConversationScreenState extends SafeState { /// Pick and send an image Future _sendImage(BuildContext context) async { - final image = await pickImage(context); + try { + final image = await pickImage(context); - if (image == null) return null; + if (image == null) return; - _submitMessage( + await _sendFileMessage(BytesFile( + image.path.split("/").last, + await image.readAsBytes(), + )); + } catch (e, s) { + logError(e, s); + showSimpleSnack(context, tr("Failed to send image!")); + } + } + + /// Send a file message + Future _sendFileMessage(BytesFile file) async { + await _submitMessage( context, NewConversationMessage( conversationID: widget.conversationID, message: null, - image: image, + file: file, ), ); - - // In case a message was already written in the input - _updatedText(_textEditingController.text); } /// Send a new text message @@ -265,18 +285,9 @@ class _ConversationScreenState extends SafeState { return result; } - void _updatedText(String text) { - setState(() { - _isMessageValid = text.length > 2; - }); - } - /// Clear send message form void _clearSendMessageForm() { - setState(() { - _textEditingController = TextEditingController(); - _isMessageValid = false; - }); + setState(() => _textEditingController = TextEditingController()); } /// Check if a message is the last message of a user or not @@ -352,7 +363,7 @@ class _ConversationScreenState extends SafeState { margin: new EdgeInsets.symmetric(horizontal: 4.0), child: new IconButton( icon: new Icon( - Icons.photo_camera, + Icons.add, color: _isSendingMessage ? Theme.of(context).disabledColor : Theme.of(context).accentColor, @@ -366,7 +377,8 @@ class _ConversationScreenState extends SafeState { child: new TextField( keyboardType: TextInputType.text, maxLines: null, - maxLength: 200, + maxLength: ServerConfigurationHelper + .config.conversationsPolicy.maxMessageLen, maxLengthEnforced: true, // Show max length only when there is some text already typed @@ -374,7 +386,7 @@ class _ConversationScreenState extends SafeState { enabled: !_isSendingMessage, controller: _textEditingController, - onChanged: _updatedText, + onChanged: (s) => setState(() {}), onSubmitted: _isMessageValid ? (s) => _submitTextMessage(context, s) : null,