Search in sources :

Example 11 with RealmRoomMessage

use of net.iGap.realm.RealmRoomMessage in project iGap-Android by KianIranian-STDG.

the class ChatSendMessageUtil method onRoomMessageReceive.

private void onRoomMessageReceive(long roomId, ProtoGlobal.RoomMessage roomMessage, ProtoGlobal.Room.Type roomType) {
    DbManager.getInstance().doRealmTransaction(realm -> {
        RealmRoom room = realm.where(RealmRoom.class).equalTo("id", roomId).findFirst();
        final RealmRoomMessage realmRoomMessage = realm.where(RealmRoomMessage.class).equalTo("messageId", roomMessage.getMessageId()).findFirst();
        if (room != null && realmRoomMessage != null) {
            /**
             * client checked  (room.getUnreadCount() <= 1)  because in HelperMessageResponse unreadCount++
             */
            if (room.getUnreadCount() <= 1) {
                realmRoomMessage.setFutureMessageId(realmRoomMessage.getMessageId());
            }
        }
    });
    /**
     * don't send update status for own message
     */
    if (roomMessage.getAuthor().getUser() != null && roomMessage.getAuthor().getUser().getUserId() != AccountManager.getInstance().getCurrentUser().getId()) {
        if (roomType == ProtoGlobal.Room.Type.CHAT) {
            getMessageController().sendUpdateStatus(roomType.getNumber(), roomId, roomMessage.getMessageId(), roomMessage.getDocumentId(), ProtoGlobal.RoomMessageStatus.DELIVERED_VALUE);
        } else if (roomType == ProtoGlobal.Room.Type.GROUP && roomMessage.getStatus() == ProtoGlobal.RoomMessageStatus.SENT) {
            getMessageController().sendUpdateStatus(roomType.getNumber(), roomId, roomMessage.getMessageId(), roomMessage.getDocumentId(), ProtoGlobal.RoomMessageStatus.DELIVERED_VALUE);
        }
    }
}
Also used : RealmRoom(net.iGap.realm.RealmRoom) RealmRoomMessage(net.iGap.realm.RealmRoomMessage)

Example 12 with RealmRoomMessage

use of net.iGap.realm.RealmRoomMessage in project iGap-Android by KianIranian-STDG.

the class ActivityPopUpNotification method sendMessage.

public static void sendMessage(final String message, final long mRoomId, ProtoGlobal.Room.Type chatType) {
    RealmRoomMessage roomMessage = RealmRoomMessage.makeTextMessage(mRoomId, message);
    HelperRealm.copyOrUpdateToRealm(roomMessage);
    ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).newBuilder(chatType, ProtoGlobal.RoomMessageType.TEXT, mRoomId).message(message).sendMessage(roomMessage.getMessageId() + "");
}
Also used : RealmRoomMessage(net.iGap.realm.RealmRoomMessage)

Example 13 with RealmRoomMessage

use of net.iGap.realm.RealmRoomMessage in project iGap-Android by KianIranian-STDG.

the class UploadManager method uploadMessageAndSend.

