Search in sources :

Example 16 with RealmStoryProto

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

the class MessageDataStorage method deleteExpiredStories.

public void deleteExpiredStories() {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    storageQueue.postRunnable(() -> {
        try {
            database.executeTransaction(realm -> {
                RealmResults<RealmStoryProto> realmStoryProtos = realm.where(RealmStoryProto.class).lessThan("createdAt", System.currentTimeMillis() - MILLIS_PER_DAY).equalTo("status", MessageObject.STATUS_SENT).findAll();
                if (realmStoryProtos != null && realmStoryProtos.size() > 0) {
                    for (int i = 0; i < realmStoryProtos.size(); i++) {
                        if (realmStoryProtos.get(i).getFile() != null) {
                            String filepath = realmStoryProtos.get(i).getFile().getLocalFilePath() != null ? realmStoryProtos.get(i).getFile().getLocalFilePath() : AndroidUtils.getFilePathWithCashId(realmStoryProtos.get(i).getFile().getCacheId(), realmStoryProtos.get(i).getFile().getName(), ProtoGlobal.RoomMessageType.STORY);
                            if (filepath != null) {
                                File file = new File(filepath);
                                if (file.exists())
                                    file.delete();
                            }
                        }
                    }
                    realmStoryProtos.deleteAllFromRealm();
                }
                RealmResults<RealmStory> realmStories = realm.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).findAll();
                if (realmStories != null && realmStories.size() > 0) {
                    for (RealmStory realmStory : realmStories) {
                        if (realmStory != null && realmStory.getRealmStoryProtos().size() == 0) {
                            realmStory.deleteFromRealm();
                        }
                    }
                }
            });
            countDownLatch.countDown();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : RealmStoryProto(net.iGap.realm.RealmStoryProto) RealmStory(net.iGap.realm.RealmStory) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File)

Example 17 with RealmStoryProto

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

the class MessageDataStorage method getStoryByStatus.

public List<StoryObject> getStoryByStatus(long userId, long roomId, int status, boolean isNotNullToken, boolean isForRoom, String[] fieldSort) {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    List<RealmStoryProto> stories = new ArrayList<>();
    List<StoryObject> storyObjects = new ArrayList<>();
    storageQueue.postRunnable(() -> {
        try {
            if (isNotNullToken) {
                if (fieldSort == null) {
                    if (isForRoom) {
                        stories.addAll(database.where(RealmStoryProto.class).equalTo("isForRoom", true).equalTo("roomId", roomId).equalTo("userId", userId).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", status).isNotNull("fileToken").findAll());
                    } else {
                        stories.addAll(database.where(RealmStoryProto.class).equalTo("userId", userId).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", status).isNotNull("fileToken").findAll());
                    }
                } else {
                    if (isForRoom) {
                        stories.addAll(database.where(RealmStoryProto.class).equalTo("isForRoom", true).equalTo("roomId", roomId).equalTo("userId", userId).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", status).isNotNull("fileToken").findAll().sort(fieldSort, new Sort[] { Sort.ASCENDING }));
                    } else {
                        stories.addAll(database.where(RealmStoryProto.class).equalTo("userId", userId).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", status).isNotNull("fileToken").findAll().sort(fieldSort, new Sort[] { Sort.ASCENDING }));
                    }
                }
                database.where(RealmStoryProto.class).equalTo("isForRoom", true).equalTo("roomId", roomId).equalTo("userId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).findAll().sort(new String[] { "createdAt", "index" }, new Sort[] { Sort.DESCENDING, Sort.DESCENDING });
            } else {
                if (isForRoom && roomId != 0) {
                    stories.addAll(database.where(RealmStoryProto.class).equalTo("isForRoom", true).equalTo("roomId", roomId).equalTo("userId", userId).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", status).findAll());
                } else {
                    stories.addAll(database.where(RealmStoryProto.class).equalTo("userId", userId).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", status).findAll());
                }
            }
            for (int i = 0; i < stories.size(); i++) {
                storyObjects.add(StoryObject.create(database.copyFromRealm(stories.get(i))));
            }
            countDownLatch.countDown();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return storyObjects;
}
Also used : MainStoryObject(net.iGap.story.MainStoryObject) StoryObject(net.iGap.story.StoryObject) RealmStoryProto(net.iGap.realm.RealmStoryProto) ArrayList(java.util.ArrayList) Sort(io.realm.Sort) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 18 with RealmStoryProto

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

the class MessageDataStorage method storySetDisplayName.

public void storySetDisplayName(long roomId, String displayName, boolean isVerified) {
    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", roomId).findFirst();
                if (realmStory != null) {
                    realmStory.setDisplayName(displayName);
                    if (realmStory.getRealmStoryProtos() != null && realmStory.getRealmStoryProtos().size() > 0) {
                        for (int i = 0; i < realmStory.getRealmStoryProtos().size(); i++) {
                            if (realmStory.getRealmStoryProtos().get(i) != null) {
                                realmStory.getRealmStoryProtos().get(i).setDisplayName(displayName);
                                realmStory.getRealmStoryProtos().get(i).setVerified(isVerified);
                            }
                        }
                    }
                    G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.STORY_ROOM_INFO, roomId));
                } else {
                    RealmResults<RealmStoryProto> realmStoryProtos = realm.where(RealmStoryProto.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("roomId", roomId).findAll();
                    if (realmStoryProtos != null && realmStoryProtos.size() > 0) {
                        for (int i = 0; i < realmStoryProtos.size(); i++) {
                            realmStoryProtos.get(i).setDisplayName(displayName);
                            realmStoryProtos.get(i).setVerified(isVerified);
                        }
                        G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.STORY_ROOM_INFO, roomId));
                    }
                }
            });
            countDownLatch.countDown();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : RealmStoryProto(net.iGap.realm.RealmStoryProto) RealmStory(net.iGap.realm.RealmStory) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 19 with RealmStoryProto

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

the class MessageDataStorage method getStoryWithIndexSort.

public List<StoryObject> getStoryWithIndexSort(long userId) {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    List<StoryObject> storyObjects = new ArrayList<>();
    storageQueue.postRunnable(() -> {
        try {
            database.beginTransaction();
            List<RealmStoryProto> realmStories = database.where(RealmStoryProto.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("userId", userId).findAll().sort("index", Sort.DESCENDING);
            if (realmStories != null && realmStories.size() > 0) {
                for (int i = 0; i < realmStories.size(); i++) {
                    storyObjects.add(StoryObject.create(realmStories.get(i)));
                }
            }
            database.commitTransaction();
            countDownLatch.countDown();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return storyObjects;
}
Also used : MainStoryObject(net.iGap.story.MainStoryObject) StoryObject(net.iGap.story.StoryObject) RealmStoryProto(net.iGap.realm.RealmStoryProto) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 20 with RealmStoryProto

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

the class MessageDataStorage method updateUserAddedStoryWithStoryObjects.

public void updateUserAddedStoryWithStoryObjects(final List<ProtoStoryGetStories.GroupedStories> stories) {
    storageQueue.postRunnable(() -> {
        try {
            database.beginTransaction();
            List<StoryObject> storyObjects = new ArrayList<>();
            if (stories.size() > 0 && stories.size() >= database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).findAll().size()) {
                for (int i = 0; i < stories.size(); i++) {
                    if (stories.get(i).getStoriesList().size() > 0) {
                        RealmRegisteredInfo realmRegisteredInfo = database.where(RealmRegisteredInfo.class).equalTo("id", stories.get(i).getOriginatorId()).findFirst();
                        boolean isForRoom = false;
                        RealmRoom realmRoom = null;
                        for (int j = 0; j < stories.get(i).getStoriesList().size(); j++) {
                            if (stories.get(i).getStoriesList().get(j).getRoomId() != 0) {
                                realmRoom = database.where(RealmRoom.class).equalTo("id", stories.get(i).getStoriesList().get(j).getRoomId()).findFirst();
                                isForRoom = true;
                                if (realmRoom == null) {
                                    new RequestClientGetRoom().clientGetRoom(stories.get(i).getStoriesList().get(j).getRoomId(), RequestClientGetRoom.CreateRoomMode.justInfo);
                                }
                            } else {
                                isForRoom = false;
                            }
                            storyObjects.add(StoryObject.create(stories.get(i).getStoriesList().get(j), j, stories.get(i).getSelf() && isForRoom && realmRoom != null ? realmRoom.getTitle() : stories.get(i).getOriginatorName(), !stories.get(i).getSelf() && isForRoom ? realmRoom != null ? realmRoom.getColor() : "#4aca69" : realmRegisteredInfo != null ? realmRegisteredInfo.getColor() : "#4aca69", isForRoom, isForRoom && realmRoom != null && realmRoom.getType() == ProtoGlobal.Room.Type.CHANNEL && realmRoom.getChannelRoom().isVerified()));
                        }
                        putStoriesToDatabase(database, stories.get(i).getSeenAllGroupStories(), stories.get(i).getOriginatorId(), storyObjects, stories.get(i).getOriginatorName(), realmRegisteredInfo != null ? realmRegisteredInfo.getColor() : "#4aca69", stories.get(i).getOriginatorValue(), !stories.get(i).getSelf() && stories.get(i).getOriginatorValue() == 1 && (realmRoom != null && realmRoom.getType() == ProtoGlobal.Room.Type.CHANNEL) && realmRoom.getChannelRoom().isVerified());
                        storyObjects.removeAll(storyObjects);
                    } else {
                        RealmStory realmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", stories.get(i).getOriginatorId()).findFirst();
                        if (realmStory != null) {
                            realmStory.deleteFromRealm();
                        }
                    }
                }
            } else if (stories.size() != 0 && stories.size() < database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).findAll().size()) {
                boolean isExist = false;
                List<RealmStory> realmStories = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).findAll();
                if (realmStories != null && realmStories.size() > 0) {
                    for (int i = 0; i < realmStories.size(); i++) {
                        for (int j = 0; j < stories.size(); j++) {
                            if (stories.get(j).getOriginatorValue() == 0) {
                                if (realmStories.get(i).getUserId() == stories.get(j).getOriginatorId()) {
                                    isExist = true;
                                    break;
                                }
                            } else {
                                if (realmStories.get(i).getRoomId() == stories.get(j).getOriginatorId()) {
                                    isExist = true;
                                    break;
                                }
                            }
                        }
                        if (!isExist) {
                            long userId = realmStories.get(i).getUserId();
                            long roomId = realmStories.get(i).getRoomId();
                            int orginatorValue = realmStories.get(i).getOrginatorValue();
                            RealmStory realmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo(realmStories.get(i).getOrginatorValue() == 0 ? "userId" : "roomId", realmStories.get(i).getOrginatorValue() == 0 ? userId : roomId).findFirst();
                            if (realmStory != null && realmStory.isSentAll()) {
                                realmStory.deleteFromRealm();
                            }
                            RealmResults<RealmStoryProto> realmStoryProtos = database.where(RealmStoryProto.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo(orginatorValue == 0 ? "userId" : "roomId", userId).equalTo("status", MessageObject.STATUS_SENT).findAll();
                            if (realmStoryProtos != null && realmStoryProtos.size() > 0) {
                                for (int y = 0; y < realmStoryProtos.size(); y++) {
                                    if (realmStoryProtos.get(y).getFile() != null) {
                                        String filepath = realmStoryProtos.get(y).getFile().getLocalFilePath() != null ? realmStoryProtos.get(y).getFile().getLocalFilePath() : AndroidUtils.getFilePathWithCashId(realmStoryProtos.get(y).getFile().getCacheId(), realmStoryProtos.get(y).getFile().getName(), ProtoGlobal.RoomMessageType.STORY);
                                        if (filepath != null) {
                                            File file = new File(filepath);
                                            if (file.exists())
                                                file.delete();
                                        }
                                    }
                                }
                                realmStoryProtos.deleteAllFromRealm();
                            }
                        }
                        isExist = false;
                    }
                }
                for (int i = 0; i < stories.size(); i++) {
                    if (stories.get(i).getStoriesList().size() > 0) {
                        RealmRegisteredInfo realmRegisteredInfo = database.where(RealmRegisteredInfo.class).equalTo("id", stories.get(i).getOriginatorId()).findFirst();
                        boolean isForRoom = false;
                        RealmRoom realmRoom = null;
                        for (int j = 0; j < stories.get(i).getStoriesList().size(); j++) {
                            if (stories.get(i).getStoriesList().get(j).getRoomId() != 0) {
                                realmRoom = database.where(RealmRoom.class).equalTo("id", stories.get(i).getStoriesList().get(j).getRoomId()).findFirst();
                                isForRoom = true;
                                if (realmRoom == null) {
                                    new RequestClientGetRoom().clientGetRoom(stories.get(i).getStoriesList().get(j).getRoomId(), RequestClientGetRoom.CreateRoomMode.justInfo);
                                }
                            } else {
                                isForRoom = false;
                            }
                            storyObjects.add(StoryObject.create(stories.get(i).getStoriesList().get(j), j, stories.get(i).getSelf() && isForRoom && realmRoom != null ? realmRoom.getTitle() : stories.get(i).getOriginatorName(), !stories.get(i).getSelf() && isForRoom ? realmRoom != null ? realmRoom.getColor() : "#4aca69" : realmRegisteredInfo != null ? realmRegisteredInfo.getColor() : "#4aca69", isForRoom, isForRoom && realmRoom != null && realmRoom.getType() == ProtoGlobal.Room.Type.CHANNEL && realmRoom.getChannelRoom().isVerified()));
                        }
                        putStoriesToDatabase(database, stories.get(i).getSeenAllGroupStories(), stories.get(i).getOriginatorId(), storyObjects, stories.get(i).getOriginatorName(), realmRegisteredInfo != null ? realmRegisteredInfo.getColor() : "#4aca69", stories.get(i).getOriginatorValue(), !stories.get(i).getSelf() && stories.get(i).getOriginatorValue() == 1 && (realmRoom != null && realmRoom.getType() == ProtoGlobal.Room.Type.CHANNEL) && realmRoom.getChannelRoom().isVerified());
                        storyObjects.removeAll(storyObjects);
                    } else {
                        RealmStory realmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", stories.get(i).getOriginatorId()).findFirst();
                        if (realmStory != null) {
                            realmStory.deleteFromRealm();
                        }
                    }
                }
            } else if (stories.size() == 0) {
                RealmResults<RealmStoryProto> realmStoryProtos = database.where(RealmStoryProto.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", MessageObject.STATUS_SENT).findAll();
                if (realmStoryProtos != null && realmStoryProtos.size() > 0) {
                    for (int y = 0; y < realmStoryProtos.size(); y++) {
                        if (realmStoryProtos.get(y).getFile() != null) {
                            String filepath = realmStoryProtos.get(y).getFile().getLocalFilePath() != null ? realmStoryProtos.get(y).getFile().getLocalFilePath() : AndroidUtils.getFilePathWithCashId(realmStoryProtos.get(y).getFile().getCacheId(), realmStoryProtos.get(y).getFile().getName(), ProtoGlobal.RoomMessageType.STORY);
                            if (filepath != null) {
                                File file = new File(filepath);
                                if (file.exists())
                                    file.delete();
                            }
                        }
                    }
                    realmStoryProtos.deleteAllFromRealm();
                }
                List<RealmStory> realmStories = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).findAll();
                if (realmStories != null && realmStories.size() > 0) {
                    for (int i = 0; i < realmStories.size(); i++) {
                        if (realmStories.get(i).getRealmStoryProtos().size() == 0) {
                            database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", realmStories.get(i).getUserId()).findAll().deleteAllFromRealm();
                        }
                    }
                }
            }
            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();
            UserLoginResponse.isFetched = true;
            G.runOnUiThread(() -> {
                if (G.onUnreadChange != null) {
                    G.onUnreadChange.onChange(storyUnReadCount[0], true);
                }
                StoryFragment.storyListFetched = false;
                getEventManager().postEvent(EventManager.STORY_LIST_FETCHED);
            });
        } catch (Exception e) {
            Log.e("Fskhfjksdhjkshdf", "updateUserAddedStoryWithStoryObjects: " + "/" + e.getMessage());
            FileLog.e(e);
        }
    });
}
Also used : MainStoryObject(net.iGap.story.MainStoryObject) StoryObject(net.iGap.story.StoryObject) RealmStory(net.iGap.realm.RealmStory) ArrayList(java.util.ArrayList) RealmRegisteredInfo(net.iGap.realm.RealmRegisteredInfo) RequestClientGetRoom(net.iGap.request.RequestClientGetRoom) RealmStoryProto(net.iGap.realm.RealmStoryProto) List(java.util.List) ArrayList(java.util.ArrayList) RealmRoom(net.iGap.realm.RealmRoom) File(java.io.File) RealmResults(io.realm.RealmResults)

Aggregations

RealmStoryProto (net.iGap.realm.RealmStoryProto)26 CountDownLatch (java.util.concurrent.CountDownLatch)19 MainStoryObject (net.iGap.story.MainStoryObject)16 StoryObject (net.iGap.story.StoryObject)16 ArrayList (java.util.ArrayList)13 RealmStory (net.iGap.realm.RealmStory)12 File (java.io.File)6 Sort (io.realm.Sort)4 List (java.util.List)3 Context (android.content.Context)2 Canvas (android.graphics.Canvas)2 Color (android.graphics.Color)2 Paint (android.graphics.Paint)2 BitmapDrawable (android.graphics.drawable.BitmapDrawable)2 TextUtils (android.text.TextUtils)2 Log (android.util.Log)2 TypedValue (android.util.TypedValue)2 Gravity (android.view.Gravity)2 View (android.view.View)2 FrameLayout (android.widget.FrameLayout)2