1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 12:59:21 +00:00

File update system

This commit is contained in:
Pierre HUBERT 2021-03-12 17:47:09 +01:00
parent ec4ca238de
commit 701d5d3c27
3 changed files with 45 additions and 31 deletions

View File

@ -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();

View File

@ -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;
}

View File

@ -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<ConversationScreen> {
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<ConversationScreen> {
/// Pick and send an image
Future<void> _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<void> _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<ConversationScreen> {
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<ConversationScreen> {
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<ConversationScreen> {
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<ConversationScreen> {
enabled: !_isSendingMessage,
controller: _textEditingController,
onChanged: _updatedText,
onChanged: (s) => setState(() {}),
onSubmitted: _isMessageValid
? (s) => _submitTextMessage(context, s)
: null,