Search in sources :

Example 1 with RealmAdditional

use of net.iGap.realm.RealmAdditional 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 2 with RealmAdditional

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

the class StoryDisplayFragment method sendStickerAsMessage.

private void sendStickerAsMessage(StructIGSticker structIGSticker) {
    RealmRoom realmRoom = DbManager.getInstance().doRealmTask(realm -> {
        return realm.where(RealmRoom.class).equalTo("chatRoom.peer_id", stories.get(counter).getUserId()).findFirst();
    });
    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.STORY_REPLY);
    if (realmRoom != null) {
        roomMessage.setRoomId(realmRoom.getId());
    }
    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();
    if (structIGSticker.getToken() != null && !structIGSticker.getToken().isEmpty()) {
        realmAttachment.setToken(structIGSticker.getToken());
    }
    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());
    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);
    ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).buildStoryReply(realmRoom.getType().getNumber(), realmRoom.getId(), stories.get(counter), messageObject, null);
}
Also used : RealmAdditional(net.iGap.realm.RealmAdditional) Gson(com.google.gson.Gson) RealmRoom(net.iGap.realm.RealmRoom) RealmRoomMessage(net.iGap.realm.RealmRoomMessage) RealmAttachment(net.iGap.realm.RealmAttachment) File(java.io.File) MessageObject(net.iGap.structs.MessageObject) RealmStickerItem(net.iGap.realm.RealmStickerItem)

Example 3 with RealmAdditional

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

the class AbstractMessage method onBotBtnClick.

public void onBotBtnClick(View v, ButtonEntity buttonEntity) {
    DbManager.getInstance().doRealmTask(realm -> {
        try {
            if (v.getId() == ButtonActionType.USERNAME_LINK) {
                // TODO: fixed this and do not use G.currentActivity
                HelperUrl.checkUsernameAndGoToRoomWithMessageId(G.currentActivity, ((ArrayList<String>) v.getTag()).get(0).substring(1), HelperUrl.ChatEntry.chat, 0, 0);
            } else if (v.getId() == ButtonActionType.BOT_ACTION) {
                long messageId = System.currentTimeMillis();
                RealmRoomMessage roomMessage = new RealmRoomMessage();
                roomMessage.setMessageId(messageId);
                roomMessage.setMessageType(ProtoGlobal.RoomMessageType.TEXT);
                roomMessage.setRoomId(messageObject.roomId);
                roomMessage.setMessage(((ArrayList<String>) v.getTag()).get(1));
                roomMessage.setStatus(ProtoGlobal.RoomMessageStatus.SENDING.toString());
                roomMessage.setUserId(AccountManager.getInstance().getCurrentUser().getId());
                roomMessage.setCreateTime(TimeUtils.currentLocalTime());
                if (((ArrayList<String>) v.getTag()).get(2) != null) {
                    RealmAdditional additional = new RealmAdditional();
                    additional.setId(AppUtils.makeRandomId());
                    additional.setAdditionalData(((ArrayList<String>) v.getTag()).get(2));
                    additional.setAdditionalType(3);
                    roomMessage.setRealmAdditional(additional);
                }
                new Thread(() -> {
                    DbManager.getInstance().doRealmTransaction(realm12 -> {
                        realm12.copyToRealmOrUpdate(roomMessage);
                    });
                }).start();
                MessageObject botMessage = MessageObject.create(roomMessage);
                ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).build(type, messageObject.roomId, botMessage);
                messageClickListener.sendFromBot(botMessage);
            } else if (v.getId() == ButtonActionType.JOIN_LINK) {
                // TODO: fixed this and do not use G.currentActivity
                HelperUrl.checkAndJoinToRoom(G.currentActivity, ((ArrayList<String>) v.getTag()).get(0).substring(14));
            } else if (v.getId() == ButtonActionType.WEB_LINK) {
                HelperUrl.openBrowser(((ArrayList<String>) v.getTag()).get(0));
            } else if (v.getId() == ButtonActionType.WEBVIEW_LINK) {
                messageClickListener.sendFromBot(((ArrayList<String>) v.getTag()).get(0));
            } else if (v.getId() == ButtonActionType.REQUEST_PHONE) {
                try {
                    new MaterialDialog.Builder(G.currentActivity).title(R.string.access_phone_number).positiveText(R.string.ok).negativeText(R.string.cancel).onPositive(new MaterialDialog.SingleButtonCallback() {

                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            Long identity = System.currentTimeMillis();
                            realm.executeTransaction(new Realm.Transaction() {

                                @Override
                                public void execute(Realm realm) {
                                    RealmUserInfo realmUserInfo = RealmUserInfo.getRealmUserInfo(realm);
                                    RealmRoomMessage realmRoomMessage = RealmRoomMessage.makeAdditionalData(messageObject.roomId, identity, realmUserInfo.getUserInfo().getPhoneNumber(), realmUserInfo.getUserInfo().getPhoneNumber(), 0, realm, ProtoGlobal.RoomMessageType.TEXT);
                                    MessageObject botMessage = MessageObject.create(realmRoomMessage);
                                    ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).build(type, messageObject.roomId, botMessage);
                                    messageClickListener.sendFromBot(botMessage);
                                }
                            });
                        }
                    }).show();
                } catch (Exception e) {
                }
            } else if (v.getId() == ButtonActionType.REQUEST_LOCATION) {
                try {
                    new MaterialDialog.Builder(G.currentActivity).title(R.string.access_location).positiveText(R.string.ok).negativeText(R.string.cancel).onPositive(new MaterialDialog.SingleButtonCallback() {

                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            if (G.locationListener != null) {
                                isLocationFromBot = true;
                                G.locationListener.requestLocation();
                            }
                        }
                    }).show();
                } catch (Exception e) {
                }
            } else if (v.getId() == ButtonActionType.PAY_DIRECT) {
                JSONObject jsonObject = new JSONObject(((ArrayList<String>) v.getTag()).get(0));
                RealmRoom room = realm.where(RealmRoom.class).equalTo("id", messageObject.roomId).findFirst();
                new HelperFragment(G.currentActivity.getSupportFragmentManager()).loadPayment(room.getTitle(), jsonObject.getString("token"), null);
            } else if (v.getId() == ProtoGlobal.DiscoveryField.ButtonActionType.CARD_TO_CARD.getNumber()) {
                JSONObject rootJsonObject = new JSONObject(buttonEntity.getJsonObject());
                JSONObject valueObject = rootJsonObject.getJSONObject("value");
                String cardNumber = valueObject.getString("cardNumber");
                int amount = valueObject.getInt("amount");
                long userId = valueObject.getLong("userId");
                CardToCardHelper.NewCallCardToCard(G.currentActivity, userId, amount, cardNumber);
            } else if (v.getId() == ProtoGlobal.DiscoveryField.ButtonActionType.BILL_MENU.getNumber()) {
                try {
                    JSONObject jsonObject = new JSONObject(((ArrayList<String>) v.getTag()).get(0));
                    new HelperFragment(G.currentActivity.getSupportFragmentManager(), FragmentPaymentBill.newInstance(R.string.pay_bills, jsonObject)).setReplace(false).load();
                } catch (JSONException e) {
                    new HelperFragment(G.currentActivity.getSupportFragmentManager(), FragmentPaymentBill.newInstance(R.string.pay_bills)).setReplace(false).load();
                }
            } else if (v.getId() == ProtoGlobal.DiscoveryField.ButtonActionType.TRAFFIC_BILL_MENU.getNumber()) {
                try {
                    JSONObject jsonObject = new JSONObject(((ArrayList<String>) v.getTag()).get(0));
                    new HelperFragment(G.currentActivity.getSupportFragmentManager(), FragmentPaymentBill.newInstance(R.string.pay_bills_crime, jsonObject)).setReplace(false).load();
                } catch (JSONException e) {
                    new HelperFragment(G.currentActivity.getSupportFragmentManager(), FragmentPaymentBill.newInstance(R.string.pay_bills_crime)).setReplace(false).load();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(G.context, "دستور با خطا مواجه شد", Toast.LENGTH_LONG).show();
        }
    });
