Skip to content

Commit

Permalink
fix down status
Browse files Browse the repository at this point in the history
  • Loading branch information
dujiepeng committed May 17, 2023
1 parent 1490a69 commit 2806361
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ private void downloadAttachment(JSONObject param, String channelName, Result res
public void onSuccess() {
post(() -> {
Map<String, Object> 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);
});
Expand All @@ -466,7 +466,7 @@ public void onError(int code, String desc) {
data.put("description", desc);
post(() -> {
Map<String, Object> 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);
Expand All @@ -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));
});
}

Expand All @@ -487,7 +487,7 @@ private void downloadThumbnail(JSONObject param, String channelName, Result resu
public void onSuccess() {
post(() -> {
Map<String, Object> 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);
});
Expand All @@ -510,7 +510,7 @@ public void onError(int code, String desc) {
data.put("description", desc);
post(() -> {
Map<String, Object> 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);
Expand All @@ -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<String, Object> 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<>());
Expand Down
89 changes: 75 additions & 14 deletions ios/Classes/EMChatManagerWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#import "EMMessageReaction+Helper.h"
#import "EMMessageReactionChange+Helper.h"


@interface EMChatManagerWrapper () <EMChatManagerDelegate>
@property (nonatomic, strong) FlutterMethodChannel *messageChannel;

Expand Down Expand Up @@ -465,46 +466,49 @@ - (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
channelName:(NSString *)aChannelName
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
Expand All @@ -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
Expand Down Expand Up @@ -619,6 +679,7 @@ - (void)fetchHistoryMessages:(NSDictionary *)param
}];
}


- (void)fetchGroupReadAck:(NSDictionary *)param
channelName:(NSString *)aChannelName
result:(FlutterResult) result {
Expand Down

0 comments on commit 2806361

Please sign in to comment.