use of com.fanap.podchat.cachemodel.GapMessageVO in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method findAndUpdateGaps.
private void findAndUpdateGaps(List<MessageVO> newMessagesFromServer, long threadId) {
Runnable jobFindAndInsertGap = () -> {
if (newMessagesFromServer.size() == 0)
return;
MessageVO lastMessage = newMessagesFromServer.get(newMessagesFromServer.size() - 1);
if (lastMessage.getPreviousId() == 0)
return;
List<CacheMessageVO> messages = messageDatabaseHelper.getMessageById(lastMessage.getPreviousId());
if (Util.isNullOrEmpty(messages)) {
GapMessageVO gapMessageVO = new GapMessageVO();
gapMessageVO.setId(lastMessage.getId());
gapMessageVO.setPreviousId(lastMessage.getPreviousId());
gapMessageVO.setThreadId(threadId);
gapMessageVO.setTime(lastMessage.getTime());
gapMessageVO.setUniqueId(lastMessage.getUniqueId());
messageDatabaseHelper.insertGap(gapMessageVO);
lastMessage.setHasGap(true);
dataSource.updateMessage(lastMessage, threadId);
// messageDatabaseHelper.updateMessage(lastMessage, threadId);
}
};
Runnable jobUpdateGaps = () -> {
List<GapMessageVO> gaps = messageDatabaseHelper.getAllGaps(threadId);
if (!Util.isNullOrEmpty(gaps)) {
Map<Long, Long> msgIdAndPreviousId = new HashMap<>();
for (GapMessageVO gapMessage : gaps) {
msgIdAndPreviousId.put(gapMessage.getPreviousId(), gapMessage.getId());
}
for (MessageVO newMessage : newMessagesFromServer) {
if (msgIdAndPreviousId.containsKey(newMessage.getId())) {
// delete gap that produced by this message
messageDatabaseHelper.deleteGapForMessageId(msgIdAndPreviousId.get(newMessage.getId()));
// set message gap field to false
messageDatabaseHelper.updateMessageGapState(msgIdAndPreviousId.get(newMessage.getId()), false);
}
}
}
};
PodThreadManager podThreadManager = new PodThreadManager();
podThreadManager.addTask(jobFindAndInsertGap);
podThreadManager.addTask(jobUpdateGaps);
podThreadManager.runTasksSynced();
}
Aggregations