Search in sources :

Example 26 with RealmStoryProto

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

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