/**
 * The data was sent via the button via the view tag. Right now I only do this for the card due to lack of time with the new object
 */
}
Also used : RealmAdditional(net.iGap.realm.RealmAdditional) ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString) NonNull(androidx.annotation.NonNull) RealmRoom(net.iGap.realm.RealmRoom) Realm(io.realm.Realm) RealmUserInfo(net.iGap.realm.RealmUserInfo) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) JSONException(org.json.JSONException) JSONException(org.json.JSONException) AndroidRuntimeException(android.util.AndroidRuntimeException) JSONObject(org.json.JSONObject) DialogAction(com.afollestad.materialdialogs.DialogAction) RealmRoomMessage(net.iGap.realm.RealmRoomMessage) HelperFragment(net.iGap.helper.HelperFragment) MessageObject(net.iGap.structs.MessageObject)

Aggregations

RealmAdditional (net.iGap.realm.RealmAdditional)3 RealmRoomMessage (net.iGap.realm.RealmRoomMessage)3 MessageObject (net.iGap.structs.MessageObject)3 Gson (com.google.gson.Gson)2 File (java.io.File)2 RealmAttachment (net.iGap.realm.RealmAttachment)2 RealmRoom (net.iGap.realm.RealmRoom)2 RealmStickerItem (net.iGap.realm.RealmStickerItem)2 SpannableString (android.text.SpannableString)1 AndroidRuntimeException (android.util.AndroidRuntimeException)1 NonNull (androidx.annotation.NonNull)1 DialogAction (com.afollestad.materialdialogs.DialogAction)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 Realm (io.realm.Realm)1 ArrayList (java.util.ArrayList)1 ActivityMain (net.iGap.activities.ActivityMain)1 HelperFragment (net.iGap.helper.HelperFragment)1 HelperSaveFile (net.iGap.helper.HelperSaveFile)1 HelperString (net.iGap.helper.HelperString)1 AttachFile (net.iGap.module.AttachFile)1