1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-23 06:53:23 +00:00

Can cancel file sending

This commit is contained in:
Pierre HUBERT 2021-03-13 09:00:00 +01:00
parent bd5ed8fb33
commit dbb2a3f1a1
4 changed files with 56 additions and 11 deletions

View File

@ -77,6 +77,8 @@ class APIHelper {
final response = await Dio().post(
url.toString(),
data: data,
cancelToken: request.cancelToken,
onSendProgress: request.progressCallback,
options: Options(
receiveDataWhenStatusError: true,
validateStatus: (s) => true,

View File

@ -18,6 +18,7 @@ import 'package:comunic/models/new_conversation_settings.dart';
import 'package:comunic/models/unread_conversation.dart';
import 'package:comunic/utils/account_utils.dart';
import 'package:comunic/utils/dart_color.dart';
import 'package:dio/dio.dart';
import 'package:meta/meta.dart';
/// Conversation helper
@ -299,15 +300,17 @@ class ConversationsHelper {
}
/// Send a new message to the server
Future<SendMessageResult> sendMessage(NewConversationMessage message) async {
final request = APIRequest(
uri: "conversations/sendMessage",
needLogin: true,
args: {
"conversationID": message.conversationID.toString(),
"message": message.hasMessage ? message.message : ""
},
);
Future<SendMessageResult> sendMessage(
NewConversationMessage message, {
ProgressCallback sendProgress,
CancelToken cancelToken,
}) async {
final request = APIRequest.withLogin("conversations/sendMessage")
.addInt("conversationID", message.conversationID)
.addString("message", message.hasMessage ? message.message : "");
request.progressCallback = sendProgress;
request.cancelToken = cancelToken;
// Check for file
if (message.hasFile) request.addBytesFile("file", message.file);

View File

@ -2,6 +2,7 @@ import 'dart:io';
import 'package:comunic/helpers/api_helper.dart';
import 'package:comunic/models/api_response.dart';
import 'package:dio/dio.dart';
import 'package:http_parser/http_parser.dart';
import 'package:image_picker/image_picker.dart';
import 'package:meta/meta.dart';
@ -27,6 +28,8 @@ class BytesFile {
class APIRequest {
final String uri;
final bool needLogin;
ProgressCallback progressCallback;
CancelToken cancelToken;
Map<String, String> args;
Map<String, File> files = Map();
Map<String, PickedFile> pickedFiles = Map();

View File

@ -24,6 +24,7 @@ import 'package:comunic/utils/list_utils.dart';
import 'package:comunic/utils/log_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:comunic/utils/video_utils.dart';
import 'package:dio/dio.dart';
import 'package:emoji_picker/emoji_picker.dart';
import 'package:flutter/material.dart';
import 'package:mime/mime.dart';
@ -65,6 +66,9 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
ScrollWatcher _scrollController;
_OlderMessagesLevel _loadingOlderMessages = _OlderMessagesLevel.NONE;
CancelToken _sendCancel;
double _sendProgress;
String get textMessage => _textController.text;
bool get _isMessageValid =>
@ -292,17 +296,28 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
maxWidth: srvConfig.conversationsPolicy.maxThumbnailWidth,
);
await _submitMessage(
_sendCancel = CancelToken();
final progressCb =
(count, total) => setState(() => _sendProgress = count / total);
final res = await ConversationsHelper().sendMessage(
NewConversationMessage(
conversationID: widget.conversationID,
message: null,
file: file,
thumbnail: thumbnail),
sendProgress: progressCb,
cancelToken: _sendCancel,
);
assert(res == SendMessageResult.SUCCESS);
} catch (e, s) {
logError(e, s);
showSimpleSnack(context, tr("Failed to send a file!"));
}
setState(() {
_sendCancel = null;
_sendProgress = null;
});
}
/// Send a new text message
@ -590,6 +605,28 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
numRecommended: 50,
);
Widget _buildSendingWidget() => Container(
height: 68,
color: _senderColor,
child: Row(
children: <Widget>[
Spacer(flex: 1),
Flexible(
child: LinearProgressIndicator(value: _sendProgress),
flex: 5,
),
Spacer(flex: 1),
Text("${(_sendProgress * 100).toInt()}%"),
Spacer(flex: 1),
OutlineButton(
onPressed: () => _sendCancel.cancel(),
child: Text(tr("Cancel").toUpperCase()),
),
Spacer(flex: 1),
],
),
);
@override
Widget build(BuildContext context) {
if (_error == ErrorLevel.MAJOR) return _buildError();
@ -607,7 +644,7 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
: null,
),
_messages.length == 0 ? _buildNoMessagesNotice() : _buildMessagesList(),
_buildSendMessageForm(),
_sendCancel != null ? _buildSendingWidget() : _buildSendMessageForm(),
_showEmojiPicker ? _buildEmojiContainer() : Container(),
],
);