LiuSiXiang-007 1 year ago
parent
commit
b783472472
  1. 3
      app/src/main/java/com/kanglai/push/ui/fragment/MessageFragment.java
  2. 11
      app/src/main/java/com/kanglai/push/util/HistoryMsgUtil.java

3
app/src/main/java/com/kanglai/push/ui/fragment/MessageFragment.java

@ -21,6 +21,7 @@ import androidx.recyclerview.widget.RecyclerView;
import com.blankj.utilcode.util.CacheDiskUtils; import com.blankj.utilcode.util.CacheDiskUtils;
import com.blankj.utilcode.util.CollectionUtils; import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.Utils; import com.blankj.utilcode.util.Utils;
import com.dolphin.core.base.BaseFragment; import com.dolphin.core.base.BaseFragment;
import com.dolphin.core.bus.RxBus; import com.dolphin.core.bus.RxBus;
@ -87,7 +88,7 @@ public class MessageFragment extends BaseFragment<FragmentMessageBinding, Messag
.compose(RxUtil.exceptionTransformer()) .compose(RxUtil.exceptionTransformer())
.subscribe(msg -> { .subscribe(msg -> {
PushChatMessage pushChatMessage = (PushChatMessage) msg; PushChatMessage pushChatMessage = (PushChatMessage) msg;
if(!TextUtils.isEmpty(pushChatMessage.getFanUserId())) { if(!TextUtils.isEmpty(pushChatMessage.getFanUserId()) && !StringUtils.equals(pushChatMessage.getFanUserId(), user.getId())) {
mViewModel.addLocalHistoryMsg(pushChatMessage.getFanUserId(), pushChatMessage.getText()); mViewModel.addLocalHistoryMsg(pushChatMessage.getFanUserId(), pushChatMessage.getText());
} }
}); });

11
app/src/main/java/com/kanglai/push/util/HistoryMsgUtil.java

@ -3,10 +3,12 @@ package com.kanglai.push.util;
import com.blankj.utilcode.util.CacheDiskUtils; import com.blankj.utilcode.util.CacheDiskUtils;
import com.blankj.utilcode.util.MapUtils; import com.blankj.utilcode.util.MapUtils;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kanglai.push.constant.CacheConstant; import com.kanglai.push.constant.CacheConstant;
import com.kanglai.push.entity.LocalPushChatMsg; import com.kanglai.push.entity.LocalPushChatMsg;
import com.kanglai.push.entity.User; import com.kanglai.push.entity.User;
import java.lang.reflect.Type;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -19,13 +21,15 @@ public class HistoryMsgUtil {
public Map<String, LocalPushChatMsg> select() { public Map<String, LocalPushChatMsg> select() {
String localHistoryMsgJson = CacheDiskUtils.getInstance().getString(user.getId().concat(user.getUserType()) ,""); String localHistoryMsgJson = CacheDiskUtils.getInstance().getString(user.getId().concat(user.getUserType()) ,"");
return (Map<String, LocalPushChatMsg>) new Gson().fromJson(localHistoryMsgJson, Map.class); Type type = new TypeToken<Map<String, LocalPushChatMsg>>(){}.getType();
return new Gson().fromJson(localHistoryMsgJson, type);
} }
public void put(String key,LocalPushChatMsg value) { public void put(String key,LocalPushChatMsg value) {
Objects.requireNonNull(key); Objects.requireNonNull(key);
String localHistoryMsgJson = CacheDiskUtils.getInstance().getString(user.getId().concat(user.getUserType()) ,""); String localHistoryMsgJson = CacheDiskUtils.getInstance().getString(user.getId().concat(user.getUserType()) ,"");
Map<String, LocalPushChatMsg> localHistoryMsg = Objects.requireNonNullElse((Map<String, LocalPushChatMsg>) new Gson().fromJson(localHistoryMsgJson, Map.class), MapUtils.newHashMap()); Type type = new TypeToken<Map<String, LocalPushChatMsg>>(){}.getType();
Map<String, LocalPushChatMsg> localHistoryMsg = Objects.requireNonNullElse(new Gson().fromJson(localHistoryMsgJson, type), MapUtils.newHashMap());
localHistoryMsg.put(key, value); localHistoryMsg.put(key, value);
String newLocalHistoryMsgJson = new Gson().toJson(localHistoryMsg); String newLocalHistoryMsgJson = new Gson().toJson(localHistoryMsg);
CacheDiskUtils.getInstance().put(user.getId().concat(user.getUserType()), newLocalHistoryMsgJson); CacheDiskUtils.getInstance().put(user.getId().concat(user.getUserType()), newLocalHistoryMsgJson);
@ -34,7 +38,8 @@ public class HistoryMsgUtil {
public void del(String key){ public void del(String key){
Objects.requireNonNull(key); Objects.requireNonNull(key);
String localHistoryMsgJson = CacheDiskUtils.getInstance().getString(user.getId().concat(user.getUserType()) ,""); String localHistoryMsgJson = CacheDiskUtils.getInstance().getString(user.getId().concat(user.getUserType()) ,"");
Map<String, LocalPushChatMsg> localHistoryMsg = Objects.requireNonNullElse((Map<String, LocalPushChatMsg>) new Gson().fromJson(localHistoryMsgJson, Map.class), MapUtils.newHashMap()); Type type = new TypeToken<Map<String, LocalPushChatMsg>>(){}.getType();
Map<String, LocalPushChatMsg> localHistoryMsg = Objects.requireNonNullElse(new Gson().fromJson(localHistoryMsgJson, type), MapUtils.newHashMap());
localHistoryMsg.remove(key); localHistoryMsg.remove(key);
String newLocalHistoryMsgJson = new Gson().toJson(localHistoryMsg); String newLocalHistoryMsgJson = new Gson().toJson(localHistoryMsg);
CacheDiskUtils.getInstance().put(user.getId().concat(user.getUserType()), newLocalHistoryMsgJson); CacheDiskUtils.getInstance().put(user.getId().concat(user.getUserType()), newLocalHistoryMsgJson);

Loading…
Cancel
Save