Search in sources :

Example 21 with RealmRoomMessage

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

the class FragmentChat method onMessageReceive.

@Override
public synchronized void onMessageReceive(final long roomId, String message, ProtoGlobal.RoomMessageType messageType, final ProtoGlobal.RoomMessage roomMessage, final ProtoGlobal.Room.Type roomType) {
    if (roomMessage.getMessageId() <= biggestMessageId) {
        return;
    }
    if (soundPool != null && sendMessageSound != 0)
        playReceiveSound(roomId, roomMessage, roomType);
    if (isBot) {
        if (rootView != null) {
            rootView.post(() -> {
                if (getActivity() == null || getActivity().isFinishing())
                    return;
                if (roomMessage.getAdditionalType() == Additional.WEB_VIEW.getAdditional()) {
                    openWebViewForSpecialUrlChat(roomMessage.getAdditionalData());
                    return;
                }
                RealmRoomMessage rm = null;
                boolean backToMenu = true;
                RealmResults<RealmRoomMessage> result = DbManager.getInstance().doRealmTask(realm -> {
                    return realm.where(RealmRoomMessage.class).equalTo("roomId", mRoomId).notEqualTo("authorHash", RealmUserInfo.getCurrentUserAuthorHash()).findAll();
                });
                if (result.size() > 0) {
                    rm = result.last();
                    if (rm != null && rm.getMessage() != null) {
                        if (rm.getMessage().toLowerCase().equals("/start") || rm.getMessage().equals("/back")) {
                            backToMenu = false;
                        }
                    }
                }
                if (roomMessage.getAuthor().getUser().getUserId() == chatPeerId && botInit != null) {
                    if (rm != null && rm.getRealmAdditional() != null && roomMessage.getAdditionalType() == AdditionalType.UNDER_KEYBOARD_BUTTON)
                        botInit.updateCommandList(false, message, getActivity(), backToMenu, roomMessage, roomId, true);
                    else
                        botInit.updateCommandList(false, "clear", getActivity(), backToMenu, null, 0, true);
                }
                if (isShowStartButton) {
                    rootView.findViewById(R.id.chl_ll_channel_footer).setVisibility(View.GONE);
                    if (webViewChatPage == null)
                        rootView.findViewById(R.id.layout_attach_file).setVisibility(View.VISIBLE);
                    isShowStartButton = false;
                }
            });
        }
    }
    DbManager.getInstance().doRealmTask(realm -> {
        final RealmRoomMessage realmRoomMessage = realm.where(RealmRoomMessage.class).equalTo("messageId", roomMessage.getMessageId()).findFirst();
        if (realmRoomMessage != null && realmRoomMessage.isValid() && !realmRoomMessage.isDeleted()) {
            if (roomMessage.getAuthor().getUser() != null) {
                RealmRoomMessage messageCopy = realm.copyFromRealm(realmRoomMessage);
                // I'm in the room
                if (roomId == mRoomId) {
                    // if (roomMessage.getAuthor().getUser().getUserId() != G.userId)
                    G.handler.post(new Runnable() {

                        @Override
                        public void run() {
                            if (addToView) {
                                switchAddItem(new ArrayList<>(Collections.singletonList(new StructMessageInfo(messageCopy))), false);
                                if (isShowLayoutUnreadMessage) {
                                    removeLayoutUnreadMessage();
                                }
                            }
                            if (messageCopy.getMessageType() == IMAGE_TEXT || messageCopy.getMessageType() == IMAGE || messageCopy.getMessageType() == VIDEO || messageCopy.getMessageType() == VIDEO_TEXT) {
                                EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.ON_NEW_MEDIA_MESSAGE_RECEIVED, roomId);
                            }
                            setBtnDownVisible(messageCopy);
                        }
                    });
                }
            }
        }
    });
}
Also used : StructMessageInfo(net.iGap.module.structs.StructMessageInfo) ArrayList(java.util.ArrayList) RealmRoomMessage(net.iGap.realm.RealmRoomMessage)

Example 22 with RealmRoomMessage

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

the class FragmentChat method exportChat.

