import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import com.fanyafeng.wechat.util.StringUtil;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMImageMessageBody;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import java.util.ArrayList;
import java.util.List;
/**
* Author: fanyafeng
* Data: 17/2/21 上午11:29
* Email: [email protected]
*/
public class ChatPresenter implements ChatContract.Presenter, EMMessageListener {
private Context context;
private ChatContract.View chatView;
private Intent intent;
private EMMessageListener emMessageListener;
private List< ChatBean> chatBeanList = new ArrayList<>();
public ChatPresenter(Context context, ChatContract.View chatView) {
this.context = context;
this.chatView = chatView;
}
@Override
public void start() {
emMessageListener = this;
EMClient.getInstance().chatManager().addMessageListener(emMessageListener);
intent = chatView.initIntent();
EMConversation emConversation = EMClient.getInstance().chatManager().getConversation(intent.getStringExtra("from"));
if (emConversation != null) {
List< EMMessage> emMessageList = emConversation.getAllMessages();
for (EMMessage emMessage : emMessageList) {
ChatBean chatBean = new ChatBean();
if (emMessage.getBody() instanceof EMTextMessageBody) {
chatBean.setTextMessage(((EMTextMessageBody) emMessage.getBody()).getMessage());
} else if (emMessage.getBody() instanceof EMImageMessageBody) {
chatBean.setTextMessage("[图片]");
}
chatBean.setType(EMClient.getInstance().getCurrentUser().equals(emMessage.getFrom()) ? 1 : 0);
chatBeanList.add(chatBean);
}
}
chatView.initRecycleView(chatBeanList);
if (chatBeanList.size() > 0) {
chatView.scrollToPosition(chatBeanList.size() - 1);
}
}
@Override
public void onMessageReceived(List< EMMessage> list) {
for (EMMessage emMessage : list) {
ChatBean chatBean = new ChatBean();
if (emMessage.getBody() instanceof EMTextMessageBody) {
chatBean.setTextMessage(((EMTextMessageBody) emMessage.getBody()).getMessage());
}
if (intent.getStringExtra("from").equals(emMessage.getFrom())) {
chatBeanList.add(chatBean);
}
}
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
chatView.notifyChanged();
chatView.scrollToPosition(chatBeanList.size() - 1);
}
});
}
@Override
public void onCmdMessageReceived(List< EMMessage> list) {
}
@Override
public void onMessageRead(List< EMMessage> list) {
}
@Override
public void onMessageDelivered(List< EMMessage> list) {
}
@Override
public void onMessageChanged(EMMessage emMessage, Object o) {
}
@Override
public void removeMessageListener() {
EMClient.getInstance().chatManager().removeMessageListener(emMessageListener);
}
@Override
public void sendMessage() {
if (!StringUtil.isNullOrEmpty(chatView.getMessage())) {
EMMessage emMessage = EMMessage.createTxtSendMessage(chatView.getMessage(), chatView.initIntent().getStringExtra("from"));
EMClient.getInstance().chatManager().sendMessage(emMessage);
ChatBean chatBean = new ChatBean();
chatBean.setTextMessage(chatView.getMessage());
chatBean.setType(1);
chatBeanList.add(chatBean);
chatView.notifyChanged();
chatView.scrollToPosition(chatBeanList.size() - 1);
chatView.clearInput();
} else {
//消息为空
}
}
}