10 changed files with 120 additions and 267 deletions
@ -0,0 +1,47 @@
@@ -0,0 +1,47 @@
|
||||
package com.kanglai.push.util; |
||||
|
||||
import com.blankj.utilcode.util.CacheDiskUtils; |
||||
import com.blankj.utilcode.util.MapUtils; |
||||
import com.google.gson.Gson; |
||||
import com.kanglai.push.constant.CacheConstant; |
||||
import com.kanglai.push.entity.LocalPushChatMsg; |
||||
import com.kanglai.push.entity.User; |
||||
|
||||
import java.util.Map; |
||||
import java.util.Objects; |
||||
|
||||
import lombok.experimental.UtilityClass; |
||||
|
||||
@UtilityClass |
||||
public class HistoryMsgUtil { |
||||
|
||||
private User user = CacheDiskUtils.getInstance().getParcelable(CacheConstant.USER_INFO, User.CREATOR, new User()); |
||||
|
||||
public Map<String, LocalPushChatMsg> select() { |
||||
String localHistoryMsgJson = CacheDiskUtils.getInstance().getString(user.getId().concat(user.getUserType()) ,""); |
||||
return (Map<String, LocalPushChatMsg>) new Gson().fromJson(localHistoryMsgJson, Map.class); |
||||
} |
||||
|
||||
public void put(String key,LocalPushChatMsg value) { |
||||
Objects.requireNonNull(key); |
||||
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()); |
||||
localHistoryMsg.put(key, value); |
||||
String newLocalHistoryMsgJson = new Gson().toJson(localHistoryMsg); |
||||
CacheDiskUtils.getInstance().put(user.getId().concat(user.getUserType()), newLocalHistoryMsgJson); |
||||
} |
||||
|
||||
public void del(String key){ |
||||
Objects.requireNonNull(key); |
||||
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()); |
||||
localHistoryMsg.remove(key); |
||||
String newLocalHistoryMsgJson = new Gson().toJson(localHistoryMsg); |
||||
CacheDiskUtils.getInstance().put(user.getId().concat(user.getUserType()), newLocalHistoryMsgJson); |
||||
} |
||||
|
||||
public void clean() { |
||||
CacheDiskUtils.getInstance().remove(user.getId().concat(user.getUserType())); |
||||
} |
||||
|
||||
} |
@ -1,117 +0,0 @@
@@ -1,117 +0,0 @@
|
||||
package com.kanglai.push.util; |
||||
|
||||
import android.text.TextUtils; |
||||
|
||||
import com.blankj.utilcode.util.CacheDiskUtils; |
||||
import com.blankj.utilcode.util.CollectionUtils; |
||||
import com.blankj.utilcode.util.LogUtils; |
||||
import com.google.gson.Gson; |
||||
import com.google.gson.reflect.TypeToken; |
||||
import com.kanglai.push.constant.CacheConstant; |
||||
import com.kanglai.push.entity.LocalPushChatMsg; |
||||
import com.kanglai.push.entity.User; |
||||
|
||||
import java.lang.reflect.Type; |
||||
import java.util.ArrayList; |
||||
import java.util.Iterator; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 本地聊天数据缓存工具类 |
||||
* |
||||
* 本地聊天记录缓存方式 |
||||
* CacheDiskUtils.getInstance().getString(user.getId()+"_MESSAGE_STORAGE",""); -获取 |
||||
* CacheDiskUtils.getInstance().put(user.getId()+"_MESSAGE_STORAGE", newArr); -存储 |
||||
* |
||||
* @Author: liusixiang007 |
||||
* @since: 2023/6/26 |
||||
*/ |
||||
public class LocalPushChatMsgUtil { |
||||
|
||||
/** |
||||
* 获取本地缓存ArrayList |
||||
* 并进行自检 id 是否为空 |
||||
* @return |
||||
*/ |
||||
public static ArrayList<LocalPushChatMsg> getLocalData() { |
||||
ArrayList<LocalPushChatMsg> data = new ArrayList<>(); |
||||
User user = CacheDiskUtils.getInstance().getParcelable(CacheConstant.USER_INFO, User.CREATOR, new User()); |
||||
Type listType = new TypeToken<List<LocalPushChatMsg>>(){}.getType(); // 定义接收结果类型
|
||||
String cachedJsonArrayStr = CacheDiskUtils.getInstance().getString(user.getId()+"_MESSAGE_STORAGE",""); |
||||
data = (ArrayList<LocalPushChatMsg>) new Gson().fromJson(cachedJsonArrayStr, listType); |
||||
if (data != null && !CollectionUtils.isEmpty(data)) { |
||||
Iterator<LocalPushChatMsg> iterator = data.iterator(); // 自检 使用迭代器的remove来删除
|
||||
while (iterator.hasNext()){ |
||||
LocalPushChatMsg item = iterator.next(); |
||||
if (item.getId() == null || TextUtils.isEmpty(item.getId())) { |
||||
iterator.remove(); |
||||
} |
||||
} |
||||
} |
||||
return data; |
||||
} |
||||
|
||||
/** |
||||
* 清除指定用户的缓存数据(添加之前需要限制id不能为空) |
||||
* @param id |
||||
*/ |
||||
public static void clearAppointData(String id){ |
||||
ArrayList<LocalPushChatMsg> data = getLocalData(); |
||||
if (data != null && !CollectionUtils.isEmpty(data)) { |
||||
Iterator<LocalPushChatMsg> iterator = data.iterator(); |
||||
while (iterator.hasNext()){ |
||||
LocalPushChatMsg item = iterator.next(); |
||||
/*获取的时候已经做过自检了 这里就不需要做非空判断了*/ |
||||
if (id.equals(item.getId())) { |
||||
iterator.remove(); |
||||
updateLocalData(data); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 更新用户缓存数据 |
||||
* @param data |
||||
*/ |
||||
public static void updateLocalData(ArrayList<LocalPushChatMsg> data) { |
||||
User user = CacheDiskUtils.getInstance().getParcelable(CacheConstant.USER_INFO, User.CREATOR, new User()); |
||||
String newArr = new Gson().toJson(data); |
||||
CacheDiskUtils.getInstance().put(user.getId()+"_MESSAGE_STORAGE", newArr); |
||||
LogUtils.i(user.getNickName()+"_MESSAGE_STORAGE 的缓存数据已更新"); |
||||
} |
||||
|
||||
/** |
||||
* 储存最新的一条数据 |
||||
* @param newDate |
||||
*/ |
||||
public static void addNewLocalData(LocalPushChatMsg newDate){ |
||||
User user = CacheDiskUtils.getInstance().getParcelable(CacheConstant.USER_INFO, User.CREATOR, new User()); |
||||
ArrayList<LocalPushChatMsg> data = new ArrayList<>(); |
||||
data.add(newDate); |
||||
LogUtils.d(data); |
||||
String newArr = new Gson().toJson(data); |
||||
CacheDiskUtils.getInstance().put(user.getId()+"_MESSAGE_STORAGE", newArr); |
||||
LogUtils.i(user.getNickName()+"_MESSAGE_STORAGE 的缓存数据已更新"); |
||||
} |
||||
|
||||
/** |
||||
* 遍历本地存储列表 出现相同id就删除旧数据 插入新的 没有就直接插入 |
||||
* |
||||
* |
||||
* @param newData 新的单条数据 |
||||
* @param oldList 老的存储数据列表(使用之前确保该列表不为空) |
||||
*/ |
||||
public static void updateLocalDatas(LocalPushChatMsg newData, ArrayList<LocalPushChatMsg> oldList) { |
||||
User user = CacheDiskUtils.getInstance().getParcelable(CacheConstant.USER_INFO, User.CREATOR, new User()); |
||||
Iterator<LocalPushChatMsg> iterator = oldList.iterator(); |
||||
while (iterator.hasNext()){ |
||||
LocalPushChatMsg item = iterator.next(); |
||||
if (newData.getId().equals(item.getId())) iterator.remove(); |
||||
} |
||||
if (CollectionUtils.isEmpty(oldList)) oldList = new ArrayList<>(); // 防止数据为空时数据插入异常
|
||||
oldList.add(newData); |
||||
CacheDiskUtils.getInstance().put(user.getId()+"_MESSAGE_STORAGE", oldList); |
||||
LogUtils.i(user.getNickName()+"_MESSAGE_STORAGE 的缓存数据已更新--",oldList); |
||||
} |
||||
} |
Loading…
Reference in new issue