Search in sources :

Example 21 with RealmStoryProto

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

the class MessageDataStorage method updateStoryFileToken.

public void updateStoryFileToken(long messageId, String fileToken) {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    storageQueue.postRunnable(() -> {
        try {
            database.executeTransaction(realm -> {
                RealmStoryProto realmStoryProto = realm.where(RealmStoryProto.class).equalTo("isForReply", false).equalTo("id", messageId).findFirst();
                if (realmStoryProto != null) {
                    realmStoryProto.setFileToken(fileToken);
                }
            });
            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 22 with RealmStoryProto

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

the class MessageDataStorage method deleteUserStoryWithStoryId.

public void deleteUserStoryWithStoryId(long storyId, long userId) {
    storageQueue.postRunnable(() -> {
        FileLog.i(TAG, "deleteUserStoryId " + storyId);
        try {
            database.beginTransaction();
            int counter = 0;
            boolean[] isFromRoomMode = new boolean[1];
            RealmStoryProto realmStoryProto = database.where((RealmStoryProto.class)).equalTo("isForReply", false).equalTo("storyId", storyId).findFirst();
            if (realmStoryProto != null) {
                isFromRoomMode[0] = realmStoryProto.isForRoom();
                if (realmStoryProto.getFile() != null) {
                    String filepath = realmStoryProto.getFile().getLocalFilePath() != null ? realmStoryProto.getFile().getLocalFilePath() : AndroidUtils.getFilePathWithCashId(realmStoryProto.getFile().getCacheId(), realmStoryProto.getFile().getName(), ProtoGlobal.RoomMessageType.STORY);
                    if (filepath != null) {
                        File file = new File(filepath);
                        if (file.exists())
                            file.delete();
                    }
                }
                realmStoryProto.deleteFromRealm();
            }
            RealmStory userStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", userId).findFirst();
            if (userStory != null && userStory.getRealmStoryProtos() != null && userStory.getRealmStoryProtos().size() == 0) {
                userStory.deleteFromRealm();
            } else if (userStory != null && userStory.getRealmStoryProtos() != null && userStory.getRealmStoryProtos().size() > 0) {
                userStory.setLastCreatedAt(userStory.getRealmStoryProtos().get(userStory.getRealmStoryProtos().size() - 1).getCreatedAt() / 1000L);
                for (int i = 0; i < userStory.getRealmStoryProtos().size(); i++) {
                    if (userStory.getRealmStoryProtos().get(i).isSeen()) {
                        counter++;
                    }
                }
                if (counter == userStory.getRealmStoryProtos().size()) {
                    userStory.setSeenAll(true);
                }
                if (userStory.getIndexOfSeen() > userStory.getRealmStoryProtos().size()) {
                    userStory.setIndexOfSeen(0);
                }
                counter = 0;
            }
            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_DELETED, isFromRoomMode[0]);
            });
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : RealmStoryProto(net.iGap.realm.RealmStoryProto) RealmStory(net.iGap.realm.RealmStory) File(java.io.File)

Example 23 with RealmStoryProto

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

the class MessageDataStorage method deleteUserStoryWithUploadId.

public void deleteUserStoryWithUploadId(long uploadId, long userId) {
    storageQueue.postRunnable(() -> {
        FileLog.i(TAG, "deleteUserStoryId " + uploadId);
        try {
            database.beginTransaction();
            boolean[] isFromRoomMode = new boolean[1];
            RealmStoryProto realmStoryProto = database.where((RealmStoryProto.class)).equalTo("isForReply", false).equalTo("id", uploadId).findFirst();
            if (realmStoryProto != null) {
                isFromRoomMode[0] = realmStoryProto.isForRoom();
                if (realmStoryProto.getFile() != null) {
                    String filepath = realmStoryProto.getFile().getLocalFilePath() != null ? realmStoryProto.getFile().getLocalFilePath() : AndroidUtils.getFilePathWithCashId(realmStoryProto.getFile().getCacheId(), realmStoryProto.getFile().getName(), ProtoGlobal.RoomMessageType.STORY);
                    if (filepath != null) {
                        File file = new File(filepath);
                        if (file.exists())
                            file.delete();
                    }
                }
                realmStoryProto.deleteFromRealm();
            }
            RealmStory userStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", userId).findFirst();
            if (userStory.getRealmStoryProtos().size() == 0) {
                userStory.deleteFromRealm();
            } else {
                if (database.where(RealmStoryProto.class).equalTo("userId", userId).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", MessageObject.STATUS_SENDING).findAll().size() > 0 || database.where(RealmStoryProto.class).equalTo("userId", userId).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", MessageObject.STATUS_FAILED).findAll().size() > 0) {
                    userStory.setSentAll(false);
                } else {
                    userStory.setSentAll(true);
                }
            }
            database.commitTransaction();
            G.runOnUiThread(() -> getEventManager().postEvent(EventManager.STORY_DELETED, isFromRoomMode[0]));
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : RealmStoryProto(net.iGap.realm.RealmStoryProto) RealmStory(net.iGap.realm.RealmStory) File(java.io.File)

Example 24 with RealmStoryProto

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

the class MessageDataStorage method userAddViewStory.

public void userAddViewStory(long storyId, int viewdAt, long viewdUserId, long storyOwnerUserId) {
    storageQueue.postRunnable(() -> {
        FileLog.i(TAG, "deleteUserStoryId " + storyId);
        try {
            database.beginTransaction();
            RealmRegisteredInfo realmRegisteredInfo = database.where(RealmRegisteredInfo.class).equalTo("id", viewdUserId).findFirst();
            RealmStoryProto realmStoryProto = database.where(RealmStoryProto.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("storyId", storyId).findFirst();
            if (realmStoryProto != null) {
                realmStoryProto.setSeen(true);
                realmStoryProto.setViewCount(realmStoryProto.getViewCount() + 1);
                boolean isExist = false;
                RealmStoryViewInfo realmStoryViewInfo;
                realmStoryViewInfo = database.where(RealmStoryViewInfo.class).equalTo("id", storyId).equalTo("userId", viewdUserId).findFirst();
                if (realmStoryViewInfo == null) {
                    realmStoryViewInfo = database.createObject(RealmStoryViewInfo.class);
                } else {
                    isExist = true;
                }
                if (realmRegisteredInfo == null) {
                    realmStoryViewInfo.setDisplayName("");
                    new RequestUserInfo().userInfo(viewdUserId);
                } else {
                    realmStoryViewInfo.setProfileColor(realmRegisteredInfo.getColor());
                    realmStoryViewInfo.setDisplayName(realmRegisteredInfo.getDisplayName());
                }
                realmStoryViewInfo.setId(storyId);
                realmStoryViewInfo.setUserId(viewdUserId);
                realmStoryViewInfo.setCreatedTime(viewdAt);
                if (isExist) {
                    realmStoryProto.getRealmStoryViewInfos().remove(realmStoryViewInfo);
                }
                realmStoryProto.getRealmStoryViewInfos().add(realmStoryViewInfo);
                isExist = false;
            }
            RealmStory realmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", storyOwnerUserId).findFirst();
            if (realmStory != null) {
                int counter = 0;
                for (int i = 0; i < realmStory.getRealmStoryProtos().size(); i++) {
                    if (realmStory.getRealmStoryProtos().get(i).isSeen()) {
                        counter++;
                    }
                }
                if (counter == realmStory.getRealmStoryProtos().size()) {
                    RealmStory userRealmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", storyOwnerUserId).findFirst();
                    if (userRealmStory != null) {
                        userRealmStory.setSeenAll(true);
                    }
                }
            }
            database.commitTransaction();
            G.runOnUiThread(() -> getEventManager().postEvent(EventManager.STORY_USER_ADD_VIEW));
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : RealmStoryProto(net.iGap.realm.RealmStoryProto) RequestUserInfo(net.iGap.request.RequestUserInfo) RealmStory(net.iGap.realm.RealmStory) RealmStoryViewInfo(net.iGap.realm.RealmStoryViewInfo) RealmRegisteredInfo(net.iGap.realm.RealmRegisteredInfo)

Example 25 with RealmStoryProto

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

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