Search in sources :

Example 21 with StoryObject

use of net.iGap.story.StoryObject 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)

Example 22 with StoryObject

use of net.iGap.story.StoryObject 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 23 with StoryObject

use of net.iGap.story.StoryObject in project iGap-Android by KianIranian-STDG.

the class MessageDataStorage method getCurrentUserStories.

public List<StoryObject> getCurrentUserStories(boolean needIsForRoom) {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    List<StoryObject> stories = new ArrayList<>();
    storageQueue.postRunnable(() -> {
        try {
            RealmResults<RealmStoryProto> realmStoryProto;
            if (needIsForRoom) {
                realmStoryProto = database.where(RealmStoryProto.class).equalTo("isForRoom", false).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 {
                realmStoryProto = database.where(RealmStoryProto.class).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 });
            }
            for (int i = 0; i < realmStoryProto.size(); i++) {
                stories.add(StoryObject.create(realmStoryProto.get(i)));
            }
            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) RealmStoryProto(net.iGap.realm.RealmStoryProto) ArrayList(java.util.ArrayList) Sort(io.realm.Sort) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 24 with StoryObject

use of net.iGap.story.StoryObject in project iGap-Android by KianIranian-STDG.

the class MessageDataStorage method getStoryById.

public MainStoryObject getStoryById(long userId, boolean needSort) {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    List<MainStoryObject> stories = new ArrayList<>();
    storageQueue.postRunnable(() -> {
        FileLog.i(TAG, "getStoryById: " + "userId: " + userId + " needSort: " + needSort);
        try {
            List<StoryObject> storyObjects = new ArrayList<>();
            MainStoryObject mainStoryObject;
            RealmStory realmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", userId).findFirst();
            if (realmStory != null) {
                if (needSort) {
                    RealmResults<RealmStoryProto> realmStoryProtos = realmStory.getRealmStoryProtos().sort(new String[] { "createdAt", "index" }, new Sort[] { Sort.DESCENDING, Sort.DESCENDING });
                    for (int i = 0; i < realmStoryProtos.size(); i++) {
                        storyObjects.add(StoryObject.create(realmStoryProtos.get(i)));
                    }
                    mainStoryObject = MainStoryObject.create(realmStory);
                    mainStoryObject.storyObjects = storyObjects;
                } else {
                    mainStoryObject = MainStoryObject.create(realmStory);
                }
                stories.add(mainStoryObject);
            }
            countDownLatch.countDown();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return (stories.size() > 0 ? stories.get(0) : null);
}
Also used : MainStoryObject(net.iGap.story.MainStoryObject) StoryObject(net.iGap.story.StoryObject) RealmStoryProto(net.iGap.realm.RealmStoryProto) RealmStory(net.iGap.realm.RealmStory) ArrayList(java.util.ArrayList) MainStoryObject(net.iGap.story.MainStoryObject) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

StoryObject (net.iGap.story.StoryObject)24 MainStoryObject (net.iGap.story.MainStoryObject)22 ArrayList (java.util.ArrayList)18 RealmStoryProto (net.iGap.realm.RealmStoryProto)15 CountDownLatch (java.util.concurrent.CountDownLatch)12 RealmStory (net.iGap.realm.RealmStory)8 Sort (io.realm.Sort)4 List (java.util.List)4 RealmAttachment (net.iGap.realm.RealmAttachment)3 View (android.view.View)2 LinearLayout (android.widget.LinearLayout)2 TextView (android.widget.TextView)2 File (java.io.File)2 IconView (net.iGap.messenger.ui.components.IconView)2 CircleImageView (net.iGap.module.CircleImageView)2 FontIconTextView (net.iGap.module.FontIconTextView)2 MaterialDesignTextView (net.iGap.module.MaterialDesignTextView)2 ProtoStoryUserAddNew (net.iGap.proto.ProtoStoryUserAddNew)2 RealmRegisteredInfo (net.iGap.realm.RealmRegisteredInfo)2 ImageLoadingView (net.iGap.story.liststories.ImageLoadingView)2