mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-04 04:04:18 +00:00 
			
		
		
		
	Can cancel file sending
This commit is contained in:
		@@ -77,6 +77,8 @@ class APIHelper {
 | 
				
			|||||||
      final response = await Dio().post(
 | 
					      final response = await Dio().post(
 | 
				
			||||||
        url.toString(),
 | 
					        url.toString(),
 | 
				
			||||||
        data: data,
 | 
					        data: data,
 | 
				
			||||||
 | 
					        cancelToken: request.cancelToken,
 | 
				
			||||||
 | 
					        onSendProgress: request.progressCallback,
 | 
				
			||||||
        options: Options(
 | 
					        options: Options(
 | 
				
			||||||
          receiveDataWhenStatusError: true,
 | 
					          receiveDataWhenStatusError: true,
 | 
				
			||||||
          validateStatus: (s) => true,
 | 
					          validateStatus: (s) => true,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,6 +18,7 @@ import 'package:comunic/models/new_conversation_settings.dart';
 | 
				
			|||||||
import 'package:comunic/models/unread_conversation.dart';
 | 
					import 'package:comunic/models/unread_conversation.dart';
 | 
				
			||||||
import 'package:comunic/utils/account_utils.dart';
 | 
					import 'package:comunic/utils/account_utils.dart';
 | 
				
			||||||
import 'package:comunic/utils/dart_color.dart';
 | 
					import 'package:comunic/utils/dart_color.dart';
 | 
				
			||||||
 | 
					import 'package:dio/dio.dart';
 | 
				
			||||||
import 'package:meta/meta.dart';
 | 
					import 'package:meta/meta.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Conversation helper
 | 
					/// Conversation helper
 | 
				
			||||||
@@ -299,15 +300,17 @@ class ConversationsHelper {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// Send a new message to the server
 | 
					  /// Send a new message to the server
 | 
				
			||||||
  Future<SendMessageResult> sendMessage(NewConversationMessage message) async {
 | 
					  Future<SendMessageResult> sendMessage(
 | 
				
			||||||
    final request = APIRequest(
 | 
					    NewConversationMessage message, {
 | 
				
			||||||
      uri: "conversations/sendMessage",
 | 
					    ProgressCallback sendProgress,
 | 
				
			||||||
      needLogin: true,
 | 
					    CancelToken cancelToken,
 | 
				
			||||||
      args: {
 | 
					  }) async {
 | 
				
			||||||
        "conversationID": message.conversationID.toString(),
 | 
					    final request = APIRequest.withLogin("conversations/sendMessage")
 | 
				
			||||||
        "message": message.hasMessage ? message.message : ""
 | 
					        .addInt("conversationID", message.conversationID)
 | 
				
			||||||
      },
 | 
					        .addString("message", message.hasMessage ? message.message : "");
 | 
				
			||||||
    );
 | 
					
 | 
				
			||||||
 | 
					    request.progressCallback = sendProgress;
 | 
				
			||||||
 | 
					    request.cancelToken = cancelToken;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Check for file
 | 
					    // Check for file
 | 
				
			||||||
    if (message.hasFile) request.addBytesFile("file", message.file);
 | 
					    if (message.hasFile) request.addBytesFile("file", message.file);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,7 @@ import 'dart:io';
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import 'package:comunic/helpers/api_helper.dart';
 | 
					import 'package:comunic/helpers/api_helper.dart';
 | 
				
			||||||
import 'package:comunic/models/api_response.dart';
 | 
					import 'package:comunic/models/api_response.dart';
 | 
				
			||||||
 | 
					import 'package:dio/dio.dart';
 | 
				
			||||||
import 'package:http_parser/http_parser.dart';
 | 
					import 'package:http_parser/http_parser.dart';
 | 
				
			||||||
import 'package:image_picker/image_picker.dart';
 | 
					import 'package:image_picker/image_picker.dart';
 | 
				
			||||||
import 'package:meta/meta.dart';
 | 
					import 'package:meta/meta.dart';
 | 
				
			||||||
@@ -27,6 +28,8 @@ class BytesFile {
 | 
				
			|||||||
class APIRequest {
 | 
					class APIRequest {
 | 
				
			||||||
  final String uri;
 | 
					  final String uri;
 | 
				
			||||||
  final bool needLogin;
 | 
					  final bool needLogin;
 | 
				
			||||||
 | 
					  ProgressCallback progressCallback;
 | 
				
			||||||
 | 
					  CancelToken cancelToken;
 | 
				
			||||||
  Map<String, String> args;
 | 
					  Map<String, String> args;
 | 
				
			||||||
  Map<String, File> files = Map();
 | 
					  Map<String, File> files = Map();
 | 
				
			||||||
  Map<String, PickedFile> pickedFiles = Map();
 | 
					  Map<String, PickedFile> pickedFiles = Map();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -24,6 +24,7 @@ import 'package:comunic/utils/list_utils.dart';
 | 
				
			|||||||
import 'package:comunic/utils/log_utils.dart';
 | 
					import 'package:comunic/utils/log_utils.dart';
 | 
				
			||||||
import 'package:comunic/utils/ui_utils.dart';
 | 
					import 'package:comunic/utils/ui_utils.dart';
 | 
				
			||||||
import 'package:comunic/utils/video_utils.dart';
 | 
					import 'package:comunic/utils/video_utils.dart';
 | 
				
			||||||
 | 
					import 'package:dio/dio.dart';
 | 
				
			||||||
import 'package:emoji_picker/emoji_picker.dart';
 | 
					import 'package:emoji_picker/emoji_picker.dart';
 | 
				
			||||||
import 'package:flutter/material.dart';
 | 
					import 'package:flutter/material.dart';
 | 
				
			||||||
import 'package:mime/mime.dart';
 | 
					import 'package:mime/mime.dart';
 | 
				
			||||||
@@ -65,6 +66,9 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
 | 
				
			|||||||
  ScrollWatcher _scrollController;
 | 
					  ScrollWatcher _scrollController;
 | 
				
			||||||
  _OlderMessagesLevel _loadingOlderMessages = _OlderMessagesLevel.NONE;
 | 
					  _OlderMessagesLevel _loadingOlderMessages = _OlderMessagesLevel.NONE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  CancelToken _sendCancel;
 | 
				
			||||||
 | 
					  double _sendProgress;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  String get textMessage => _textController.text;
 | 
					  String get textMessage => _textController.text;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  bool get _isMessageValid =>
 | 
					  bool get _isMessageValid =>
 | 
				
			||||||
@@ -292,17 +296,28 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
 | 
				
			|||||||
          maxWidth: srvConfig.conversationsPolicy.maxThumbnailWidth,
 | 
					          maxWidth: srvConfig.conversationsPolicy.maxThumbnailWidth,
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      await _submitMessage(
 | 
					      _sendCancel = CancelToken();
 | 
				
			||||||
 | 
					      final progressCb =
 | 
				
			||||||
 | 
					          (count, total) => setState(() => _sendProgress = count / total);
 | 
				
			||||||
 | 
					      final res = await ConversationsHelper().sendMessage(
 | 
				
			||||||
        NewConversationMessage(
 | 
					        NewConversationMessage(
 | 
				
			||||||
            conversationID: widget.conversationID,
 | 
					            conversationID: widget.conversationID,
 | 
				
			||||||
            message: null,
 | 
					            message: null,
 | 
				
			||||||
            file: file,
 | 
					            file: file,
 | 
				
			||||||
            thumbnail: thumbnail),
 | 
					            thumbnail: thumbnail),
 | 
				
			||||||
 | 
					        sendProgress: progressCb,
 | 
				
			||||||
 | 
					        cancelToken: _sendCancel,
 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
 | 
					      assert(res == SendMessageResult.SUCCESS);
 | 
				
			||||||
    } catch (e, s) {
 | 
					    } catch (e, s) {
 | 
				
			||||||
      logError(e, s);
 | 
					      logError(e, s);
 | 
				
			||||||
      showSimpleSnack(context, tr("Failed to send a file!"));
 | 
					      showSimpleSnack(context, tr("Failed to send a file!"));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    setState(() {
 | 
				
			||||||
 | 
					      _sendCancel = null;
 | 
				
			||||||
 | 
					      _sendProgress = null;
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// Send a new text message
 | 
					  /// Send a new text message
 | 
				
			||||||
@@ -590,6 +605,28 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
 | 
				
			|||||||
        numRecommended: 50,
 | 
					        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
 | 
					  @override
 | 
				
			||||||
  Widget build(BuildContext context) {
 | 
					  Widget build(BuildContext context) {
 | 
				
			||||||
    if (_error == ErrorLevel.MAJOR) return _buildError();
 | 
					    if (_error == ErrorLevel.MAJOR) return _buildError();
 | 
				
			||||||
@@ -607,7 +644,7 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
 | 
				
			|||||||
              : null,
 | 
					              : null,
 | 
				
			||||||
        ),
 | 
					        ),
 | 
				
			||||||
        _messages.length == 0 ? _buildNoMessagesNotice() : _buildMessagesList(),
 | 
					        _messages.length == 0 ? _buildNoMessagesNotice() : _buildMessagesList(),
 | 
				
			||||||
        _buildSendMessageForm(),
 | 
					        _sendCancel != null ? _buildSendingWidget() : _buildSendMessageForm(),
 | 
				
			||||||
        _showEmojiPicker ? _buildEmojiContainer() : Container(),
 | 
					        _showEmojiPicker ? _buildEmojiContainer() : Container(),
 | 
				
			||||||
      ],
 | 
					      ],
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user