Search in sources :

Example 1 with StructMessageInfo

use of net.iGap.module.structs.StructMessageInfo in project iGap-Android by KianIranian-STDG.

the class FragmentChat method sendForwardedMessage.

private void sendForwardedMessage(final MessageObject sourceMessage, final long destinationRoomId, final boolean isSingleForward, int k, boolean isMessage) {
    final long messageId = AppUtils.makeRandomId();
    RealmRoom destinationRoom = DbManager.getInstance().doRealmTask(realm -> {
        return realm.where(RealmRoom.class).equalTo("id", destinationRoomId).findFirst();
    });
    if (destinationRoom == null || destinationRoom.getReadOnly()) {
        return;
    }
    final int type = destinationRoom.getType().getNumber();
    getMessageDataStorage().createForwardMessage(destinationRoomId, messageId, sourceMessage, isMessage, object -> {
        MessageObject createdForwardMessage = (MessageObject) object[0];
        RealmRoomMessage forwardedRealm = (RealmRoomMessage) object[1];
        Long sourceRoomId = (Long) object[2];
        Long sourceMessageId = (Long) object[3];
        if (forwardedRealm.isValid() && !createdForwardMessage.deleted) {
            if (isSingleForward || forwardedRealm.getRoomId() == mRoomId) {
                switchAddItem(new ArrayList<>(Collections.singletonList(new StructMessageInfo(forwardedRealm))), false);
                scrollToEnd();
            }
            getSendMessageUtil().buildForward(type, createdForwardMessage.roomId, createdForwardMessage, sourceRoomId, sourceMessageId);
        }
    });
}
Also used : StructMessageInfo(net.iGap.module.structs.StructMessageInfo) RealmRoom(net.iGap.realm.RealmRoom) RealmRoomMessage(net.iGap.realm.RealmRoomMessage) MessageObject(net.iGap.structs.MessageObject) SuppressLint(android.annotation.SuppressLint)

Example 2 with StructMessageInfo

use of net.iGap.module.structs.StructMessageInfo 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 3 with StructMessageInfo

use of net.iGap.module.structs.StructMessageInfo in project iGap-Android by KianIranian-STDG.

the class FragmentChat method onChatClearMessage.

