Search in sources :

Example 6 with RealmRoom

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

the class MessageDataStorage method deleteRoomFromStorage.

public void deleteRoomFromStorage(long roomId) {
    storageQueue.postRunnable(() -> {
        FileLog.i(TAG, "deleteRoomFromStorage: " + roomId);
        try {
            database.beginTransaction();
            RealmRoom realmRoom = database.where(RealmRoom.class).equalTo("id", roomId).findFirst();
            if (realmRoom != null) {
                realmRoom.deleteFromRealm();
            }
            database.where(RealmClientCondition.class).equalTo("roomId", roomId).findAll().deleteAllFromRealm();
            database.where(RealmRoomMessage.class).equalTo("roomId", roomId).findAll().deleteAllFromRealm();
            database.commitTransaction();
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : RealmClientCondition(net.iGap.realm.RealmClientCondition) RealmRoom(net.iGap.realm.RealmRoom) RealmRoomMessage(net.iGap.realm.RealmRoomMessage)

Example 7 with RealmRoom

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

the class MessageDataStorage method updateRoomAddedStory.

public void updateRoomAddedStory(final List<ProtoGlobal.Story> stories) {
    storageQueue.postRunnable(() -> {
        FileLog.i(TAG, "updateRoomAddedStory roomId " + stories.get(0).getRoomId() + " storiesId " + stories.get(0).getId());
        try {
            database.beginTransaction();
            RealmRoom realmRoom = database.where(RealmRoom.class).equalTo("id", stories.get(0).getRoomId()).findFirst();
            if (realmRoom != null && realmRoom.getTitle() != null) {
                RealmStory realmStory;
                if (stories.get(0).getUserId() == AccountManager.getInstance().getCurrentUser().getId()) {
                    realmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", stories.get(0).getUserId()).findFirst();
                } else {
                    realmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("roomId", stories.get(0).getRoomId()).findFirst();
                }
                if (realmStory == null) {
                    realmStory = database.createObject(RealmStory.class, SUID.id().get());
                }
                List<StoryObject> storyObjects = new ArrayList<>();
                for (int i = 0; i < stories.size(); i++) {
                    if (stories.get(i).getTypeValue() == 0) {
                        StoryObject storyObject = StoryObject.create(stories.get(i), i, realmRoom.getTitle(), realmRoom != null ? realmRoom.getColor() : "#4aca69", true, realmRoom.getType() == ProtoGlobal.Room.Type.CHANNEL && realmRoom.getChannelRoom().isVerified());
                        storyObjects.add(storyObject);
                    }
                }
                realmStory.setLastCreatedAt(storyObjects.get(storyObjects.size() - 1).createdAt);
                realmStory.setDisplayName(realmRoom.getTitle());
                realmStory.setProfileColor(realmRoom.getColor());
                realmStory.setSessionId(AccountManager.getInstance().getCurrentUser().getId());
                if (stories.get(0).getUserId() != AccountManager.getInstance().getCurrentUser().getId()) {
                    realmStory.setUserId(0);
                    realmStory.setOrginatorValue(1);
                    realmStory.setVerified(realmRoom.getType() == ProtoGlobal.Room.Type.CHANNEL && realmRoom.getChannelRoom().isVerified());
                } else {
                    realmStory.setUserId(AccountManager.getInstance().getCurrentUser().getId());
                }
                realmStory.setRoomId(stories.get(0).getRoomId());
                realmStory.setSeenAll(false);
                realmStory.setRealmStoryProtos(database, storyObjects);
                int[] storyUnReadCount = new int[1];
                RealmResults<RealmStory> otherStories = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).notEqualTo("userId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isSeenAll", false).findAll();
                if (otherStories != null && otherStories.size() > 0) {
                    storyUnReadCount[0] = otherStories.size();
                } else {
                    storyUnReadCount[0] = 0;
                }
                database.commitTransaction();
                G.runOnUiThread(() -> {
                    if (G.onUnreadChange != null) {
                        G.onUnreadChange.onChange(storyUnReadCount[0], true);
                    }
                    getEventManager().postEvent(EventManager.STORY_USER_ADD_NEW);
                });
            } else {
                new RequestClientGetRoom().clientGetRoom(stories.get(0).getRoomId(), RequestClientGetRoom.CreateRoomMode.justInfo);
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : MainStoryObject(net.iGap.story.MainStoryObject) StoryObject(net.iGap.story.StoryObject) RequestClientGetRoom(net.iGap.request.RequestClientGetRoom) RealmStory(net.iGap.realm.RealmStory) ArrayList(java.util.ArrayList) RealmRoom(net.iGap.realm.RealmRoom)

Example 8 with RealmRoom

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

the class MessageDataStorage method getRoom.

public RealmRoom getRoom(final long roomId) {
    FileLog.i(TAG, "getRoom: " + roomId);
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final RealmRoom[] result = new RealmRoom[1];
    storageQueue.postRunnable(() -> {
        try {
            RealmRoom realmRoom = database.where(RealmRoom.class).equalTo("id", roomId).findFirst();
            if (realmRoom != null) {
                result[0] = database.copyFromRealm(realmRoom);
            }
            countDownLatch.countDown();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (Exception e) {
        FileLog.e(e);
    }
    Log.e(TAG, "getRoom: " + (result[0] != null ? result[0].getTitle() : "NULL"));
    return result[0];
}
Also used : RealmRoom(net.iGap.realm.RealmRoom) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 9 with RealmRoom

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

the class MessageController method clearHistoryMessage.

public void clearHistoryMessage(final long roomId) {
    if (roomId > 0) {
        RealmRoom realmRoom = getMessageDataStorage().getRoom(roomId);
        long clearMessageId;
        if (realmRoom == null || !realmRoom.isLoaded() || !realmRoom.isValid()) {
            return;
        }
        if (realmRoom.getLastMessage() != null) {
            clearMessageId = realmRoom.getLastMessage().getMessageId();
        } else {
            clearMessageId = getMessageDataStorage().getRoomClearId(roomId);
        }
        if (clearMessageId == 0) {
            return;
        }
        getMessageDataStorage().setRoomClearId(roomId, clearMessageId, true);
        AbstractObject req = null;
        if (realmRoom.getType() == ProtoGlobal.Room.Type.CHAT) {
            IG_RPC.Chat_Clear_History historyReq = new IG_RPC.Chat_Clear_History();
            historyReq.roomId = roomId;
            historyReq.lastMessageId = clearMessageId;
            req = historyReq;
        } else if (realmRoom.getType() == ProtoGlobal.Room.Type.GROUP) {
            IG_RPC.Group_Clear_History historyReq = new IG_RPC.Group_Clear_History();
            historyReq.roomId = roomId;
            historyReq.lastMessageId = clearMessageId;
            req = historyReq;
        }
        getRequestManager().sendRequest(req, (response, error) -> {
            if (error == null) {
                long clearId = 0;
                if (response instanceof IG_RPC.Res_Chat_Clear_History) {
                    IG_RPC.Res_Chat_Clear_History resClearMessage = (IG_RPC.Res_Chat_Clear_History) response;
                    clearId = resClearMessage.clearId;
                } else if (response instanceof IG_RPC.Res_Group_Clear_History) {
                    IG_RPC.Res_Group_Clear_History resClearMessage = (IG_RPC.Res_Group_Clear_History) response;
                    clearId = resClearMessage.clearId;
                }
                getMessageDataStorage().clearRoomHistory(roomId, clearId);
            }
        });
    }
}
Also used : IG_RPC(net.iGap.network.IG_RPC) AbstractObject(net.iGap.network.AbstractObject) RealmRoom(net.iGap.realm.RealmRoom)

Example 10 with RealmRoom

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

the class HelperUrl method goToActivity.

private static void goToActivity(FragmentActivity activity, final long roomId, final long peerId, ChatEntry chatEntry, final long messageId) {
    switch(chatEntry) {
        case chat:
            if (roomId != FragmentChat.lastChatRoomId) {
                DbManager.getInstance().doRealmTask(new DbManager.RealmTask() {

                    @Override
                    public void doTask(Realm realm) {
                        final RealmRoom realmRoom = realm.where(RealmRoom.class).equalTo("chatRoom.peer_id", peerId).findFirst();
                        if (realmRoom != null) {
                            new GoToChatActivity(realmRoom.getId()).setMessageID(messageId).startActivity(activity);
                        } else {
                            G.onChatGetRoom = new OnChatGetRoom() {

                                @Override
                                public void onChatGetRoom(final ProtoGlobal.Room room) {
                                    DbManager.getInstance().doRealmTransaction(realm2 -> {
                                        if (realm2.where(RealmRoom.class).equalTo("id", room.getId()).findFirst() == null) {
                                            RealmRoom realmRoom1 = RealmRoom.putOrUpdate(room, realm2);
                                            realmRoom1.setDeleted(true);
                                        } else {
                                            RealmRoom.putOrUpdate(room, realm2);
                                        }
                                    });
                                    G.handler.postDelayed(new Runnable() {

                                        @Override
                                        public void run() {
                                            new GoToChatActivity(room.getId()).setPeerID(peerId).startActivity(activity);
                                            G.onChatGetRoom = null;
                                        }
                                    }, 500);
                                }

                                @Override
                                public void onChatGetRoomTimeOut() {
                                }

                                @Override
                                public void onChatGetRoomError(int majorCode, int minorCode) {
                                }
                            };
                            new RequestChatGetRoom().chatGetRoom(peerId);
                        }
                    }
                });
            }
            break;
        case profile:
            G.handler.post(new Runnable() {

                @Override
                public void run() {
                    FragmentContactsProfile contactsProfile = new FragmentContactsProfile();
                    Bundle bundle = new Bundle();
                    bundle.putLong("peerId", peerId);
                    bundle.putLong("RoomId", roomId);
                    bundle.putString("enterFrom", GROUP.toString());
                    contactsProfile.setArguments(bundle);
                    new HelperFragment(activity.getSupportFragmentManager(), contactsProfile).setReplace(false).load();
                }
            });
            break;
    }
}
Also used : RequestChatGetRoom(net.iGap.request.RequestChatGetRoom) Bundle(android.os.Bundle) FragmentContactsProfile(net.iGap.fragments.FragmentContactsProfile) DbManager(net.iGap.module.accountManager.DbManager) OnChatGetRoom(net.iGap.observers.interfaces.OnChatGetRoom) RealmRoom(net.iGap.realm.RealmRoom) CustomTabsHelperFragment(me.zhanghai.android.customtabshelper.CustomTabsHelperFragment) Realm(io.realm.Realm) RequestClientGetRoom(net.iGap.request.RequestClientGetRoom) RealmRoom(net.iGap.realm.RealmRoom) OnChatGetRoom(net.iGap.observers.interfaces.OnChatGetRoom) RequestChatGetRoom(net.iGap.request.RequestChatGetRoom) ProtoClientGetRoom(net.iGap.proto.ProtoClientGetRoom)

Aggregations

RealmRoom (net.iGap.realm.RealmRoom)80 ArrayList (java.util.ArrayList)28 ProtoGlobal (net.iGap.proto.ProtoGlobal)27 RealmRoomMessage (net.iGap.realm.RealmRoomMessage)27 DbManager (net.iGap.module.accountManager.DbManager)26 View (android.view.View)23 G (net.iGap.G)23 List (java.util.List)19 R (net.iGap.R)19 SuppressLint (android.annotation.SuppressLint)18 TextView (android.widget.TextView)18 AccountManager (net.iGap.module.accountManager.AccountManager)18 RealmRegisteredInfo (net.iGap.realm.RealmRegisteredInfo)18 Bundle (android.os.Bundle)17 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)17 RecyclerView (androidx.recyclerview.widget.RecyclerView)16 AvatarHandler (net.iGap.helper.avatar.AvatarHandler)16 Context (android.content.Context)15 NonNull (androidx.annotation.NonNull)15 HelperFragment (net.iGap.helper.HelperFragment)15