基于android的手机聊天app,基于环信的Android聊天APP(二)

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 {

//消息为空

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值