Search in sources :

Example 16 with RealmStory

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

the class MessageDataStorage method updateUserAddedStory.

public void updateUserAddedStory(final List<ProtoGlobal.Story> stories) {
    storageQueue.postRunnable(() -> {
        FileLog.i(TAG, "updateUserAddedStory userId " + stories.get(0).getUserId() + " storiesId " + stories.get(0).getId());
        try {
            if (stories.get(0).getTypeValue() == 0) {
                database.beginTransaction();
                RealmRegisteredInfo realmRegisteredInfo = database.where(RealmRegisteredInfo.class).equalTo("id", stories.get(0).getUserId()).findFirst();
                if (realmRegisteredInfo != null && realmRegisteredInfo.getDisplayName() != null) {
                    RealmStory realmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", stories.get(0).getUserId()).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) {
                            storyObjects.add(StoryObject.create(stories.get(i), i, realmRegisteredInfo.getDisplayName(), realmRegisteredInfo != null ? realmRegisteredInfo.getColor() : "#4aca69", false, false));
                        }
                    }
                    realmStory.setLastCreatedAt(storyObjects.get(storyObjects.size() - 1).createdAt);
                    realmStory.setDisplayName(realmRegisteredInfo.getDisplayName());
                    realmStory.setProfileColor(realmRegisteredInfo.getColor());
                    realmStory.setSessionId(AccountManager.getInstance().getCurrentUser().getId());
                    realmStory.setUserId(stories.get(0).getUserId());
                    realmStory.setOrginatorValue(0);
                    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 RequestUserInfo().userInfo(stories.get(0).getUserId());
                }
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : MainStoryObject(net.iGap.story.MainStoryObject) StoryObject(net.iGap.story.StoryObject) RequestUserInfo(net.iGap.request.RequestUserInfo) RealmStory(net.iGap.realm.RealmStory) ArrayList(java.util.ArrayList) RealmRegisteredInfo(net.iGap.realm.RealmRegisteredInfo)

Example 17 with RealmStory

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

the class MessageDataStorage method isHaveUnSeenStory.

public boolean isHaveUnSeenStory() {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    boolean[] result = new boolean[1];
    storageQueue.postRunnable(() -> {
        try {
            database.executeTransaction(realm -> {
                RealmResults<RealmStory> realmStory = realm.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).notEqualTo("userId", AccountManager.getInstance().getCurrentUser().getId()).findAll();
                if (realmStory != null && realmStory.size() > 0) {
                    for (int i = 0; i < realmStory.size(); i++) {
                        if (!realmStory.get(i).isSeenAll()) {
                            result[0] = true;
                            break;
                        }
                    }
                }
            });
            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)

Example 18 with RealmStory

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

the class MessageDataStorage method updateStorySentStatus.

public void updateStorySentStatus(long userId, boolean status) {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    storageQueue.postRunnable(() -> {
        FileLog.i(TAG, "updateStorySentStatus: " + "userId: " + userId + " status: " + status);
        try {
            database.executeTransaction(realm -> {
                RealmStory realmStory = realm.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", userId).findFirst();
                if (realmStory != null) {
                    realmStory.setSentAll(status);
                }
            });
            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 19 with RealmStory

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

the class MessageDataStorage method deleteUserStoryWithStoryId.

public void deleteUserStoryWithStoryId(long storyId, long userId) {
    storageQueue.postRunnable(() -> {
        FileLog.i(TAG, "deleteUserStoryId " + storyId);
        try {
            database.beginTransaction();
            int counter = 0;
            boolean[] isFromRoomMode = new boolean[1];
            RealmStoryProto realmStoryProto = database.where((RealmStoryProto.class)).equalTo("isForReply", false).equalTo("storyId", storyId).findFirst();
            if (realmStoryProto != null) {
                isFromRoomMode[0] = realmStoryProto.isForRoom();
                if (realmStoryProto.getFile() != null) {
                    String filepath = realmStoryProto.getFile().getLocalFilePath() != null ? realmStoryProto.getFile().getLocalFilePath() : AndroidUtils.getFilePathWithCashId(realmStoryProto.getFile().getCacheId(), realmStoryProto.getFile().getName(), ProtoGlobal.RoomMessageType.STORY);
                    if (filepath != null) {
                        File file = new File(filepath);
                        if (file.exists())
                            file.delete();
                    }
                }
                realmStoryProto.deleteFromRealm();
            }
            RealmStory userStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", userId).findFirst();
            if (userStory != null && userStory.getRealmStoryProtos() != null && userStory.getRealmStoryProtos().size() == 0) {
                userStory.deleteFromRealm();
            } else if (userStory != null && userStory.getRealmStoryProtos() != null && userStory.getRealmStoryProtos().size() > 0) {
                userStory.setLastCreatedAt(userStory.getRealmStoryProtos().get(userStory.getRealmStoryProtos().size() - 1).getCreatedAt() / 1000L);
                for (int i = 0; i < userStory.getRealmStoryProtos().size(); i++) {
                    if (userStory.getRealmStoryProtos().get(i).isSeen()) {
                        counter++;
                    }
                }
                if (counter == userStory.getRealmStoryProtos().size()) {
                    userStory.setSeenAll(true);
                }
                if (userStory.getIndexOfSeen() > userStory.getRealmStoryProtos().size()) {
                    userStory.setIndexOfSeen(0);
                }
                counter = 0;
            }
            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_DELETED, isFromRoomMode[0]);
            });
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : RealmStoryProto(net.iGap.realm.RealmStoryProto) RealmStory(net.iGap.realm.RealmStory) File(java.io.File)

Example 20 with RealmStory

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

the class MessageDataStorage method deleteUserStoryWithUploadId.

public void deleteUserStoryWithUploadId(long uploadId, long userId) {
    storageQueue.postRunnable(() -> {
        FileLog.i(TAG, "deleteUserStoryId " + uploadId);
        try {
            database.beginTransaction();
            boolean[] isFromRoomMode = new boolean[1];
            RealmStoryProto realmStoryProto = database.where((RealmStoryProto.class)).equalTo("isForReply", false).equalTo("id", uploadId).findFirst();
            if (realmStoryProto != null) {
                isFromRoomMode[0] = realmStoryProto.isForRoom();
                if (realmStoryProto.getFile() != null) {
                    String filepath = realmStoryProto.getFile().getLocalFilePath() != null ? realmStoryProto.getFile().getLocalFilePath() : AndroidUtils.getFilePathWithCashId(realmStoryProto.getFile().getCacheId(), realmStoryProto.getFile().getName(), ProtoGlobal.RoomMessageType.STORY);
                    if (filepath != null) {
                        File file = new File(filepath);
                        if (file.exists())
                            file.delete();
                    }
                }
                realmStoryProto.deleteFromRealm();
            }
            RealmStory userStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", userId).findFirst();
            if (userStory.getRealmStoryProtos().size() == 0) {
                userStory.deleteFromRealm();
            } else {
                if (database.where(RealmStoryProto.class).equalTo("userId", userId).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", MessageObject.STATUS_SENDING).findAll().size() > 0 || database.where(RealmStoryProto.class).equalTo("userId", userId).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", MessageObject.STATUS_FAILED).findAll().size() > 0) {
                    userStory.setSentAll(false);
                } else {
                    userStory.setSentAll(true);
                }
            }
            database.commitTransaction();
            G.runOnUiThread(() -> getEventManager().postEvent(EventManager.STORY_DELETED, isFromRoomMode[0]));
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : RealmStoryProto(net.iGap.realm.RealmStoryProto) RealmStory(net.iGap.realm.RealmStory) File(java.io.File)

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