public void uploadMessageAndSend(ProtoGlobal.Room.Type roomType, RealmRoomMessage message, boolean ignoreCompress) {
    Log.d("bagi", "uploadMessageAndSend" + message.getMessageId());
    if (message.isManaged()) {
        uploadMessageAndSend(roomType, DbManager.getInstance().doRealmTask(realm -> {
            return realm.copyFromRealm(message);
        }), ignoreCompress);
        return;
    }
    if (message.getForwardMessage() != null || message.getAttachment() == null || message.getMessageType() == ProtoGlobal.RoomMessageType.STICKER || message.getMessageType() == ProtoGlobal.RoomMessageType.CONTACT) {
        MessageObject uploadMessage = MessageObject.create(message);
        ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).build(roomType, message.getRoomId(), uploadMessage);
        return;
    }
    Log.d("bagi", "uploadMessageAndSend222");
    String savePathVideoCompress = G.DIR_TEMP + "/VIDEO_" + message.getMessageId() + ".mp4";
    File compressFile = new File(savePathVideoCompress);
    File CompletedCompressFile = new File(savePathVideoCompress.replace(".mp4", "_finish.mp4"));
    if (!CompletedCompressFile.exists() && !ignoreCompress && (message.getMessageType() == ProtoGlobal.RoomMessageType.VIDEO || message.getMessageType() == ProtoGlobal.RoomMessageType.VIDEO_TEXT)) {
        if (pendingCompressTasks.containsKey(message.getMessageId() + ""))
            return;
        Log.d("bagi", "uploadMessageAndSend33");
        CompressTask compressTask = new CompressTask(message.getMessageId() + "", message.getAttachment().getLocalFilePath(), savePathVideoCompress, new OnCompress() {

            @Override
            public void onCompressProgress(String id, int percent) {
                Log.d("bagi", "onCompressProgress" + percent);
                G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.ON_UPLOAD_COMPRESS, id, percent));
            }

            @Override
            public void onCompressFinish(String id, boolean compress, boolean isCancel) {
                Log.d("bagi", "onCompressFinish" + message.getMessageId());
                if (compress && compressFile.exists() && compressFile.length() < (new File(message.getAttachment().getLocalFilePath())).length()) {
                    compressFile.renameTo(CompletedCompressFile);
                    message.attachment.size = CompletedCompressFile.length();
                    G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.ON_UPLOAD_COMPRESS, id, 100, CompletedCompressFile.length()));
                    uploadMessageAndSend(roomType, message, ignoreCompress);
                } else {
                    if (compressFile.exists()) {
                        compressFile.delete();
                    }
                    G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.ON_UPLOAD_COMPRESS, id, 100));
                    uploadMessageAndSend(roomType, message, true);
                }
            }
        });
        if (compressFile.exists()) {
            compressFile.delete();
        }
        pendingCompressTasks.put(message.getMessageId() + "", compressTask);
        mThreadPoolExecutor.execute(compressTask);
        return;
    }
    CompressTask compressTask = pendingCompressTasks.remove(message.getMessageId() + "");
    if ((message.getMessageType() == ProtoGlobal.RoomMessageType.VIDEO || message.getMessageType() == ProtoGlobal.RoomMessageType.VIDEO_TEXT) && !ignoreCompress && (compressTask == null || !CompletedCompressFile.exists()))
        return;
    Log.d("bagi", "after Compress");
    OnUploadListener onUploadListener = new OnUploadListener() {

        @Override
        public void onProgress(String id, int progress) {
            Log.d("bagi", progress + "uploadMessageAndSend2");
            G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.ON_UPLOAD_PROGRESS, id, progress, message.attachment.size));
        }

        @Override
        public void onFinish(String id, String token) {
            Log.d("bagi", "uploadMessageAndSendonFinish");
            if (CompletedCompressFile.exists()) {
                CompletedCompressFile.delete();
            }
            HelperSetAction.sendCancel(message.getMessageId());
            DbManager.getInstance().doRealmTransaction(realm -> {
                RealmAttachment.updateToken(message.getMessageId(), token);
            });
            /**
             * this code should exist in under of other codes in this block
             */
            if (message.getReplyTo() == null) {
                ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).newBuilder(roomType, message.getMessageType(), message.getRoomId()).attachment(token).message(message.getMessage()).sendMessage(message.getMessageId() + "");
            } else {
                ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).newBuilder(roomType, message.getMessageType(), message.getRoomId()).replyMessage(message.getReplyTo().getMessageId()).attachment(token).message(message.getMessage()).sendMessage(message.getMessageId() + "");
            }
            pendingUploadTasks.remove(id);
        }

        @Override
        public void onError(String id) {
            Log.d("bagi", "uploadMessageAndSendError");
            pendingUploadTasks.remove(id);
            HelperSetAction.sendCancel(Long.parseLong(id));
            makeFailed(id);
        }
    };
    UploadTask uploadTask;
    if (CompletedCompressFile.exists()) {
        uploadTask = new UploadTask(message, CompletedCompressFile.getAbsolutePath(), onUploadListener);
    } else {
        uploadTask = new UploadTask(message, onUploadListener);
    }
    if (!pendingUploadTasks.containsKey(uploadTask.identity)) {
        pendingUploadTasks.put(uploadTask.identity, uploadTask);
        mThreadPoolExecutor.execute(uploadTask);
        HelperSetAction.setActionFiles(message.getRoomId(), message.getMessageId(), HelperSetAction.getAction(message.getMessageType()), roomType);
        G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.ON_UPLOAD_PROGRESS, message.getMessageId() + "", 1));
    }
}
Also used : OnCompress(net.igap.video.compress.OnCompress) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) MessageObject(net.iGap.structs.MessageObject) AccountManager(net.iGap.module.accountManager.AccountManager) ChatSendMessageUtil(net.iGap.module.ChatSendMessageUtil) File(java.io.File) G(net.iGap.G) TimeUnit(java.util.concurrent.TimeUnit) HelperSetAction(net.iGap.helper.HelperSetAction) SHP_SETTING(net.iGap.module.SHP_SETTING) Looper(android.os.Looper) MODE_PRIVATE(android.content.Context.MODE_PRIVATE) ProtoGlobal(net.iGap.proto.ProtoGlobal) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) RealmRoomMessage(net.iGap.realm.RealmRoomMessage) EventManager(net.iGap.observers.eventbus.EventManager) DbManager(net.iGap.module.accountManager.DbManager) Log(android.util.Log) RealmAttachment(net.iGap.realm.RealmAttachment) ArrayMap(androidx.collection.ArrayMap) OnCompress(net.igap.video.compress.OnCompress) MessageObject(net.iGap.structs.MessageObject) File(java.io.File)

