Search in sources :

Example 1 with StoryObject

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

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

the class MessageDataStorage method getCurrentUserStoryById.

public StoryObject getCurrentUserStoryById(long storyId) {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    StoryObject[] stories = new StoryObject[1];
    storageQueue.postRunnable(() -> {
        try {
            RealmStoryProto realmStoryProto = database.where(RealmStoryProto.class).equalTo("storyId", storyId).equalTo("userId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).findFirst();
            if (realmStoryProto != null) {
                stories[0] = StoryObject.create(realmStoryProto);
            }
            countDownLatch.countDown();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return stories[0];
}
Also used : MainStoryObject(net.iGap.story.MainStoryObject) StoryObject(net.iGap.story.StoryObject) RealmStoryProto(net.iGap.realm.RealmStoryProto) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with StoryObject

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

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

the class MessageDataStorage method getOtherUsersStories.

public List<MainStoryObject> getOtherUsersStories() {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    List<RealmStory> stories = new ArrayList<>();
    List<MainStoryObject> mainStoryObjects = new ArrayList<>();
    storageQueue.postRunnable(() -> {
        try {
            boolean isExistIgapRoomStory = false;
            RealmStory igapRealmStory = database.where(RealmStory.class).equalTo("roomId", 2901).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).findFirst();
            stories.addAll(database.where(RealmStory.class).notEqualTo("userId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).sort("lastCreatedAt", Sort.DESCENDING).findAll());
            if (igapRealmStory != null && igapRealmStory.getRealmStoryProtos().size() > 0) {
                isExistIgapRoomStory = true;
            }
            for (int i = 0; i < stories.size(); i++) {
                List<StoryObject> storyObjects = new ArrayList<>();
                RealmResults<RealmStoryProto> realmStoryProtos = stories.get(i).getRealmStoryProtos().sort(new String[] { "createdAt", "index" }, new Sort[] { Sort.DESCENDING, Sort.DESCENDING });
                if (realmStoryProtos != null && realmStoryProtos.size() > 0) {
                    for (int j = 0; j < realmStoryProtos.size(); j++) {
                        storyObjects.add(StoryObject.create(realmStoryProtos.get(j)));
                    }
                    MainStoryObject mainStoryObject = MainStoryObject.create(database.copyFromRealm(stories.get(i)));
                    mainStoryObject.storyObjects = storyObjects;
                    if (isExistIgapRoomStory && stories.get(i).getRoomId() == 2901) {
                        mainStoryObjects.add(0, mainStoryObject);
                    } else {
                        mainStoryObjects.add(mainStoryObject);
                    }
                }
            }
            countDownLatch.countDown();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return mainStoryObjects;
}
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 5 with StoryObject

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

the class MessageDataStorage method getCurrentUserRoomStories.

public List<StoryObject> getCurrentUserRoomStories(long roomId, int listMode) {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    List<StoryObject> stories = new ArrayList<>();
    storageQueue.postRunnable(() -> {
        try {
            RealmResults<RealmStoryProto> realmStoryProto;
            boolean isAbleToAdd = true;
            if (roomId == 0) {
                realmStoryProto = database.where(RealmStoryProto.class).equalTo("isForRoom", true).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("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 });
            }
            for (int i = 0; i < realmStoryProto.size(); i++) {
                if (listMode == 0) {
                    for (int j = 0; j < stories.size(); j++) {
                        if (realmStoryProto.get(i).getRoomId() == stories.get(j).roomId) {
                            isAbleToAdd = false;
                            break;
                        } else {
                            isAbleToAdd = true;
                        }
                    }
                    if (isAbleToAdd) {
                        stories.add(StoryObject.create(realmStoryProto.get(i)));
                    }
                } else {
                    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)

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