public void exportChat() {
    RealmResults<RealmRoomMessage> realmRoomMessages = DbManager.getInstance().doRealmTask(realm -> {
        return realm.where(RealmRoomMessage.class).equalTo("roomId", mRoomId).sort("createTime").findAll();
    });
    File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/iGap", "iGap Messages");
    if (!root.exists()) {
        root.mkdir();
    }
    FileListerDialog fileListerDialog = FileListerDialog.createFileListerDialog(G.fragmentActivity);
    fileListerDialog.setDefaultDir(root);
    fileListerDialog.setFileFilter(FileListerDialog.FILE_FILTER.DIRECTORY_ONLY);
    fileListerDialog.show();
    fileListerDialog.setOnFileSelectedListener(new OnFileSelectedListener() {

        @Override
        public void onFileSelected(File file, String path) {
            final MaterialDialog[] dialog = new MaterialDialog[1];
            if (realmRoomMessages.size() != 0 && chatType != CHANNEL) {
                G.handler.post(new Runnable() {

                    @Override
                    public void run() {
                        dialog[0] = new MaterialDialog.Builder(G.currentActivity).title(R.string.export_chat).content(R.string.just_wait_en).progress(false, realmRoomMessages.size(), true).show();
                    }
                });
                try {
                    File filepath = new File(file, title + ".txt");
                    FileWriter writer = new FileWriter(filepath);
                    for (RealmRoomMessage export : realmRoomMessages) {
                        if (export.getMessageType().toString().equalsIgnoreCase("TEXT")) {
                            writer.append(RealmRegisteredInfo.getNameWithId(export.getUserId()) + "  text message " + "  :  " + export.getMessage() + "  date  :" + HelperCalander.milladyDate(export.getCreateTime()) + "\n");
                        } else {
                            writer.append(RealmRegisteredInfo.getNameWithId(export.getUserId()) + "  text message " + export.getMessage() + "  :  message in format " + export.getMessageType() + "  date  :" + HelperCalander.milladyDate(export.getCreateTime()) + "\n");
                        }
                        G.handler.post(new Runnable() {

                            @Override
                            public void run() {
                                dialog[0].incrementProgress(1);
                            }
                        });
                    }
                    writer.flush();
                    writer.close();
                    G.handler.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            dialog[0].dismiss();
                        }
                    }, 500);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) FileListerDialog(net.iGap.module.FileListerDialog.FileListerDialog) OnFileSelectedListener(net.iGap.module.FileListerDialog.OnFileSelectedListener) FileWriter(java.io.FileWriter) RealmRoomMessage(net.iGap.realm.RealmRoomMessage) RealmString(net.iGap.realm.RealmString) HelperString(net.iGap.helper.HelperString) HelperSaveFile(net.iGap.helper.HelperSaveFile) File(java.io.File) AttachFile(net.iGap.module.AttachFile) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException)

Example 23 with RealmRoomMessage

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

the class FragmentChat method sendStickerAsMessage.

private void sendStickerAsMessage(StructIGSticker structIGSticker) {
    String additional = new Gson().toJson(structIGSticker);
    long identity = AppUtils.makeRandomId();
    int[] imageSize = AndroidUtils.getImageDimens(structIGSticker.getPath());
    RealmRoomMessage roomMessage = new RealmRoomMessage();
    roomMessage.setMessageId(identity);
    roomMessage.setMessageType(ProtoGlobal.RoomMessageType.STICKER);
    roomMessage.setRoomId(mRoomId);
    roomMessage.setMessage(structIGSticker.getName());
    roomMessage.setStatus(ProtoGlobal.RoomMessageStatus.SENDING.toString());
    roomMessage.setUserId(AccountManager.getInstance().getCurrentUser().getId());
    roomMessage.setCreateTime(TimeUtils.currentLocalTime());
    RealmAdditional realmAdditional = new RealmAdditional();
    realmAdditional.setId(AppUtils.makeRandomId());
    realmAdditional.setAdditionalType(structIGSticker.isGiftSticker() ? AdditionalType.GIFT_STICKER : AdditionalType.STICKER);
    realmAdditional.setAdditionalData(additional);
    roomMessage.setRealmAdditional(realmAdditional);
    RealmAttachment realmAttachment = new RealmAttachment();
    realmAttachment.setId(identity);
    realmAttachment.setLocalFilePath(structIGSticker.getPath());
    realmAttachment.setWidth(imageSize[0]);
    realmAttachment.setHeight(imageSize[1]);
    realmAttachment.setSize(new File(structIGSticker.getPath()).length());
    realmAttachment.setName(new File(structIGSticker.getPath()).getName());
    realmAttachment.setDuration(0);
    roomMessage.setAttachment(realmAttachment);
    roomMessage.getAttachment().setToken(structIGSticker.getToken());
    roomMessage.setAuthorHash(RealmUserInfo.getCurrentUserAuthorHash());
    roomMessage.setShowMessage(true);
    roomMessage.setCreateTime(TimeUtils.currentLocalTime());
    if (isReply()) {
        RealmRoomMessage copyReplyMessage = DbManager.getInstance().doRealmTask(realm -> {
            RealmRoomMessage copyReplyMessage1 = realm.where(RealmRoomMessage.class).equalTo("messageId", getReplyMessageId()).findFirst();
            if (copyReplyMessage1 != null) {
                return realm.copyFromRealm(copyReplyMessage1);
            }
            return null;
        });
        if (copyReplyMessage != null) {
            roomMessage.setReplyTo(copyReplyMessage);
        }
    }
    new Thread(() -> DbManager.getInstance().doRealmTransaction(realm -> {
        realm.copyToRealmOrUpdate(roomMessage);
        RealmStickerItem stickerItem = realm.where(RealmStickerItem.class).equalTo("id", structIGSticker.getId()).findFirst();
        if (stickerItem != null && stickerItem.isValid()) {
            stickerItem.setRecent();
        }
    })).start();
    MessageObject messageObject = MessageObject.create(roomMessage);
    scrollToEnd();
    if (isReply()) {
        mReplayLayout.setTag(null);
        mReplayLayout.setVisibility(View.GONE);
    }
    if (FragmentChat.structIGSticker != null) {
        FragmentChat.structIGSticker = null;
        if (getActivity() instanceof ActivityMain) {
            ((ActivityMain) getActivity()).checkHasSharedData(false);
        }
    }
}
Also used : RealmAdditional(net.iGap.realm.RealmAdditional) ActivityMain(net.iGap.activities.ActivityMain) Gson(com.google.gson.Gson) RealmString(net.iGap.realm.RealmString) HelperString(net.iGap.helper.HelperString) RealmRoomMessage(net.iGap.realm.RealmRoomMessage) RealmAttachment(net.iGap.realm.RealmAttachment) HelperSaveFile(net.iGap.helper.HelperSaveFile) File(java.io.File) AttachFile(net.iGap.module.AttachFile) MessageObject(net.iGap.structs.MessageObject) RealmStickerItem(net.iGap.realm.RealmStickerItem)

Example 24 with RealmRoomMessage

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

the class FragmentChat method onReplyClick.

/**
 * @param replyMessage when click on replay message this method call.
 *                     if message in view scroll to position with animation called,but if message exist in room db reset message value and get message.
 *                     and if not exist in db and view get message from history request and put into db and call again onReplayClick method.
 */
@Override
public void onReplyClick(MessageObject replyMessage) {
    // TODO: 12/29/20 MESSAGE_REFACTOR
    if (replyMessage.messageType == STORY_REPLY_VALUE && replyMessage.replayToMessage == null) {
        if (replyMessage.storyObject != null && replyMessage.storyStatus == ProtoGlobal.RoomMessageStory.Status.ACTIVE_VALUE) {
            new HelperFragment(getActivity().getSupportFragmentManager(), new StoryViewFragment(replyMessage.storyObject.userId, true, true, true, replyMessage.storyObject.storyId)).setReplace(false).load();
        } else {
            Toast.makeText(getContext(), R.string.moment_not_available, Toast.LENGTH_SHORT).show();
        }
    } else if (replyMessage.replayToMessage != null && !goToPositionWithAnimation(replyMessage.replayToMessage.id, 1000)) {
        long replayMessageId = Math.abs(replyMessage.replayToMessage.id);
        long documentId = replyMessage.replayToMessage.documentId;
        if (!goToPositionWithAnimation(replayMessageId, 1000)) {
            if (RealmRoomMessage.existMessageInRoom(replayMessageId, mRoomId)) {
                resetMessagingValue();
                savedScrollMessageId = replayMessageId;
                savedScrollDocumentId = replyMessage.replayToMessage.documentId;
                firstVisiblePositionOffset = 0;
                getMessages();
            } else {
                new RequestClientGetRoomHistory().getRoomHistory(mRoomId, documentId, replayMessageId - 1, 1, DOWN, new RequestClientGetRoomHistory.OnHistoryReady() {

                    @Override
                    public void onHistory(List<ProtoGlobal.RoomMessage> messageList) {
                        G.handler.post(() -> {
                            DbManager.getInstance().doRealmTransaction(realm1 -> {
                                for (ProtoGlobal.RoomMessage roomMessage : messageList) {
                                    RealmRoomMessage realmRoomMessage = RealmRoomMessage.putOrUpdate(realm1, mRoomId, roomMessage, new StructMessageOption().setGap());
                                    onReplyClick(MessageObject.create(realmRoomMessage, false, true, false));
                                }
                            });
                        });
                    }

                    @Override
                    public void onErrorHistory(int major, int minor) {
                        G.handler.post(() -> {
                            if (major == 626) {
                                HelperError.showSnackMessage(G.context.getResources().getString(R.string.not_found_message), false);
                            } else if (minor == 624) {
                                HelperError.showSnackMessage(G.context.getResources().getString(R.string.ivnalid_data_provided), false);
                            } else {
                                HelperError.showSnackMessage(G.context.getResources().getString(R.string.there_is_no_connection_to_server), false);
                            }
                        });
                    }
                });
            }
        }
    }
}
Also used : StructMessageOption(net.iGap.module.structs.StructMessageOption) ProtoGlobal(net.iGap.proto.ProtoGlobal) RealmList(io.realm.RealmList) ArrayList(java.util.ArrayList) List(java.util.List) StoryViewFragment(net.iGap.story.viewPager.StoryViewFragment) RealmRoomMessage(net.iGap.realm.RealmRoomMessage) HelperFragment(net.iGap.helper.HelperFragment) RequestClientGetRoomHistory(net.iGap.request.RequestClientGetRoomHistory)

Example 25 with RealmRoomMessage

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

the class FragmentChat method makeLayoutTime.

private MessageObject makeLayoutTime(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);
    String timeString = TimeUtils.getChatSettingsTimeAgo(G.fragmentActivity, calendar.getTime());
    RealmRoomMessage timeMessage = RealmRoomMessage.makeTimeMessage(time, timeString);
    return MessageObject.create(timeMessage);
}
Also used : Calendar(java.util.Calendar) RealmString(net.iGap.realm.RealmString) HelperString(net.iGap.helper.HelperString) 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