Example 14 with RealmRoomMessage

use of net.iGap.realm.RealmRoomMessage in project iGap-Android by KianIranian-STDG.

the class SearchFragment method fillMessages.

private void fillMessages(String text) {
    DbManager.getInstance().doRealmTask(realm -> {
        final RealmResults<RealmRoomMessage> results = realm.where(RealmRoomMessage.class).contains("message", text, Case.INSENSITIVE).equalTo("deleted", false).isNotEmpty("message").findAll();
        if (results != null && results.size() > 0) {
            addHeader(G.fragmentActivity.getResources().getString(R.string.messages));
            for (RealmRoomMessage roomMessage : results) {
                StructSearch item = new StructSearch();
                item.time = roomMessage.getUpdateTime();
                item.comment = roomMessage.getMessage();
                item.id = roomMessage.getRoomId();
                item.type = SearchType.message;
                item.messageId = roomMessage.getMessageId();
                RealmRoom realmRoom = realm.where(RealmRoom.class).equalTo("id", roomMessage.getRoomId()).findFirst();
                if (realmRoom != null) {
                    // room exist
                    item.name = realmRoom.getTitle();
                    item.initials = realmRoom.getInitials();
                    item.color = realmRoom.getColor();
                    item.roomType = realmRoom.getType();
                    item.avatar = realmRoom.getAvatar();
                    if (realmRoom.getType() == ProtoGlobal.Room.Type.CHANNEL && realmRoom.getChannelRoom().isVerified() || realmRoom.getType() == CHAT && realmRoom.getChatRoom().isVerified()) {
                        item.isVerified = true;
                    }
                    if (realmRoom.getType() == ProtoGlobal.Room.Type.CHAT && realmRoom.getChatRoom() != null) {
                        item.idDetectAvatar = realmRoom.getChatRoom().getPeerId();
                    } else {
                        if (realmRoom.getType() == ProtoGlobal.Room.Type.GROUP && realmRoom.getGroupRoom() != null) {
                            item.userName = realmRoom.getGroupRoom().getUsername();
                        } else if (realmRoom.getType() == ProtoGlobal.Room.Type.CHANNEL && realmRoom.getChannelRoom() != null) {
                            item.userName = realmRoom.getChannelRoom().getUsername();
                        }
                        item.idDetectAvatar = realmRoom.getId();
                    }
                    list.add(item);
                }
            }
        }
    });
}
Also used : RealmRoomMessage(net.iGap.realm.RealmRoomMessage) RealmRoom(net.iGap.realm.RealmRoom)

