Search in sources :

Example 1 with RealmStoryProto

use of net.iGap.realm.RealmStoryProto 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 RealmStoryProto

use of net.iGap.realm.RealmStoryProto 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 RealmStoryProto

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

the class MessageDataStorage method storySetSeen.

public void storySetSeen(long storyId) {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    storageQueue.postRunnable(() -> {
        try {
            database.executeTransaction(realm -> {
                RealmStoryProto realmStoryProto = realm.where(RealmStoryProto.class).equalTo("isForReply", false).equalTo("storyId", storyId).findFirst();
                if (realmStoryProto != null) {
                    realmStoryProto.setSeen(true);
                }
            });
            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) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with RealmStoryProto

use of net.iGap.realm.RealmStoryProto 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 RealmStoryProto

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

the class MessageDataStorage method updateOwnViews.

public void updateOwnViews(List<ProtoStoryGetOwnStoryViews.GroupedViews> groupedViews) {
    CountDownLatch countdown = new CountDownLatch(1);
    storageQueue.postRunnable(() -> {
        try {
            database.executeTransaction(realm -> {
                int counter = 0;
                for (int i = 0; i < groupedViews.size(); i++) {
                    // for (int j = 0; j < groupedViews.get(i).getStoryViewsList().size(); j++) {
                    // if (groupedViews.get(i).getStoryViewsList().get(j).getUserId() != AccountManager.getInstance().getCurrentUser().getId()) {
                    // counter++;
                    // }
                    // }
                    RealmStoryProto realmStoryProto = realm.where(RealmStoryProto.class).equalTo("isForReply", false).equalTo("storyId", groupedViews.get(i).getStoryId()).findFirst();
                    if (realmStoryProto != null) {
                        // realmStoryProto.setViewCount(counter);
                        boolean isExist = false;
                        for (int j = 0; j < groupedViews.get(i).getStoryViewsList().size(); j++) {
                            RealmRegisteredInfo realmRegisteredInfo = database.where(RealmRegisteredInfo.class).equalTo("id", groupedViews.get(i).getStoryViewsList().get(j).getUserId()).findFirst();
                            RealmStoryViewInfo realmStoryViewInfo;
                            realmStoryViewInfo = realm.where(RealmStoryViewInfo.class).equalTo("id", groupedViews.get(i).getStoryId()).equalTo("userId", groupedViews.get(i).getStoryViewsList().get(j).getUserId()).findFirst();
                            if (realmStoryViewInfo == null) {
                                realmStoryViewInfo = realm.createObject(RealmStoryViewInfo.class);
                            } else {
                                isExist = true;
                            }
                            if (realmRegisteredInfo == null) {
                                realmStoryViewInfo.setDisplayName("");
                                new RequestUserInfo().userInfo(groupedViews.get(i).getStoryViewsList().get(j).getUserId());
                            } else {
                                realmStoryViewInfo.setDisplayName(realmRegisteredInfo.getDisplayName());
                                realmStoryViewInfo.setProfileColor(realmRegisteredInfo.getColor());
                            }
                            realmStoryViewInfo.setId(groupedViews.get(i).getStoryId());
                            realmStoryViewInfo.setUserId(groupedViews.get(i).getStoryViewsList().get(j).getUserId());
                            realmStoryViewInfo.setCreatedTime(groupedViews.get(i).getStoryViewsList().get(j).getViewedAt());
                            if (isExist) {
                                realmStoryProto.getRealmStoryViewInfos().remove(realmStoryViewInfo);
                            }
                            realmStoryProto.getRealmStoryViewInfos().add(realmStoryViewInfo);
                            isExist = false;
                        }
                    }
                    counter = 0;
                }
            });
            G.runOnUiThread(() -> getEventManager().postEvent(EventManager.STORY_VIEWS_FETCHED));
            countdown.countDown();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            countdown.countDown();
        }
    });
    try {
        countdown.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : RealmStoryProto(net.iGap.realm.RealmStoryProto) RequestUserInfo(net.iGap.request.RequestUserInfo) RealmStoryViewInfo(net.iGap.realm.RealmStoryViewInfo) CountDownLatch(java.util.concurrent.CountDownLatch) RealmRegisteredInfo(net.iGap.realm.RealmRegisteredInfo)

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