Search in sources :

Example 1 with RealmStory

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

the class MessageDataStorage method getSortedStoryObjectsInMainStoryObject.

public List<MainStoryObject> getSortedStoryObjectsInMainStoryObject(long userId, String[] sortBy, Sort[] orderBy) {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    List<MainStoryObject> stories = new ArrayList<>();
    storageQueue.postRunnable(() -> {
        try {
            RealmResults<RealmStoryProto> realmStoryProtos;
            RealmResults<RealmStory> realmStory;
            RealmStory myRealmStory = null;
            RealmStory igapRealmStory = null;
            boolean isExistIgapRoomStory = false;
            igapRealmStory = database.where(RealmStory.class).equalTo("roomId", 2901).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).findFirst();
            if (igapRealmStory != null && igapRealmStory.getRealmStoryProtos().size() > 0) {
                isExistIgapRoomStory = true;
            }
            if (userId == 0) {
                myRealmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", AccountManager.getInstance().getCurrentUser().getId()).findFirst();
                realmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).notEqualTo("userId", AccountManager.getInstance().getCurrentUser().getId()).sort("lastCreatedAt", Sort.DESCENDING).findAll();
            } else {
                realmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", userId).findAll();
            }
            if (realmStory != null && realmStory.size() > 0) {
                for (int i = 0; i < realmStory.size(); i++) {
                    List<StoryObject> storyObjects = new ArrayList<>();
                    realmStoryProtos = realmStory.get(i).getRealmStoryProtos().sort(sortBy, orderBy);
                    if (isExistIgapRoomStory && realmStory.get(i).getRoomId() == 2901) {
                        stories.add(0, MainStoryObject.create(realmStory.get(i)));
                    } else {
                        stories.add(MainStoryObject.create(realmStory.get(i)));
                    }
                    for (int j = 0; j < realmStoryProtos.size(); j++) {
                        storyObjects.add(StoryObject.create(realmStoryProtos.get(j)));
                    }
                    stories.get(isExistIgapRoomStory && realmStory.get(i).getRoomId() == 2901 ? 0 : i).storyObjects = storyObjects;
                }
                if (userId == 0 && myRealmStory != null) {
                    List<StoryObject> storyObjects = new ArrayList<>();
                    realmStoryProtos = myRealmStory.getRealmStoryProtos().sort(sortBy, orderBy);
                    stories.add(0, MainStoryObject.create(myRealmStory));
                    for (int j = 0; j < realmStoryProtos.size(); j++) {
                        storyObjects.add(StoryObject.create(realmStoryProtos.get(j)));
                    }
                    stories.get(0).storyObjects = storyObjects;
                }
            } else {
                if (userId == 0 && myRealmStory != null) {
                    List<StoryObject> storyObjects = new ArrayList<>();
                    realmStoryProtos = myRealmStory.getRealmStoryProtos().sort(sortBy, orderBy);
                    stories.add(0, MainStoryObject.create(myRealmStory));
                    for (int j = 0; j < realmStoryProtos.size(); j++) {
                        storyObjects.add(StoryObject.create(realmStoryProtos.get(j)));
                    }
                    stories.get(0).storyObjects = storyObjects;
                }
            }
            countDownLatch.countDown();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return stories;
}
Also used : MainStoryObject(net.iGap.story.MainStoryObject) StoryObject(net.iGap.story.StoryObject) RealmStory(net.iGap.realm.RealmStory) ArrayList(java.util.ArrayList) MainStoryObject(net.iGap.story.MainStoryObject) CountDownLatch(java.util.concurrent.CountDownLatch) RealmStoryProto(net.iGap.realm.RealmStoryProto)

Example 2 with RealmStory

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

the class MessageDataStorage method putStoriesToDatabase.

private void putStoriesToDatabase(Realm database, boolean isSeenAll, long userId, List<StoryObject> stories, String displayName, String profileColor, int orginatorValue, boolean isVerified) {
    try {
        RealmStory realmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo(orginatorValue == 0 ? "userId" : "roomId", userId).findFirst();
        if (realmStory == null) {
            realmStory = database.createObject(RealmStory.class, SUID.id().get());
            realmStory.setSeenAll(false);
        } else {
            boolean isExist = false;
            for (int i = 0; i < realmStory.getRealmStoryProtos().size(); i++) {
                for (int j = 0; j < stories.size(); j++) {
                    if (realmStory.getRealmStoryProtos().get(i).getStoryId() == stories.get(j).storyId) {
                        isExist = true;
                        break;
                    }
                }
                if (!isExist && realmStory.getRealmStoryProtos().get(i).getStoryId() != 0) {
                    database.where(RealmStoryProto.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", MessageObject.STATUS_SENT).equalTo("storyId", realmStory.getRealmStoryProtos().get(i).getStoryId()).findAll().deleteAllFromRealm();
                    RealmStory userStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", userId).findFirst();
                    if (userStory != null && userStory.getRealmStoryProtos().size() == 0) {
                        userStory.deleteFromRealm();
                    } else if (userStory != null && userStory.getIndexOfSeen() > stories.size()) {
                        userStory.setIndexOfSeen(0);
                    }
                }
                isExist = false;
            }
        }
        realmStory.setProfileColor(profileColor);
        realmStory.setVerified(isVerified);
        realmStory.setLastCreatedAt(stories.get(stories.size() - 1).createdAt);
        realmStory.setDisplayName(displayName);
        realmStory.setOrginatorValue(orginatorValue);
        realmStory.setSessionId(AccountManager.getInstance().getCurrentUser().getId());
        if (orginatorValue == 0) {
            realmStory.setUserId(userId);
        } else {
            realmStory.setRoomId(userId);
        }
        realmStory.setSeenAll(isSeenAll);
        realmStory.setRealmStoryProtos(database, stories);
    } catch (Exception e) {
        FileLog.e(e);
    }
}
Also used : RealmStory(net.iGap.realm.RealmStory)

Example 3 with RealmStory

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

the class MessageDataStorage method storySetIndexOfSeen.

public void storySetIndexOfSeen(long userId, long roomId, int position) {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    storageQueue.postRunnable(() -> {
        try {
            database.executeTransaction(realm -> {
                RealmStory realmStory = realm.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo(roomId != 0 ? "roomId" : "userId", roomId != 0 ? roomId : userId).findFirst();
                if (realmStory != null) {
                    realmStory.setIndexOfSeen(position);
                }
            });
            countDownLatch.countDown();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : RealmStory(net.iGap.realm.RealmStory) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with RealmStory

use of net.iGap.realm.RealmStory 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 5 with RealmStory

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

the class MessageDataStorage method isAllStorySeen.

public boolean isAllStorySeen(boolean isUser, long userId) {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    boolean[] result = new boolean[1];
    storageQueue.postRunnable(() -> {
        try {
            database.executeTransaction(realm -> {
                RealmStory realmStory = realm.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo(isUser ? "userId" : "roomId", userId).findFirst();
                if (realmStory != null) {
                    result[0] = realmStory.isSeenAll();
                }
            });
            countDownLatch.countDown();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return result[0];
}
Also used : RealmStory(net.iGap.realm.RealmStory) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

RealmStory (net.iGap.realm.RealmStory)23 CountDownLatch (java.util.concurrent.CountDownLatch)15 RealmStoryProto (net.iGap.realm.RealmStoryProto)10 ArrayList (java.util.ArrayList)8 MainStoryObject (net.iGap.story.MainStoryObject)8 StoryObject (net.iGap.story.StoryObject)7 File (java.io.File)4 RealmRegisteredInfo (net.iGap.realm.RealmRegisteredInfo)3 RealmRoom (net.iGap.realm.RealmRoom)3 RealmStoryViewInfo (net.iGap.realm.RealmStoryViewInfo)2 RequestClientGetRoom (net.iGap.request.RequestClientGetRoom)2 RequestUserInfo (net.iGap.request.RequestUserInfo)2 Fragment (androidx.fragment.app.Fragment)1 RealmResults (io.realm.RealmResults)1 List (java.util.List)1 DiscoveryFragment (net.iGap.fragments.discovery.DiscoveryFragment)1 PopularChannelHomeFragment (net.iGap.fragments.populaChannel.PopularChannelHomeFragment)1 PopularMoreChannelFragment (net.iGap.fragments.populaChannel.PopularMoreChannelFragment)1 HelperFragment (net.iGap.helper.HelperFragment)1 OnItemChangeListener (net.iGap.libs.bottomNavigation.Event.OnItemChangeListener)1