Example 15 with RealmRoomMessage

use of net.iGap.realm.RealmRoomMessage in project iGap-Android by KianIranian-STDG.

the class ShowMediaListener method getRoomMediaMessages.

private List<RealmRoomMessage> getRoomMediaMessages(Bundle bundle) {
    if (bundle != null) {
        long mRoomId = bundle.getLong(RealmConstants.REALM_ROOM_ID, 0L);
        long selectedFileToken = bundle.getLong(RealmConstants.REALM_SELECTED_IMAGE);
        String[] messageTypeImageVideo = new String[] { ProtoGlobal.RoomMessageType.VIDEO.toString(), ProtoGlobal.RoomMessageType.VIDEO_TEXT.toString(), ProtoGlobal.RoomMessageType.IMAGE.toString(), ProtoGlobal.RoomMessageType.IMAGE_TEXT.toString() };
        List<RealmRoomMessage> realmRoomMessages = DbManager.getInstance().doRealmTask(realm -> {
            return realm.copyFromRealm(realm.where(RealmRoomMessage.class).equalTo(RealmConstants.REALM_ROOM_ID, mRoomId).in(RealmConstants.REALM_MESSAGE_TYPE, messageTypeImageVideo).or().equalTo(RealmConstants.REALM_ROOM_ID, mRoomId).isNotNull(RealmConstants.REALM_FORWARD_MESSAGE).findAll().sort(RealmConstants.REALM_UPDATE_TIME, Sort.ASCENDING));
        });
        List messageTypesList = Arrays.asList(messageTypeImageVideo);
        ArrayList<RealmRoomMessage> roomMessagesImageVideo = new ArrayList<>(realmRoomMessages);
        for (RealmRoomMessage roomMessageObj : realmRoomMessages) {
            if (roomMessageObj.forwardMessage != null && !messageTypesList.contains(roomMessageObj.forwardMessage.messageType)) {
                roomMessagesImageVideo.remove(roomMessageObj);
            }
        }
        for (int i = roomMessagesImageVideo.size() - 1; i >= 0; i--) {
            if (selectedFileToken == roomMessagesImageVideo.get(i).messageId) {
                selectedFile = i;
                break;
            }
        }
        return roomMessagesImageVideo;
    } else {
        return null;
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) RealmRoomMessage(net.iGap.realm.RealmRoomMessage)

Aggregations

RealmRoomMessage (net.iGap.realm.RealmRoomMessage)56 RealmRoom (net.iGap.realm.RealmRoom)20 MessageObject (net.iGap.structs.MessageObject)20 ArrayList (java.util.ArrayList)19 File (java.io.File)14 SuppressLint (android.annotation.SuppressLint)11 DbManager (net.iGap.module.accountManager.DbManager)11 NonNull (androidx.annotation.NonNull)10 HelperString (net.iGap.helper.HelperString)10 StructMessageInfo (net.iGap.module.structs.StructMessageInfo)10 RealmString (net.iGap.realm.RealmString)10 View (android.view.View)9 TextView (android.widget.TextView)9 Realm (io.realm.Realm)9 AttachmentObject (net.iGap.structs.AttachmentObject)9 Intent (android.content.Intent)8 Nullable (androidx.annotation.Nullable)8 Configuration (android.content.res.Configuration)7 Bundle (android.os.Bundle)7 LayoutInflater (android.view.LayoutInflater)7