diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index 15a9a9b2..f11ba8c1 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -1,3 +1,9 @@ +## Upcoming + +🔄 Changed + +- Cancelling a attachment upload now removes the attachment from the message. + ## 5.3.0 🔄 Changed diff --git a/packages/stream_chat/lib/src/client/channel.dart b/packages/stream_chat/lib/src/client/channel.dart index bfaae76a..888780f3 100644 --- a/packages/stream_chat/lib/src/client/channel.dart +++ b/packages/stream_chat/lib/src/client/channel.dart @@ -463,12 +463,19 @@ class Channel { client.logger.info('Found ${attachments.length} attachments'); - void updateAttachment(Attachment attachment) { + void updateAttachment(Attachment attachment, {bool remove = false}) { final index = message!.attachments.indexWhere( (it) => it.id == attachment.id, ); if (index != -1) { - final newAttachments = [...message!.attachments]..[index] = attachment; + // update or remove attachment from message. + final List newAttachments; + if (remove) { + newAttachments = [...message!.attachments]..removeAt(index); + } else { + newAttachments = [...message!.attachments]..[index] = attachment; + } + final updatedMessage = message!.copyWith(attachments: newAttachments); state?.updateMessage(updatedMessage); // updating original message for next iteration @@ -533,6 +540,14 @@ class Channel { ); } }).catchError((e, stk) { + if (e is StreamChatNetworkError && e.isRequestCancelledError) { + client.logger.info('Attachment ${it.id} upload cancelled'); + + // remove attachment from message if cancelled. + updateAttachment(it, remove: true); + return; + } + client.logger.severe('error uploading the attachment', e, stk); updateAttachment( it.copyWith(uploadState: UploadState.failed(error: e.toString())), diff --git a/packages/stream_chat/lib/src/core/error/stream_chat_error.dart b/packages/stream_chat/lib/src/core/error/stream_chat_error.dart index 80e83079..fc069a90 100644 --- a/packages/stream_chat/lib/src/core/error/stream_chat_error.dart +++ b/packages/stream_chat/lib/src/core/error/stream_chat_error.dart @@ -74,6 +74,7 @@ class StreamChatNetworkError extends StreamChatError { ChatErrorCode errorCode, { int? statusCode, this.data, + this.isRequestCancelledError = false, }) : code = errorCode.code, statusCode = statusCode ?? data?.statusCode, super(errorCode.message); @@ -84,6 +85,7 @@ class StreamChatNetworkError extends StreamChatError { required String message, this.statusCode, this.data, + this.isRequestCancelledError = false, }) : super(message); /// @@ -100,6 +102,7 @@ class StreamChatNetworkError extends StreamChatError { errorResponse?.message ?? response?.statusMessage ?? error.message, statusCode: errorResponse?.statusCode ?? response?.statusCode, data: errorResponse, + isRequestCancelledError: error.type == DioErrorType.cancel, )..stackTrace = error.stackTrace; } @@ -112,6 +115,9 @@ class StreamChatNetworkError extends StreamChatError { /// Response body. please refer to [ErrorResponse]. final ErrorResponse? data; + /// True, in case the error is due to a cancelled network request. + final bool isRequestCancelledError; + StackTrace? _stackTrace; ///