|
|
@ -1,6 +1,10 @@ |
|
|
|
package com.kanglai.push.ui.vm; |
|
|
|
package com.kanglai.push.ui.vm; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.app.Activity; |
|
|
|
import android.app.Application; |
|
|
|
import android.app.Application; |
|
|
|
|
|
|
|
import android.content.Intent; |
|
|
|
|
|
|
|
import android.icu.text.SimpleDateFormat; |
|
|
|
|
|
|
|
import android.icu.util.Calendar; |
|
|
|
import android.os.Handler; |
|
|
|
import android.os.Handler; |
|
|
|
import android.os.Looper; |
|
|
|
import android.os.Looper; |
|
|
|
import android.util.Pair; |
|
|
|
import android.util.Pair; |
|
|
@ -22,6 +26,7 @@ import com.dolphin.core.util.RxUtil; |
|
|
|
import com.dolphin.core.util.ToastUtil; |
|
|
|
import com.dolphin.core.util.ToastUtil; |
|
|
|
import com.kanglai.push.app.AppApplication; |
|
|
|
import com.kanglai.push.app.AppApplication; |
|
|
|
import com.kanglai.push.constant.CacheConstant; |
|
|
|
import com.kanglai.push.constant.CacheConstant; |
|
|
|
|
|
|
|
import com.kanglai.push.constant.CommonConstant; |
|
|
|
import com.kanglai.push.di.component.DaggerServiceComponent; |
|
|
|
import com.kanglai.push.di.component.DaggerServiceComponent; |
|
|
|
import com.kanglai.push.entity.LocalPushChatMsg; |
|
|
|
import com.kanglai.push.entity.LocalPushChatMsg; |
|
|
|
import com.kanglai.push.entity.MsgVo; |
|
|
|
import com.kanglai.push.entity.MsgVo; |
|
|
@ -30,10 +35,12 @@ import com.kanglai.push.entity.User; |
|
|
|
import com.kanglai.push.service.PushService; |
|
|
|
import com.kanglai.push.service.PushService; |
|
|
|
import com.kanglai.push.ui.activity.InstantActivity; |
|
|
|
import com.kanglai.push.ui.activity.InstantActivity; |
|
|
|
import com.kanglai.push.util.LocalPushChatMsgUtil; |
|
|
|
import com.kanglai.push.util.LocalPushChatMsgUtil; |
|
|
|
|
|
|
|
import com.luck.picture.lib.config.PictureConfig; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import java.util.Locale; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
import javax.inject.Inject; |
|
|
|
import javax.inject.Inject; |
|
|
@ -56,11 +63,7 @@ public class InstantViewModel extends ToolbarViewModel<InstantActivity>{ |
|
|
|
|
|
|
|
|
|
|
|
public int current = 1; |
|
|
|
public int current = 1; |
|
|
|
|
|
|
|
|
|
|
|
public Boolean isNew = false; // 是否接收到新消息
|
|
|
|
public String cursor = ""; |
|
|
|
|
|
|
|
|
|
|
|
public Boolean sendBtnType = true; // 发送按钮状态事件
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String cursor = ""; // 记录游标
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ArrayList<LocalPushChatMsg> localPushChatMsgs; // 本地缓存的列表数据
|
|
|
|
public ArrayList<LocalPushChatMsg> localPushChatMsgs; // 本地缓存的列表数据
|
|
|
|
|
|
|
|
|
|
|
@ -79,7 +82,7 @@ public class InstantViewModel extends ToolbarViewModel<InstantActivity>{ |
|
|
|
public void onCreate(@NonNull LifecycleOwner owner) { |
|
|
|
public void onCreate(@NonNull LifecycleOwner owner) { |
|
|
|
super.onCreate(owner); |
|
|
|
super.onCreate(owner); |
|
|
|
super.setRightIconVisible(View.VISIBLE); |
|
|
|
super.setRightIconVisible(View.VISIBLE); |
|
|
|
localPushChatMsgs = LocalPushChatMsgUtil.getLocalData(); // 获取本地缓存的历史记录
|
|
|
|
localPushChatMsgs = LocalPushChatMsgUtil.getLocalData(); |
|
|
|
} |
|
|
|
} |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void rightIconOnClick() { |
|
|
|
protected void rightIconOnClick() { |
|
|
@ -106,20 +109,12 @@ public class InstantViewModel extends ToolbarViewModel<InstantActivity>{ |
|
|
|
ToastUtil.showBottom("定时任务"); |
|
|
|
ToastUtil.showBottom("定时任务"); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 输入框发送消息 |
|
|
|
|
|
|
|
* text - 内容、tit - 标题、alias-接收方id、userId-发送方id |
|
|
|
|
|
|
|
* 禁止重复多次点击 - 只有当新消息完全加载完成之后才能 再一次发送 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public BindingCommand sendMsgClickCommand = new BindingCommand(() ->{ |
|
|
|
public BindingCommand sendMsgClickCommand = new BindingCommand(() ->{ |
|
|
|
if (!sendBtnType) return; |
|
|
|
|
|
|
|
String msg = String.valueOf(mActivity.input_box.getText()); |
|
|
|
String msg = String.valueOf(mActivity.input_box.getText()); |
|
|
|
if (StringUtils.isTrimEmpty(msg)) { |
|
|
|
if (StringUtils.isTrimEmpty(msg)) { |
|
|
|
ToastUtil.show("不能发送空白信息"); |
|
|
|
ToastUtil.show("不能发送空白信息"); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
if (isNew) isNew = false; // 防止请求失败也更新了最新的缓存数据
|
|
|
|
|
|
|
|
if (sendBtnType) sendBtnType = false; // 防止新消息加载出来前用户的重复点击
|
|
|
|
|
|
|
|
PushChatMessage chatMessage = new PushChatMessage(); |
|
|
|
PushChatMessage chatMessage = new PushChatMessage(); |
|
|
|
if ("1".equals(adverseData.getFocusType())) chatMessage.setConcernFanStatus("1"); // 从关注列表中进入聊天页界面则需要做特殊处理
|
|
|
|
if ("1".equals(adverseData.getFocusType())) chatMessage.setConcernFanStatus("1"); // 从关注列表中进入聊天页界面则需要做特殊处理
|
|
|
|
chatMessage.setText(msg); |
|
|
|
chatMessage.setText(msg); |
|
|
@ -136,8 +131,16 @@ public class InstantViewModel extends ToolbarViewModel<InstantActivity>{ |
|
|
|
public void onNext(ResultResponse R) { |
|
|
|
public void onNext(ResultResponse R) { |
|
|
|
if (R.getCode() == R.SUCCESS) { |
|
|
|
if (R.getCode() == R.SUCCESS) { |
|
|
|
mActivity.input_box.setText(""); |
|
|
|
mActivity.input_box.setText(""); |
|
|
|
isNew = true; // 发送了新的消息 需要更新到本地缓存中
|
|
|
|
PushChatMessage chatMessage = new PushChatMessage(); |
|
|
|
loadNewDate(); // 加载最新的一条数据
|
|
|
|
chatMessage.setUserId(user.getId()); |
|
|
|
|
|
|
|
chatMessage.setText(msg); |
|
|
|
|
|
|
|
Calendar calendar = Calendar.getInstance(); |
|
|
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); |
|
|
|
|
|
|
|
String currentTime = sdf.format(calendar.getTime()); |
|
|
|
|
|
|
|
chatMessage.setCreateTime(currentTime); |
|
|
|
|
|
|
|
mActivity.mAdapter.append(chatMessage); |
|
|
|
|
|
|
|
mActivity.setActivitieResult(); |
|
|
|
|
|
|
|
|
|
|
|
} else ToastUtil.showTop(R.getMsg()); |
|
|
|
} else ToastUtil.showTop(R.getMsg()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -146,17 +149,16 @@ public class InstantViewModel extends ToolbarViewModel<InstantActivity>{ |
|
|
|
if ("1".equals(adverseData.getFocusType())) { |
|
|
|
if ("1".equals(adverseData.getFocusType())) { |
|
|
|
ToastUtil.show("对方还未关注你,你只能发送一条消息,发送失败"); |
|
|
|
ToastUtil.show("对方还未关注你,你只能发送一条消息,发送失败"); |
|
|
|
}else ExceptionHandle.baseExceptionMsg(e); |
|
|
|
}else ExceptionHandle.baseExceptionMsg(e); |
|
|
|
sendBtnType = true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onComplete() { |
|
|
|
public void onComplete() { |
|
|
|
mActivity.hideKeyboard(); // 发送成功之后隐藏键盘
|
|
|
|
mActivity.hideKeyboard(); |
|
|
|
|
|
|
|
mActivity.mRecyclerView.scrollToPosition(mActivity.mAdapter.getItemCount()-1); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
/** 进入聊天页 初始化数据 */ |
|
|
|
|
|
|
|
public void init() { |
|
|
|
public void init() { |
|
|
|
pushService.listFriendHistoryMessage( |
|
|
|
pushService.listFriendHistoryMessage( |
|
|
|
MapUtils.newHashMap( |
|
|
|
MapUtils.newHashMap( |
|
|
@ -196,56 +198,6 @@ public class InstantViewModel extends ToolbarViewModel<InstantActivity>{ |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** 加载最新的一条数据 */ |
|
|
|
|
|
|
|
public void loadNewDate() { |
|
|
|
|
|
|
|
pushService.listFriendHistoryMessage( |
|
|
|
|
|
|
|
MapUtils.newHashMap( |
|
|
|
|
|
|
|
Pair.create("size", 1), |
|
|
|
|
|
|
|
Pair.create("cursor", ""), |
|
|
|
|
|
|
|
Pair.create("current", 1), |
|
|
|
|
|
|
|
Pair.create("alias", adverseData.getId()), |
|
|
|
|
|
|
|
Pair.create("userId", user.getId()) |
|
|
|
|
|
|
|
)) |
|
|
|
|
|
|
|
.compose(RxUtil.schedulersTransformer()) |
|
|
|
|
|
|
|
.compose(RxUtil.exceptionTransformer()) |
|
|
|
|
|
|
|
.doOnSubscribe(this) |
|
|
|
|
|
|
|
.subscribe(new DisposableObserver<ResultResponse<Map<String, Object>>>() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onNext(ResultResponse<Map<String, Object>> R) { |
|
|
|
|
|
|
|
if (R.getCode() == R.SUCCESS ) { |
|
|
|
|
|
|
|
MsgVo msgVo = (MsgVo) R.getData(); |
|
|
|
|
|
|
|
List<PushChatMessage> data = msgVo.getData(); |
|
|
|
|
|
|
|
if (CollectionUtils.isEmpty(data)) { |
|
|
|
|
|
|
|
ToastUtil.showBottomWarn("操作移除,退出后重试"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (CollectionUtils.isEmpty(requestList)) { // 本地没有聊天记录
|
|
|
|
|
|
|
|
cursor = msgVo.getCursor(); |
|
|
|
|
|
|
|
requestList.add(data.get(0)); |
|
|
|
|
|
|
|
mActivity.adapterInit(requestList); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!data.get(0).getId().equals( requestList.get(requestList.size()-1 ).getId()) ) { |
|
|
|
|
|
|
|
requestList.add(requestList.size(), data.get(0)); // 确定为新消息才将数据插入数组末尾
|
|
|
|
|
|
|
|
mActivity.mAdapter.refreshLastDate(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}else ToastUtil.showBottomWarn(R.getMsg()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onError(Throwable e) { |
|
|
|
|
|
|
|
ExceptionHandle.baseExceptionMsg(e); |
|
|
|
|
|
|
|
sendBtnType = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onComplete() { |
|
|
|
|
|
|
|
if (requestList.size() >= 8) dropDownView(); |
|
|
|
|
|
|
|
sendBtnType = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void loadMore() { // 加载更多数据
|
|
|
|
public void loadMore() { // 加载更多数据
|
|
|
|
if (CollectionUtils.isEmpty(requestList)) { // 如果此时的列表为空 则重新初始化
|
|
|
|
if (CollectionUtils.isEmpty(requestList)) { // 如果此时的列表为空 则重新初始化
|
|
|
|
init(); |
|
|
|
init(); |
|
|
@ -264,7 +216,7 @@ public class InstantViewModel extends ToolbarViewModel<InstantActivity>{ |
|
|
|
.subscribe(new DisposableObserver<ResultResponse<Map<String, Object>>>() { |
|
|
|
.subscribe(new DisposableObserver<ResultResponse<Map<String, Object>>>() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onNext(ResultResponse<Map<String, Object>> R) { |
|
|
|
public void onNext(ResultResponse<Map<String, Object>> R) { |
|
|
|
if (R.getCode() == R.SUCCESS ) { |
|
|
|
if (R.getCode() == R.SUCCESS) { |
|
|
|
MsgVo msgVo = (MsgVo) R.getData(); |
|
|
|
MsgVo msgVo = (MsgVo) R.getData(); |
|
|
|
cursor = msgVo.getCursor(); |
|
|
|
cursor = msgVo.getCursor(); |
|
|
|
List<PushChatMessage> data = msgVo.getData(); |
|
|
|
List<PushChatMessage> data = msgVo.getData(); |
|
|
@ -304,40 +256,4 @@ public class InstantViewModel extends ToolbarViewModel<InstantActivity>{ |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** 将回收视图滚动到底部 */ |
|
|
|
|
|
|
|
public void dropDownView() { // 延时将回收视图下拉到底部
|
|
|
|
|
|
|
|
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void run() { |
|
|
|
|
|
|
|
mActivity.mRecyclerView.scrollToPosition(requestList.size()-1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, 1000); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onStop(@NonNull LifecycleOwner owner) { |
|
|
|
|
|
|
|
super.onStop(owner); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void finish() { // 用户点击返回
|
|
|
|
|
|
|
|
super.finish(); |
|
|
|
|
|
|
|
if (isNew) { |
|
|
|
|
|
|
|
LocalPushChatMsg newLocalData = new LocalPushChatMsg(adverseData.getId(), adverseData.getUserName(), |
|
|
|
|
|
|
|
adverseData.getUserType(), adverseData.getAvatar(), requestList.get(requestList.size() - 1).getCreateTime(), |
|
|
|
|
|
|
|
requestList.get(requestList.size() - 1).getCreateTime(), "0"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// todo 如果是主动发送了消息 则需要更新本地缓存
|
|
|
|
|
|
|
|
if (CollectionUtils.isEmpty(localPushChatMsgs)){ // 没有本地缓存
|
|
|
|
|
|
|
|
LocalPushChatMsgUtil.addNewLocalData(newLocalData); |
|
|
|
|
|
|
|
requestList.clear(); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// /** 如果有数据就在工具类中处理数据 */
|
|
|
|
|
|
|
|
// LocalPushChatMsgUtil.updateLocalDatas(newLocalData, localPushChatMsgs);
|
|
|
|
|
|
|
|
// requestList.clear();
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|