use of net.iGap.module.structs.StructMessageInfo in project iGap-Android by KianIranian-STDG.
the class FragmentChat method sendMessage.
/**
* *************************** Messaging ***************************
*/
private void sendMessage(int requestCode, String filePath) {
String message;
if (getWrittenMessage().length() > Config.MAX_TEXT_ATTACHMENT_LENGTH)
message = getWrittenMessage().substring(0, Config.MAX_TEXT_ATTACHMENT_LENGTH);
else
message = getWrittenMessage();
String mainMessage = getWrittenMessage();
if (filePath == null || (filePath.length() == 0 && requestCode != AttachFile.request_code_contact_phone)) {
clearReplyView();
return;
}
showPopup(-1);
if (isShowLayoutUnreadMessage) {
removeLayoutUnreadMessage();
}
long messageId = AppUtils.makeRandomId();
final long updateTime = TimeUtils.currentLocalTime();
ProtoGlobal.RoomMessageType messageType = null;
String fileName;
long duration = 0;
long fileSize;
int[] imageDimens = { 0, 0 };
final long senderID = AccountManager.getInstance().getCurrentUser().getId();
/**
* check if path is uri detect real path from uri
*/
String path = getFilePathFromUri(Uri.parse(filePath));
if (path != null) {
filePath = path;
}
if (requestCode == AttachFile.requestOpenGalleryForVideoMultipleSelect && filePath.toLowerCase().endsWith(".gif")) {
requestCode = AttachFile.requestOpenGalleryForImageMultipleSelect;
}
fileName = new File(filePath).getName();
fileSize = new File(filePath).length();
RealmRoomMessage roomMessage = new RealmRoomMessage();
StructMessageInfo structMessageInfoNew = new StructMessageInfo(roomMessage);
switch(requestCode) {
case IntentRequests.REQ_CROP:
case AttachFile.requestOpenGalleryForImageMultipleSelect:
if (!filePath.toLowerCase().endsWith(".gif")) {
if (isMessageWrote()) {
messageType = IMAGE_TEXT;
} else {
messageType = ProtoGlobal.RoomMessageType.IMAGE;
}
} else {
if (isMessageWrote()) {
messageType = GIF_TEXT;
} else {
messageType = ProtoGlobal.RoomMessageType.GIF;
}
}
imageDimens = AndroidUtils.getImageDimens(filePath);
break;
case AttachFile.request_code_TAKE_PICTURE:
if (AndroidUtils.getImageDimens(filePath)[0] == 0 && AndroidUtils.getImageDimens(filePath)[1] == 0) {
G.handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "Picture Not Loaded", Toast.LENGTH_SHORT).show();
}
});
return;
}
imageDimens = AndroidUtils.getImageDimens(filePath);
if (isMessageWrote()) {
messageType = IMAGE_TEXT;
} else {
messageType = ProtoGlobal.RoomMessageType.IMAGE;
}
break;
case AttachFile.requestOpenGalleryForVideoMultipleSelect:
case request_code_VIDEO_CAPTURED:
// mainVideoPath
duration = AndroidUtils.getAudioDuration(G.fragmentActivity, filePath) / 1000;
if (isMessageWrote()) {
messageType = VIDEO_TEXT;
} else {
messageType = VIDEO;
}
break;
case AttachFile.request_code_pic_audi:
duration = AndroidUtils.getAudioDuration(G.fragmentActivity, filePath) / 1000;
if (isMessageWrote()) {
messageType = ProtoGlobal.RoomMessageType.AUDIO_TEXT;
} else {
messageType = ProtoGlobal.RoomMessageType.AUDIO;
}
String songArtist = AndroidUtils.getAudioArtistName(filePath);
long songDuration = AndroidUtils.getAudioDuration(G.fragmentActivity, filePath);
structMessageInfoNew.setSongArtist(songArtist);
structMessageInfoNew.setSongLength(songDuration);
break;
case AttachFile.request_code_pic_file:
case AttachFile.request_code_open_document:
if (isMessageWrote()) {
messageType = ProtoGlobal.RoomMessageType.FILE_TEXT;
} else {
messageType = ProtoGlobal.RoomMessageType.FILE;
}
break;
case AttachFile.request_code_contact_phone:
if (latestUri == null) {
break;
}
messageType = CONTACT;
ContactUtils contactUtils = new ContactUtils(G.fragmentActivity, latestUri);
String name = contactUtils.retrieveName();
String number = contactUtils.retrieveNumber();
structMessageInfoNew.setContactValues(name, "", number);
break;
case AttachFile.request_code_paint:
imageDimens = AndroidUtils.getImageDimens(filePath);
if (isMessageWrote()) {
messageType = IMAGE_TEXT;
} else {
messageType = ProtoGlobal.RoomMessageType.IMAGE;
}
break;
}
final ProtoGlobal.RoomMessageType finalMessageType = messageType;
final String finalFilePath = filePath;
final String finalFileName = fileName;
final long finalDuration = duration;
final long finalFileSize = fileSize;
final int[] finalImageDimens = imageDimens;
roomMessage.setMessageId(messageId);
roomMessage.setMessageType(finalMessageType);
roomMessage.setMessage(message);
DbManager.getInstance().doRealmTask(realm -> {
RealmRoomMessage.addTimeIfNeed(roomMessage, realm);
});
RealmRoomMessage.isEmojiInText(roomMessage, message);
roomMessage.setStatus(ProtoGlobal.RoomMessageStatus.SENDING.toString());
roomMessage.setRoomId(mRoomId);
RealmAttachment realmAttachment = new RealmAttachment();
realmAttachment.setId(messageId);
realmAttachment.setLocalFilePath(finalFilePath);
realmAttachment.setWidth(finalImageDimens[0]);
realmAttachment.setHeight(finalImageDimens[1]);
realmAttachment.setSize(finalFileSize);
realmAttachment.setName(finalFileName);
realmAttachment.setDuration(finalDuration);
if (messageType != CONTACT) {
roomMessage.setAttachment(realmAttachment);
}
roomMessage.setUserId(senderID);
roomMessage.setAuthorHash(RealmUserInfo.getCurrentUserAuthorHash());
roomMessage.setShowMessage(true);
roomMessage.setCreateTime(updateTime);
if (isReply()) {
MessageObject replyLayoutObject = (MessageObject) mReplayLayout.getTag();
RealmRoomMessage replyMessage = new RealmRoomMessage();
replyMessage.setUserId(replyLayoutObject.userId);
replyMessage.setUpdateTime(replyLayoutObject.updateTime);
replyMessage.setStatusVersion(replyLayoutObject.statusVersion);
replyMessage.setShowTime(replyLayoutObject.needToShow);
replyMessage.setRoomId(replyLayoutObject.roomId);
replyMessage.setPreviousMessageId(replyLayoutObject.previousMessageId);
replyMessage.setFutureMessageId(replyLayoutObject.futureMessageId);
replyMessage.setMessageId(replyLayoutObject.id);
replyMessage.setEdited(replyLayoutObject.edited);
replyMessage.setDeleted(replyLayoutObject.deleted);
replyMessage.setCreateTime(replyLayoutObject.createTime);
replyMessage.setMessage(replyLayoutObject.message);
replyMessage.setMessageType(ProtoGlobal.RoomMessageType.forNumber(replyLayoutObject.messageType));
replyMessage.setStatus(ProtoGlobal.RoomMessageStatus.forNumber(replyLayoutObject.status).toString());
if (replyLayoutObject.getAttachment() != null) {
AttachmentObject attachmentObject = replyLayoutObject.getAttachment();
RealmAttachment replyToAttachment = new RealmAttachment();
replyToAttachment.setId(replyLayoutObject.id);
replyToAttachment.setLocalFilePath(attachmentObject.filePath);
replyToAttachment.setWidth(attachmentObject.width);
replyToAttachment.setHeight(attachmentObject.height);
replyToAttachment.setSize(attachmentObject.size);
replyToAttachment.setName(attachmentObject.name);
replyToAttachment.setDuration(attachmentObject.duration);
replyMessage.setAttachment(replyToAttachment);
}
// TODO: 1/13/21 MESSAGE_REFACTOR
roomMessage.setReplyTo(replyMessage);
}
long replyMessageId = 0;
if (roomMessage.getReplyTo() != null) {
if (roomMessage.getReplyTo().getMessageId() < 0) {
replyMessageId = roomMessage.getReplyTo().getMessageId() * (-1);
} else {
replyMessageId = roomMessage.getReplyTo().getMessageId();
}
}
if (chatType == CHANNEL) {
RealmChannelExtra realmChannelExtra = new RealmChannelExtra();
realmChannelExtra.setMessageId(messageId);
if (RealmRoom.showSignature(mRoomId)) {
realmChannelExtra.setSignature(AccountManager.getInstance().getCurrentUser().getName());
} else {
realmChannelExtra.setSignature("");
}
realmChannelExtra.setThumbsUp("0");
realmChannelExtra.setThumbsDown("0");
realmChannelExtra.setViewsLabel("1");
roomMessage.setChannelExtra(realmChannelExtra);
}
if (finalMessageType == CONTACT) {
if (latestUri != null) {
ContactUtils contactUtils = new ContactUtils(G.fragmentActivity, latestUri);
String name = contactUtils.retrieveName();
String number = contactUtils.retrieveNumber();
RealmRoomMessageContact realmRoomMessageContact = new RealmRoomMessageContact();
realmRoomMessageContact.setId(AppUtils.makeRandomId());
realmRoomMessageContact.setFirstName(name);
realmRoomMessageContact.setLastName("");
RealmList<RealmString> listString = new RealmList<>();
RealmString phoneRealmStr = new RealmString();
phoneRealmStr.setString(number);
listString.add(phoneRealmStr);
realmRoomMessageContact.setPhones(listString);
roomMessage.setRoomMessageContact(realmRoomMessageContact);
}
}
String makeThumbnailFilePath = "";
if (finalMessageType == VIDEO || finalMessageType == VIDEO_TEXT) {
// mainVideoPath
makeThumbnailFilePath = finalFilePath;
}
if (finalMessageType == VIDEO || finalMessageType == VIDEO_TEXT) {
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(makeThumbnailFilePath, MediaStore.Video.Thumbnails.MINI_KIND);
if (bitmap != null) {
roomMessage.getAttachment().setLocalThumbnailPath(AndroidUtils.saveBitmap(bitmap));
roomMessage.getAttachment().setWidth(bitmap.getWidth());
roomMessage.getAttachment().setHeight(bitmap.getHeight());
}
}
new Thread(() -> {
DbManager.getInstance().doRealmTransaction(realm1 -> {
RealmRoom room = realm1.where(RealmRoom.class).equalTo("id", mRoomId).findFirst();
if (room != null) {
room.setDeleted(false);
}
RealmRoom.setLastMessageWithRoomMessage(realm1, roomMessage.getRoomId(), realm1.copyToRealmOrUpdate(roomMessage));
});
}).start();
if (finalMessageType == CONTACT) {
ChatSendMessageUtil messageUtil = getSendMessageUtil().newBuilder(chatType, finalMessageType, mRoomId).message(message);
messageUtil.contact(structMessageInfoNew.realmRoomMessage.getRoomMessageContact().getFirstName(), structMessageInfoNew.realmRoomMessage.getRoomMessageContact().getLastName(), structMessageInfoNew.realmRoomMessage.getRoomMessageContact().getPhones().first().getString());
if (isReply()) {
messageUtil.replyMessage(replyMessageId);
}
messageUtil.sendMessage(Long.toString(messageId));
}
if (isReply()) {
mReplayLayout.setTag(null);
G.handler.post(new Runnable() {
@Override
public void run() {
mReplayLayout.setVisibility(View.GONE);
}
});
}
G.handler.post(new Runnable() {
@Override
public void run() {
switchAddItem(new ArrayList<>(Collections.singletonList(structMessageInfoNew)), false);
if (mainMessage.length() > message.length()) {
sendNewMessage(mainMessage.substring(message.length()));
}
}
});
G.handler.postDelayed(new Runnable() {
@Override
public void run() {
scrollToEnd();
}
}, 100);
}
use of net.iGap.module.structs.StructMessageInfo in project iGap-Android by KianIranian-STDG.
the class FragmentChat method getOnlineMessage.
/**
* get message history from server
*
* @param oldMessageId if set oldMessageId=0 messages will be get from latest message that exist in server
*/
private void getOnlineMessage(final long oldMessageId, long documentId, final ProtoClientGetRoomHistory.ClientGetRoomHistory.Direction direction) {
if ((direction == UP && !isWaitingForHistoryUp && allowGetHistoryUp) || (direction == DOWN && !isWaitingForHistoryDown && allowGetHistoryDown)) {
/**
* show progress when start for get history from server
*/
progressItem(SHOW, direction);
if (!getRequestManager().isUserLogin()) {
getOnlineMessageAfterTimeOut(oldMessageId, documentId, direction);
return;
}
long reachMessageId;
if (direction == UP) {
reachMessageId = reachMessageIdUp;
isWaitingForHistoryUp = true;
} else {
reachMessageId = reachMessageIdDown;
isWaitingForHistoryDown = true;
}
String requestId = DbManager.getInstance().doRealmTask(realm -> {
int limit = Config.LIMIT_GET_HISTORY_NORMAL;
if ((firstUp && direction == UP) || (firstDown && direction == DOWN)) {
limit = Config.LIMIT_GET_HISTORY_LOW;
}
return MessageLoader.getOnlineMessage(realm, mRoomId, documentId, oldMessageId, reachMessageId, limit, direction, new OnMessageReceive() {
@Override
public void onMessage(final long roomId, long startMessageId, long startDocumentId, long endMessageId, long endDocumentId, List<RealmRoomMessage> realmRoomMessages, boolean gapReached, boolean jumpOverLocal, ProtoClientGetRoomHistory.ClientGetRoomHistory.Direction direction) {
if (roomId != mRoomId) {
return;
}
hideProgress();
/**
* hide progress received history
*/
progressItem(HIDE, direction);
if (direction == UP) {
firstUp = false;
startFutureMessageIdUp = startMessageId;
documentIdUp = startDocumentId;
isWaitingForHistoryUp = false;
} else {
firstDown = false;
startFutureMessageIdDown = endMessageId;
documentIdDown = endDocumentId;
isWaitingForHistoryDown = false;
}
/**
* I do this for set addToView true
*/
if (direction == DOWN && realmRoomMessages.size() < (Config.LIMIT_GET_HISTORY_NORMAL - 1)) {
getOnlineMessage(startFutureMessageIdDown, documentIdDown, direction);
}
/**
* when reached to gap and not jumped over local, set gapMessageIdUp = 0; do this action
* means that gap not exist (need this value for future get message) set topMore/bottomMore
* local after that gap reached true for allow that get message from
*/
if (gapReached && !jumpOverLocal) {
if (direction == UP) {
gapMessageIdUp = 0;
reachMessageIdUp = 0;
topMore = true;
} else {
gapMessageIdDown = 0;
reachMessageIdDown = 0;
bottomMore = true;
}
gapDetection(realmRoomMessages, direction);
} else if ((direction == UP && isReachedToTopView()) || direction == DOWN && isReachedToBottomView()) {
/**
* check this changeState because if user is near to top view and not scroll get top message from server
*/
// getOnlineMessage(startFutureMessageId, directionEnum);
}
final ArrayList<StructMessageInfo> structMessageInfos = new ArrayList<>();
for (RealmRoomMessage realmRoomMessage : realmRoomMessages) {
// suggestion: may be could remove db query in constructor of StructMessageInfo and add query in load item in adapter with cache
structMessageInfos.add(new StructMessageInfo(realmRoomMessage));
}
switchAddItem(structMessageInfos, direction == UP);
}
@Override
public void onError(int majorCode, int minorCode, long messageIdGetHistory, long documentIdGetHistory, ProtoClientGetRoomHistory.ClientGetRoomHistory.Direction direction) {
hideProgress();
/**
* hide progress if have any error
*/
progressItem(HIDE, direction);
if (majorCode == 617) {
if (!isWaitingForHistoryUp && !isWaitingForHistoryDown && mAdapter.getItemCount() == 0) {
G.handler.post(new Runnable() {
@Override
public void run() {
}
});
}
if (direction == UP) {
isWaitingForHistoryUp = false;
allowGetHistoryUp = false;
G.handler.post(new Runnable() {
@Override
public void run() {
// TODO [Saeed Mozaffari] [2017-03-06 9:50 AM] - for avoid from 'Inconsistency detected. Invalid item position' error i set notifyDataSetChanged. Find Solution And Clear it!!!
mAdapter.notifyDataSetChanged();
}
});
} else {
addToView = true;
isWaitingForHistoryDown = false;
allowGetHistoryDown = false;
}
}
/**
* if time out came up try again for get history with previous value
*/
if (majorCode == 5) {
if (direction == UP) {
isWaitingForHistoryUp = false;
} else {
isWaitingForHistoryDown = false;
}
}
G.handler.post(new Runnable() {
@Override
public void run() {
if (majorCode == 5) {
getOnlineMessageAfterTimeOut(messageIdGetHistory, documentIdGetHistory, direction);
}
}
});
}
});
});
if (direction == UP) {
lastRandomRequestIdUp = requestId;
} else {
lastRandomRequestIdDown = requestId;
}
}
}
use of net.iGap.module.structs.StructMessageInfo in project iGap-Android by KianIranian-STDG.
the class FragmentChat method getMessages.
private void getMessages() {
DbManager.getInstance().doRealmTask(realm -> {
ProtoClientGetRoomHistory.ClientGetRoomHistory.Direction direction;
ArrayList<StructMessageInfo> messageInfos = new ArrayList<>();
RealmResults<RealmRoomMessage> results;
RealmResults<RealmRoomMessage> resultsDown = null;
RealmResults<RealmRoomMessage> resultsUp;
// with this value realm will be queried for get message
long fetchMessageId = 0;
if (hasUnread() || hasSavedState()) {
RealmRoomMessage firstUnreadMessage = null;
RealmRoom room = realm.where(RealmRoom.class).equalTo("id", mRoomId).findFirst();
if (room != null) {
room = realm.copyFromRealm(room);
firstUnreadMessage = room.getFirstUnreadMessage();
}
if (hasSavedState()) {
fetchMessageId = getSavedState();
documentIdUp = getSavedDocumentIdState();
if (hasUnread()) {
if (firstUnreadMessage == null) {
resetMessagingValue();
getMessages();
return;
}
}
} else {
if (firstUnreadMessage == null) {
resetMessagingValue();
getMessages();
return;
}
G.runOnUiThread(this::unreadLayoutMessage);
fetchMessageId = firstUnreadMessage.getMessageId();
documentIdUp = firstUnreadMessage.getDocumentId();
}
if (hasUnread()) {
countNewMessage = unreadCount;
G.runOnUiThread(() -> {
txtNewUnreadMessage.setVisibility(View.VISIBLE);
txtNewUnreadMessage.getTextView().setText(String.valueOf(countNewMessage));
});
}
startFutureMessageIdUp = fetchMessageId;
// we have firstUnreadMessage but for gapDetection method we need RealmResult so get this message with query; if we change gap detection method will be can use from firstUnreadMessage
resultsDown = realm.where(RealmRoomMessage.class).equalTo("roomId", mRoomId).notEqualTo("createTime", 0).equalTo("deleted", false).equalTo("showMessage", true).equalTo("messageId", fetchMessageId).findAll();
addToView = false;
direction = DOWN;
} else {
addToView = true;
direction = UP;
}
Number latestMessageId = realm.where(RealmRoomMessage.class).equalTo("roomId", mRoomId).notEqualTo("createTime", 0).equalTo("deleted", false).equalTo("showMessage", true).max("messageId");
if (latestMessageId != null) {
resultsUp = realm.where(RealmRoomMessage.class).equalTo("roomId", mRoomId).equalTo("messageId", latestMessageId.longValue()).findAll();
} else {
resultsUp = realm.where(RealmRoomMessage.class).equalTo("roomId", -100).findAll();
}
long gapMessageId;
if (direction == DOWN) {
RealmQuery realmQuery = realm.where(RealmRoomMessage.class).equalTo("roomId", mRoomId).lessThanOrEqualTo("messageId", fetchMessageId).notEqualTo("createTime", 0).equalTo("deleted", false).equalTo("showMessage", true);
if (realmQuery.count() > 1) {
resultsUp = realm.where(RealmRoomMessage.class).equalTo("roomId", mRoomId).equalTo("messageId", fetchMessageId).notEqualTo("createTime", 0).equalTo("deleted", false).equalTo("showMessage", true).findAll();
gapDetection(resultsUp, UP);
} else {
getOnlineMessage(fetchMessageId, documentIdUp, UP);
}
results = resultsDown;
gapMessageId = gapDetection(results, direction);
} else {
results = resultsUp;
gapMessageId = gapDetection(resultsUp, UP);
}
if (results.size() > 0) {
Object[] object = MessageLoader.getLocalMessage(realm, mRoomId, results.first().getMessageId(), gapMessageId, true, direction);
messageInfos = (ArrayList<StructMessageInfo>) object[0];
if (messageInfos.size() > 0) {
if (direction == UP) {
topMore = (boolean) object[1];
startFutureMessageIdUp = messageInfos.get(messageInfos.size() - 1).realmRoomMessage.getMessageId();
documentIdUp = messageInfos.get(messageInfos.size() - 1).realmRoomMessage.getDocumentId();
} else {
bottomMore = (boolean) object[1];
startFutureMessageIdDown = messageInfos.get(messageInfos.size() - 1).realmRoomMessage.getMessageId();
documentIdDown = messageInfos.get(messageInfos.size() - 1).realmRoomMessage.getDocumentId();
}
} else {
if (direction == UP) {
startFutureMessageIdUp = 0;
documentIdUp = 0;
} else {
startFutureMessageIdDown = 0;
documentIdDown = 0;
}
}
if (gapMessageId > 0) {
boolean hasSpaceToGap = (boolean) object[2];
if (!hasSpaceToGap) {
long oldMessageId = 0;
long documentId = 0;
if (messageInfos.size() > 0) {
oldMessageId = messageInfos.get(messageInfos.size() - 1).realmRoomMessage.getMessageId();
documentId = messageInfos.get(messageInfos.size() - 1).realmRoomMessage.getDocumentId();
}
getOnlineMessage(oldMessageId, documentId, direction);
}
} else {
if ((direction == UP && !topMore) || (direction == DOWN && !bottomMore)) {
if (messageInfos.size() > 0) {
getOnlineMessage(messageInfos.get(messageInfos.size() - 1).realmRoomMessage.getMessageId(), messageInfos.get(messageInfos.size() - 1).realmRoomMessage.getDocumentId(), direction);
} else {
getOnlineMessage(0, 0, direction);
}
}
}
} else {
long oldMessageId = 0;
long documentId = 0;
if (direction == DOWN) {
RealmRoomMessage realmRoomMessage = realm.where(RealmRoomMessage.class).equalTo("roomId", mRoomId).notEqualTo("createTime", 0).equalTo("showMessage", true).equalTo("messageId", fetchMessageId).findFirst();
if (realmRoomMessage != null) {
oldMessageId = realmRoomMessage.getMessageId();
documentId = realmRoomMessage.getDocumentId();
}
}
getOnlineMessage(oldMessageId, documentId, direction);
}
final ArrayList<StructMessageInfo> finalMessageInfos = messageInfos;
G.runOnUiThread(new Runnable() {
@Override
public void run() {
if (direction == UP) {
switchAddItem(finalMessageInfos, true);
} else {
switchAddItem(finalMessageInfos, false);
if (hasSavedState()) {
if (messageId != 0) {
if (goToPositionWithAnimation(savedScrollMessageId, 1000)) {
savedScrollMessageId = 0;
savedScrollDocumentId = 0;
}
} else {
int position = mAdapter.findPositionByMessageId(savedScrollMessageId);
LinearLayoutManager linearLayout = (LinearLayoutManager) recyclerView.getLayoutManager();
linearLayout.scrollToPositionWithOffset(position, firstVisiblePositionOffset);
savedScrollMessageId = 0;
savedScrollDocumentId = 0;
}
}
}
scrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
int firstVisiblePosition = linearLayoutManager.findFirstVisibleItemPosition();
View view = linearLayoutManager.getChildAt(0);
if (firstVisiblePosition > 0 && view != null) {
firstVisiblePositionOffset = view.getTop();
}
visibleItemCount = linearLayoutManager.getChildCount();
totalItemCount = linearLayoutManager.getItemCount();
if (firstVisiblePosition < scrollEnd) {
loadMessage(UP);
if (totalItemCount <= scrollEnd) {
loadMessage(DOWN);
}
} else if (firstVisiblePosition + visibleItemCount >= (totalItemCount - scrollEnd)) {
loadMessage(DOWN);
}
}
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
};
// TODO: 2/14/21 Should write a better logic to loadMessage
recyclerView.addOnScrollListener(scrollListener);
if (unreadCount > 0 && allowScrollToTop) {
recyclerView.scrollToPosition(0);
}
allowScrollToTop = false;
}
});
});
}
use of net.iGap.module.structs.StructMessageInfo in project iGap-Android by KianIranian-STDG.
the class MessageLoader method getLocalMessage.
/**
* fetch local message from RealmRoomMessage.
* (hint : deleted message doesn't count)
*
* @param roomId roomId that want show message for that
* @param messageId start query with this messageId
* @param duplicateMessage if set true return message for messageId that used in this method (will be used "lessThanOrEqualTo") otherwise just return less or greater than messageId(will be used "lessThan" method)
* @param direction direction for load message up or down
* @return Object[] ==> [0] -> ArrayList<StructMessageInfo>, [1] -> boolean hasMore, [2] -> boolean hasGap
*/
public static Object[] getLocalMessage(Realm realm, long roomId, long messageId, long gapMessageId, boolean duplicateMessage, ProtoClientGetRoomHistory.ClientGetRoomHistory.Direction direction) {
boolean hasMore = true;
boolean hasSpaceToGap = true;
List<RealmRoomMessage> realmRoomMessages;
ArrayList<StructMessageInfo> structMessageInfos = new ArrayList<>();
if (messageId == 0) {
return new Object[] { structMessageInfos, false, false };
}
/**
* get message from RealmRoomMessage
*/
if (gapMessageId > 0) {
if (direction == UP) {
if (duplicateMessage) {
realmRoomMessages = realm.where(RealmRoomMessage.class).equalTo("roomId", roomId).lessThanOrEqualTo("messageId", messageId).notEqualTo("deleted", true).between("messageId", gapMessageId, messageId).findAll().sort("messageId", Sort.DESCENDING);
} else {
realmRoomMessages = realm.where(RealmRoomMessage.class).equalTo("roomId", roomId).lessThan("messageId", messageId).notEqualTo("deleted", true).between("messageId", gapMessageId, messageId).findAll().sort("messageId", Sort.DESCENDING);
}
} else {
if (duplicateMessage) {
realmRoomMessages = realm.where(RealmRoomMessage.class).equalTo("roomId", roomId).greaterThanOrEqualTo("messageId", messageId).notEqualTo("deleted", true).between("messageId", messageId, gapMessageId).findAll().sort("messageId", Sort.ASCENDING);
} else {
realmRoomMessages = realm.where(RealmRoomMessage.class).equalTo("roomId", roomId).greaterThan("messageId", messageId).notEqualTo("deleted", true).between("messageId", messageId, gapMessageId).findAll().sort("messageId", Sort.ASCENDING);
}
}
} else {
if (direction == UP) {
if (duplicateMessage) {
realmRoomMessages = realm.where(RealmRoomMessage.class).equalTo("roomId", roomId).lessThanOrEqualTo("messageId", messageId).notEqualTo("deleted", true).findAll().sort("messageId", Sort.DESCENDING);
} else {
realmRoomMessages = realm.where(RealmRoomMessage.class).equalTo("roomId", roomId).lessThan("messageId", messageId).notEqualTo("deleted", true).findAll().sort("messageId", Sort.DESCENDING);
}
} else {
if (duplicateMessage) {
realmRoomMessages = realm.where(RealmRoomMessage.class).equalTo("roomId", roomId).greaterThanOrEqualTo("messageId", messageId).notEqualTo("deleted", true).findAll().sort("messageId", Sort.ASCENDING);
} else {
realmRoomMessages = realm.where(RealmRoomMessage.class).equalTo("roomId", roomId).greaterThan("messageId", messageId).notEqualTo("deleted", true).findAll().sort("messageId", Sort.ASCENDING);
}
}
}
/**
* manage subList
*/
if (realmRoomMessages.size() > LOCAL_LIMIT) {
realmRoomMessages = realmRoomMessages.subList(0, LOCAL_LIMIT);
} else {
/**
* when run this block means that end of message reached
* and should be send request to server for get history
*/
hasMore = false;
hasSpaceToGap = false;
realmRoomMessages = realmRoomMessages.subList(0, realmRoomMessages.size());
}
/**
* convert message from RealmRoomMessage to StructMessageInfo for send to view
*/
for (RealmRoomMessage realmRoomMessage : realmRoomMessages) {
if (realmRoomMessage.getMessageId() != 0) {
structMessageInfos.add(new StructMessageInfo(realmRoomMessage));
}
}
return new Object[] { structMessageInfos, hasMore, hasSpaceToGap };
}
Aggregations