From 28063614bff7278de8c3456302d4be24dc52294c Mon Sep 17 00:00:00 2001 From: dujiepeng <347302029@qq.com> Date: Wed, 17 May 2023 11:11:37 +0800 Subject: [PATCH] fix down status --- .../im_flutter_sdk/EMChatManagerWrapper.java | 67 ++++++++++++-- ios/Classes/EMChatManagerWrapper.m | 89 ++++++++++++++++--- 2 files changed, 136 insertions(+), 20 deletions(-) diff --git a/android/src/main/java/com/easemob/im_flutter_sdk/EMChatManagerWrapper.java b/android/src/main/java/com/easemob/im_flutter_sdk/EMChatManagerWrapper.java index 8457ee67..c8fd0d90 100644 --- a/android/src/main/java/com/easemob/im_flutter_sdk/EMChatManagerWrapper.java +++ b/android/src/main/java/com/easemob/im_flutter_sdk/EMChatManagerWrapper.java @@ -443,7 +443,7 @@ private void downloadAttachment(JSONObject param, String channelName, Result res public void onSuccess() { post(() -> { Map map = new HashMap<>(); - map.put("message", EMMessageHelper.toJson(msg)); + map.put("message", updateDownloadStatus(EMFileMessageBody.EMDownloadStatus.SUCCESSED, msg, false)); map.put("localId", msg.getMsgId()); messageChannel.invokeMethod(EMSDKMethod.onMessageSuccess, map); }); @@ -466,7 +466,7 @@ public void onError(int code, String desc) { data.put("description", desc); post(() -> { Map map = new HashMap<>(); - map.put("message", EMMessageHelper.toJson(msg)); + map.put("message", updateDownloadStatus(EMFileMessageBody.EMDownloadStatus.FAILED, msg, false)); map.put("localId", msg.getMsgId()); map.put("error", data); messageChannel.invokeMethod(EMSDKMethod.onMessageError, map); @@ -475,7 +475,7 @@ public void onError(int code, String desc) { }); asyncRunnable(() -> { EMClient.getInstance().chatManager().downloadAttachment(msg); - onSuccess(result, channelName, EMMessageHelper.toJson(msg)); + onSuccess(result, channelName, updateDownloadStatus(EMFileMessageBody.EMDownloadStatus.DOWNLOADING, msg, false)); }); } @@ -487,7 +487,7 @@ private void downloadThumbnail(JSONObject param, String channelName, Result resu public void onSuccess() { post(() -> { Map map = new HashMap<>(); - map.put("message", EMMessageHelper.toJson(msg)); + map.put("message", updateDownloadStatus(EMFileMessageBody.EMDownloadStatus.SUCCESSED, msg, true)); map.put("localId", msg.getMsgId()); messageChannel.invokeMethod(EMSDKMethod.onMessageSuccess, map); }); @@ -510,7 +510,7 @@ public void onError(int code, String desc) { data.put("description", desc); post(() -> { Map map = new HashMap<>(); - map.put("message", EMMessageHelper.toJson(msg)); + map.put("message", updateDownloadStatus(EMFileMessageBody.EMDownloadStatus.FAILED, msg, true)); map.put("localId", msg.getMsgId()); map.put("error", data); messageChannel.invokeMethod(EMSDKMethod.onMessageError, map); @@ -519,10 +519,65 @@ public void onError(int code, String desc) { }); asyncRunnable(() -> { EMClient.getInstance().chatManager().downloadThumbnail(msg); - onSuccess(result, channelName, EMMessageHelper.toJson(msg)); + onSuccess(result, channelName, updateDownloadStatus(EMFileMessageBody.EMDownloadStatus.DOWNLOADING, msg, true)); }); } + private Map updateDownloadStatus(EMFileMessageBody.EMDownloadStatus downloadStatus, EMMessage msg, boolean isThumbnail) { + boolean canUpdate = false; + switch (msg.getType()) { + case FILE: + case VOICE: { + if (isThumbnail) { + break; + } + } + case IMAGE: + case VIDEO: + { + canUpdate = true; + } + break; + default: + break; + } + if (canUpdate) { + EMMessageBody body = msg.getBody(); + if (msg.getType() == EMMessage.Type.FILE) { + EMFileMessageBody tmpBody = (EMFileMessageBody) body; + tmpBody.setDownloadStatus(downloadStatus); + body = tmpBody; + }else if (msg.getType() == EMMessage.Type.VOICE) { + EMVoiceMessageBody tmpBody = (EMVoiceMessageBody) body; + tmpBody.setDownloadStatus(downloadStatus); + body = tmpBody; + }else if (msg.getType() == EMMessage.Type.IMAGE) { + EMImageMessageBody tmpBody = (EMImageMessageBody) body; + if (isThumbnail) { + // android not support now. + // tmpBody.setThumbnailDownloadStatus(downloadStatus); + }else { + tmpBody.setDownloadStatus(downloadStatus); + } + + body = tmpBody; + }else if (msg.getType() == EMMessage.Type.VIDEO) { + EMVideoMessageBody tmpBody = (EMVideoMessageBody) body; + if (isThumbnail) { + // android not support now. + // tmpBody.setThumbnailDownloadStatus(downloadStatus); + }else { + tmpBody.setDownloadStatus(downloadStatus); + } + + body = tmpBody; + } + + msg.setBody(body); + } + return EMMessageHelper.toJson(msg); + } + private void loadAllConversations(JSONObject param, String channelName, Result result) throws JSONException { if (EMClient.getInstance().getCurrentUser() == null || EMClient.getInstance().getCurrentUser().length() == 0) { onSuccess(result, channelName, new ArrayList<>()); diff --git a/ios/Classes/EMChatManagerWrapper.m b/ios/Classes/EMChatManagerWrapper.m index ca73d533..8652d61f 100644 --- a/ios/Classes/EMChatManagerWrapper.m +++ b/ios/Classes/EMChatManagerWrapper.m @@ -16,6 +16,7 @@ #import "EMMessageReaction+Helper.h" #import "EMMessageReactionChange+Helper.h" + @interface EMChatManagerWrapper () @property (nonatomic, strong) FlutterMethodChannel *messageChannel; @@ -465,37 +466,40 @@ - (void)downloadAttachment:(NSDictionary *)param result:(FlutterResult)result { __weak typeof(self) weakSelf = self; __block EMChatMessage *msg = [EMChatMessage fromJson:param[@"message"]]; - EMChatMessage *needDownMSg = [EMClient.sharedClient.chatManager getMessageWithMessageId:msg.messageId]; - [EMClient.sharedClient.chatManager downloadMessageAttachment:needDownMSg + EMChatMessage *needDownMsg = [EMClient.sharedClient.chatManager getMessageWithMessageId:msg.messageId]; + [EMClient.sharedClient.chatManager downloadMessageAttachment:needDownMsg progress:^(int progress) { [weakSelf.messageChannel invokeMethod:ChatOnMessageProgressUpdate arguments:@{ @"progress":@(progress), - @"localId":msg.messageId + @"localId": msg.messageId }]; } completion:^(EMChatMessage *message, EMError *error) { if (error) { + NSDictionary *msgDict = [self updateDownloadStatus:EMDownloadStatusFailed message:message thumbnail:NO]; [weakSelf.messageChannel invokeMethod:ChatOnMessageError arguments:@{ @"error":[error toJson], - @"localId":msg.messageId, - @"message":[message toJson] + @"localId": msg.messageId, + @"message":msgDict }]; }else { + NSDictionary *msgDict = [self updateDownloadStatus:EMDownloadStatusSucceed message:message thumbnail:NO]; [weakSelf.messageChannel invokeMethod:ChatOnMessageSuccess arguments:@{ - @"message":[message toJson], - @"localId":msg.messageId + @"message":msgDict, + @"localId": msg.messageId }]; } }]; + NSDictionary *msgDict = [self updateDownloadStatus:EMDownloadStatusDownloading message:msg thumbnail:NO]; [weakSelf wrapperCallBack:result channelName:aChannelName error:nil - object:[msg toJson]]; + object:msgDict]; } - (void)downloadThumbnail:(NSDictionary *)param @@ -503,8 +507,8 @@ - (void)downloadThumbnail:(NSDictionary *)param result:(FlutterResult)result { __weak typeof(self) weakSelf = self; __block EMChatMessage *msg = [EMChatMessage fromJson:param[@"message"]]; - EMChatMessage *needDownMSg = [EMClient.sharedClient.chatManager getMessageWithMessageId:msg.messageId]; - [EMClient.sharedClient.chatManager downloadMessageThumbnail:needDownMSg + EMChatMessage *needDownMsg = [EMClient.sharedClient.chatManager getMessageWithMessageId:msg.messageId]; + [EMClient.sharedClient.chatManager downloadMessageThumbnail:needDownMsg progress:^(int progress) { [weakSelf.messageChannel invokeMethod:ChatOnMessageProgressUpdate @@ -515,25 +519,81 @@ - (void)downloadThumbnail:(NSDictionary *)param } completion:^(EMChatMessage *message, EMError *error) { if (error) { + NSDictionary *msgDict = [self updateDownloadStatus:EMDownloadStatusFailed message:message thumbnail:YES]; [weakSelf.messageChannel invokeMethod:ChatOnMessageError arguments:@{ @"error":[error toJson], @"localId":msg.messageId, - @"message":[message toJson] + @"message":msgDict }]; }else { + NSDictionary *msgDict = [self updateDownloadStatus:EMDownloadStatusSucceed message:message thumbnail:YES]; [weakSelf.messageChannel invokeMethod:ChatOnMessageSuccess arguments:@{ - @"message":[message toJson], + @"message":msgDict, @"localId":msg.messageId }]; } }]; - + NSDictionary *msgDict = [self updateDownloadStatus:EMDownloadStatusDownloading message:msg thumbnail:YES]; [weakSelf wrapperCallBack:result channelName:aChannelName error:nil - object:[msg toJson]]; + object:msgDict]; +} + +// 用于修改下载状态。 +- (NSDictionary *)updateDownloadStatus:(EMDownloadStatus)status + message:(EMChatMessage *)msg + thumbnail:(BOOL)isThumbnail +{ + BOOL canUpdate = NO; + switch(msg.body.type){ + case EMMessageBodyTypeFile: + case EMMessageBodyTypeVoice:{ + if(isThumbnail) { + break; + } + } + case EMMessageBodyTypeVideo: + case EMMessageBodyTypeImage:{ + canUpdate = YES; + } + break; + default: + break; + } + + if(canUpdate) { + EMMessageBody *body = msg.body; + if(msg.body.type == EMMessageBodyTypeFile) { + EMFileMessageBody *tmpBody = (EMFileMessageBody *)body; + tmpBody.downloadStatus = status; + body = tmpBody; + }else if(msg.body.type == EMMessageBodyTypeVoice) { + EMVoiceMessageBody *tmpBody = (EMVoiceMessageBody *)body; + tmpBody.downloadStatus = status; + body = tmpBody; + }else if(msg.body.type == EMMessageBodyTypeImage) { + EMImageMessageBody *tmpBody = (EMImageMessageBody *)body; + if(isThumbnail) { + tmpBody.thumbnailDownloadStatus = status; + }else { + tmpBody.downloadStatus = status; + } + body = tmpBody; + }else if(msg.body.type == EMMessageBodyTypeVideo) { + EMVideoMessageBody *tmpBody = (EMVideoMessageBody *)body; + if(isThumbnail) { + tmpBody.thumbnailDownloadStatus = status; + }else { + tmpBody.downloadStatus = status; + } + body = tmpBody; + } + msg.body = body; + } + return [msg toJson] ; } - (void)loadAllConversations:(NSDictionary *)param @@ -619,6 +679,7 @@ - (void)fetchHistoryMessages:(NSDictionary *)param }]; } + - (void)fetchGroupReadAck:(NSDictionary *)param channelName:(NSString *)aChannelName result:(FlutterResult) result {