public void onChatClearMessage(final long roomId, final long clearId) {
    // TODO: 12/28/20 MESSAGE_REFACTOR_NEED_TEST
    setDownBtnGone();
    saveMessageIdPositionState(0, 0);
    addToView = true;
    if (botInit != null)
        botInit.updateCommandList(false, "clear", getActivity(), false, null, 0, false);
    mAdapter.clear();
    recyclerView.removeAllViews();
    if (edtChat.getTag() != null && edtChat.getTag() instanceof StructMessageInfo) {
        edtChat.setTag(null);
    }
    if (mAdapter != null) {
        boolean cleared = false;
        if (mAdapter.getAdapterItemCount() > 1) {
            try {
                if (mAdapter.getAdapterItem(mAdapter.getAdapterItemCount() - 1).messageObject.id == clearId) {
                    cleared = true;
                    mAdapter.clear();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (!cleared) {
            int selectedPosition = -1;
            for (int i = (mAdapter.getAdapterItemCount() - 1); i >= 0; i--) {
                try {
                    MessageObject messageObject = mAdapter.getAdapterItem(i).messageObject;
                    if (messageObject != null && messageObject.id == clearId) {
                        selectedPosition = i;
                        break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (selectedPosition != -1) {
                for (int i = selectedPosition; i >= 0; i--) {
                    mAdapter.remove(i);
                }
            }
        }
    }
    /**
     * remove tag from edtChat if the message has deleted
     */
    if (edtChat != null && edtChat.getTag() != null && edtChat.getTag() instanceof StructMessageInfo) {
        edtChat.setTag(null);
    }
}
Also used : StructMessageInfo(net.iGap.module.structs.StructMessageInfo) MessageObject(net.iGap.structs.MessageObject) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) SuppressLint(android.annotation.SuppressLint)

Example 4 with StructMessageInfo

use of net.iGap.module.structs.StructMessageInfo in project iGap-Android by KianIranian-STDG.

the class FragmentChat method switchAddItem.

private void switchAddItem(ArrayList<StructMessageInfo> infos, boolean addTop) {
    if (prgWaiting != null && infos.size() > 0) {
        prgWaiting.setVisibility(View.GONE);
    }
    long identifier = SUID.id().get();
    for (StructMessageInfo info : infos) {
        MessageObject messageObject = MessageObject.create(info.realmRoomMessage);
        int messageType;
        if (messageObject.isForwarded()) {
            messageType = messageObject.forwardedMessage.messageType;
        } else {
            messageType = messageObject.messageType;
        }
        if (!messageObject.isTimeOrLogMessage() || (messageType == LOG_VALUE)) {
            int index = 0;
            if (addTop) {
                if (messageObject.needToShow) {
                    for (int i = 0; i < mAdapter.getAdapterItemCount(); i++) {
                        if (mAdapter.getAdapterItem(i) instanceof TimeItem) {
                            // TODO: 12/28/20 MESSAGE_REFACTOR
                            if (!RealmRoomMessage.isTimeDayDifferent(messageObject.getUpdateOrCreateTime(), mAdapter.getAdapterItem(i).messageObject.getUpdateOrCreateTime())) {
                                mAdapter.remove(i);
                            }
                            break;
                        }
                    }
                    mAdapter.add(0, new TimeItem(mAdapter, this).setMessage(makeLayoutTime(messageObject.getUpdateOrCreateTime())).withIdentifier(identifier++));
                    index = 1;
                }
            } else {
                /**
                 * don't allow for add lower messageId to bottom of list
                 */
                if (messageObject.id > biggestMessageId) {
                    if (messageObject.status != MessageObject.STATUS_SENDING) {
                        biggestMessageId = messageObject.id;
                    }
                } else {
                    continue;
                }
                if (lastMessageId == messageObject.id) {
                    continue;
                } else {
                    lastMessageId = messageObject.id;
                }
                if (messageObject.needToShow) {
                    if (mAdapter.getItemCount() > 0) {
                        if (mAdapter.getAdapterItem(mAdapter.getItemCount() - 1).messageObject != null && RealmRoomMessage.isTimeDayDifferent(messageObject.getUpdateOrCreateTime(), mAdapter.getAdapterItem(mAdapter.getItemCount() - 1).messageObject.getUpdateOrCreateTime())) {
                            mAdapter.add(new TimeItem(mAdapter, this).setMessage(makeLayoutTime(messageObject.getUpdateOrCreateTime())).withIdentifier(identifier++));
                        }
                    } else {
                        mAdapter.add(new TimeItem(mAdapter, this).setMessage(makeLayoutTime(messageObject.getUpdateOrCreateTime())).withIdentifier(identifier++));
                    }
                }
            }
            switch(messageType) {
                case TEXT_VALUE:
                case STORY_REPLY_VALUE:
                    if (messageObject.getAdditional() != null && messageObject.getAdditional().type == AdditionalType.CARD_TO_CARD_MESSAGE)
                        if (!addTop) {
                            mAdapter.add(new CardToCardItem(mAdapter, chatType, FragmentChat.this).setMessage(messageObject).withIdentifier(identifier));
                        } else {
                            mAdapter.add(index, new CardToCardItem(mAdapter, chatType, FragmentChat.this).setMessage(messageObject).withIdentifier(identifier));
                        }
                    else {
                        if (!addTop) {
                            mAdapter.add(new TextItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                        } else {
                            mAdapter.add(index, new TextItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                        }
                    }
                    break;
                case WALLET_VALUE:
                    if (messageObject.wallet.cardToCard != null) {
                        if (!addTop) {
                            mAdapter.add(new LogWalletCardToCard(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                        } else {
                            mAdapter.add(index, new LogWalletCardToCard(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                        }
                    } else if (messageObject.wallet.topupObject != null) {
                        if (!addTop) {
                            mAdapter.add(new LogWalletTopup(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                        } else {
                            mAdapter.add(index, new LogWalletTopup(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                        }
                        ;
                    } else if (messageObject.wallet.billObject != null) {
                        if (!addTop) {
                            mAdapter.add(new LogWalletBill(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                        } else {
                            mAdapter.add(index, new LogWalletBill(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                        }
                    } else {
                        if (!addTop) {
                            mAdapter.add(new LogWallet(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                        } else {
                            mAdapter.add(index, new LogWallet(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                        }
                    }
                    break;
                case IMAGE_VALUE:
                case IMAGE_TEXT_VALUE:
                    if (!addTop) {
                        mAdapter.add(new ImageWithTextItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                    } else {
                        mAdapter.add(index, new ImageWithTextItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                    }
                    break;
                case VIDEO_VALUE:
                case VIDEO_TEXT_VALUE:
                    if (!addTop) {
                        mAdapter.add(new VideoWithTextItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                    } else {
                        mAdapter.add(index, new VideoWithTextItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                    }
                    break;
                case LOCATION_VALUE:
                    if (!addTop) {
                        mAdapter.add(new LocationItem(mAdapter, chatType, this, getActivity()).setMessage(messageObject).withIdentifier(identifier));
                    } else {
                        mAdapter.add(index, new LocationItem(mAdapter, chatType, this, getActivity()).setMessage(messageObject).withIdentifier(identifier));
                    }
                    break;
                case FILE_VALUE:
                case FILE_TEXT_VALUE:
                    if (!addTop) {
                        mAdapter.add(new FileItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                    } else {
                        mAdapter.add(index, new FileItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                    }
                    break;
                case STICKER_VALUE:
                    if (messageObject.getAdditional() != null && messageObject.getAdditional().type == AdditionalType.GIFT_STICKER) {
                    } else if (messageObject.getAttachment() != null && messageObject.getAttachment().name != null && messageObject.getAttachment().isAnimatedSticker()) {
                        if (!addTop) {
                            mAdapter.add(new AnimatedStickerItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                        } else {
                            mAdapter.add(index, new AnimatedStickerItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                        }
                    } else {
                        if (messageObject.getAttachment() != null) {
                            if (!addTop) {
                                mAdapter.add(new StickerItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                            } else {
                                mAdapter.add(index, new StickerItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                            }
                        }
                    }
                    break;
                case VOICE_VALUE:
                    if (!addTop) {
                        mAdapter.add(new VoiceItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                    } else {
                        mAdapter.add(index, new VoiceItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                    }
                    break;
                case AUDIO_VALUE:
                case AUDIO_TEXT_VALUE:
                    if (!addTop) {
                        mAdapter.add(new AudioItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                    } else {
                        mAdapter.add(index, new AudioItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                    }
                    break;
                case CONTACT_VALUE:
                    if (!addTop) {
                        mAdapter.add(new ContactItem(getActivity(), mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                    } else {
                        mAdapter.add(index, new ContactItem(getActivity(), mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                    }
                    break;
                case GIF_VALUE:
                case GIF_TEXT_VALUE:
                    if (!addTop) {
                        mAdapter.add(new GifWithTextItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                    } else {
                        mAdapter.add(index, new GifWithTextItem(mAdapter, chatType, this).setMessage(messageObject).withIdentifier(identifier));
                    }
                    break;
                case LOG_VALUE:
                    if (messageObject.needToShow) {
                        if (!addTop) {
                            mAdapter.add(new LogItem(mAdapter, this).setMessage(messageObject).withIdentifier(identifier));
                        } else {
                            mAdapter.add(index, new LogItem(mAdapter, this).setMessage(messageObject).withIdentifier(identifier));
                        }
                    }
                    break;
            }
        }
        identifier++;
    }
}
Also used : StickerItem(net.iGap.adapter.items.chat.StickerItem) AnimatedStickerItem(net.iGap.adapter.items.chat.AnimatedStickerItem) RealmStickerItem(net.iGap.realm.RealmStickerItem) GifWithTextItem(net.iGap.adapter.items.chat.GifWithTextItem) ImageWithTextItem(net.iGap.adapter.items.chat.ImageWithTextItem) StructMessageInfo(net.iGap.module.structs.StructMessageInfo) TimeItem(net.iGap.adapter.items.chat.TimeItem) VideoWithTextItem(net.iGap.adapter.items.chat.VideoWithTextItem) TextItem(net.iGap.adapter.items.chat.TextItem) GifWithTextItem(net.iGap.adapter.items.chat.GifWithTextItem) ImageWithTextItem(net.iGap.adapter.items.chat.ImageWithTextItem) LocationItem(net.iGap.adapter.items.chat.LocationItem) IUpdateLogItem(net.iGap.observers.interfaces.IUpdateLogItem) LogItem(net.iGap.adapter.items.chat.LogItem) ContactItem(net.iGap.adapter.items.chat.ContactItem) LogWallet(net.iGap.adapter.items.chat.LogWallet) LogWalletBill(net.iGap.adapter.items.chat.LogWalletBill) AudioItem(net.iGap.adapter.items.chat.AudioItem) SuppressLint(android.annotation.SuppressLint) AnimatedStickerItem(net.iGap.adapter.items.chat.AnimatedStickerItem) VideoWithTextItem(net.iGap.adapter.items.chat.VideoWithTextItem) CardToCardItem(net.iGap.adapter.items.chat.CardToCardItem) FileItem(net.iGap.adapter.items.chat.FileItem) LogWalletTopup(net.iGap.adapter.items.chat.LogWalletTopup) VoiceItem(net.iGap.adapter.items.chat.VoiceItem) LogWalletCardToCard(net.iGap.adapter.items.chat.LogWalletCardToCard) MessageObject(net.iGap.structs.MessageObject)

Example 5 with StructMessageInfo

use of net.iGap.module.structs.StructMessageInfo in project iGap-Android by KianIranian-STDG.

the class FragmentChat method sendPosition.

public void sendPosition(final Double latitude, final Double longitude, final String imagePath) {
    sendCancelAction();
    if (isShowLayoutUnreadMessage) {
        removeLayoutUnreadMessage();
    }
    final long messageId = AppUtils.makeRandomId();
    RealmRoomMessage roomMessage = new RealmRoomMessage();
    roomMessage.setMessageId(messageId);
    RealmRoomMessageLocation realmRoomMessageLocation = new RealmRoomMessageLocation();
    realmRoomMessageLocation.setId(AppUtils.makeRandomId());
    realmRoomMessageLocation.setLocationLat(latitude);
    realmRoomMessageLocation.setLocationLong(longitude);
    realmRoomMessageLocation.setImagePath(imagePath);
    roomMessage.setLocation(realmRoomMessageLocation);
    roomMessage.setCreateTime(TimeUtils.currentLocalTime());
    roomMessage.setMessageType(ProtoGlobal.RoomMessageType.LOCATION);
    roomMessage.setRoomId(mRoomId);
    roomMessage.setUserId(AccountManager.getInstance().getCurrentUser().getId());
    roomMessage.setAuthorHash(RealmUserInfo.getCurrentUserAuthorHash());
    roomMessage.setStatus(ProtoGlobal.RoomMessageStatus.SENDING.toString());
    if (replyMessageId() > 0) {
        RealmRoomMessage realmRoomMessageCopy = DbManager.getInstance().doRealmTask(realm -> {
            RealmRoomMessage realmRoomMessage = realm.where(RealmRoomMessage.class).equalTo("messageId", replyMessageId()).findFirst();
            if (realmRoomMessage != null) {
                return realm.copyFromRealm(realmRoomMessage);
            } else {
                return null;
            }
        });
        if (realmRoomMessageCopy != null) {
            roomMessage.setReplyTo(realmRoomMessageCopy);
        }
    }
    new Thread(new Runnable() {

        @Override
        public void run() {
            DbManager.getInstance().doRealmTransaction(realm -> {
                RealmRoomMessage managedMessage = realm.copyToRealmOrUpdate(roomMessage);
                RealmRoom.setLastMessageWithRoomMessage(realm, mRoomId, managedMessage);
            });
        }
    }).start();
    G.handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            switchAddItem(new ArrayList<>(Collections.singletonList(new StructMessageInfo(roomMessage))), false);
            MessageObject locationMessage = MessageObject.create(roomMessage);
            getSendMessageUtil().build(chatType, mRoomId, locationMessage);
            scrollToEnd();
        }
    }, 300);
    clearReplyView();
}
Also used : StructMessageInfo(net.iGap.module.structs.StructMessageInfo) RealmRoomMessageLocation(net.iGap.realm.RealmRoomMessageLocation) ArrayList(java.util.ArrayList) RealmRoomMessage(net.iGap.realm.RealmRoomMessage) MessageObject(net.iGap.structs.MessageObject)

Aggregations

StructMessageInfo (net.iGap.module.structs.StructMessageInfo)9 RealmRoomMessage (net.iGap.realm.RealmRoomMessage)7 SuppressLint (android.annotation.SuppressLint)6 ArrayList (java.util.ArrayList)6 MessageObject (net.iGap.structs.MessageObject)6 ProtoClientGetRoomHistory (net.iGap.proto.ProtoClientGetRoomHistory)3 RequestClientGetRoomHistory (net.iGap.request.RequestClientGetRoomHistory)3 View (android.view.View)2 WebView (android.webkit.WebView)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 NonNull (androidx.annotation.NonNull)2 AppCompatImageView (androidx.appcompat.widget.AppCompatImageView)2 AppCompatTextView (androidx.appcompat.widget.AppCompatTextView)2 CardView (androidx.cardview.widget.CardView)2 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)2 RecyclerView (androidx.recyclerview.widget.RecyclerView)2 JsonObject (com.google.gson.JsonObject)2 RealmQuery (io.realm.RealmQuery)2 AnimatedStickerItem (net.iGap.adapter.items.chat.AnimatedStickerItem)2