use of net.iGap.realm.RealmStory 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;
}
use of net.iGap.realm.RealmStory in project iGap-Android by KianIranian-STDG.
the class MessageDataStorage method putStoriesToDatabase.
private void putStoriesToDatabase(Realm database, boolean isSeenAll, long userId, List<StoryObject> stories, String displayName, String profileColor, int orginatorValue, boolean isVerified) {
try {
RealmStory realmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo(orginatorValue == 0 ? "userId" : "roomId", userId).findFirst();
if (realmStory == null) {
realmStory = database.createObject(RealmStory.class, SUID.id().get());
realmStory.setSeenAll(false);
} else {
boolean isExist = false;
for (int i = 0; i < realmStory.getRealmStoryProtos().size(); i++) {
for (int j = 0; j < stories.size(); j++) {
if (realmStory.getRealmStoryProtos().get(i).getStoryId() == stories.get(j).storyId) {
isExist = true;
break;
}
}
if (!isExist && realmStory.getRealmStoryProtos().get(i).getStoryId() != 0) {
database.where(RealmStoryProto.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", MessageObject.STATUS_SENT).equalTo("storyId", realmStory.getRealmStoryProtos().get(i).getStoryId()).findAll().deleteAllFromRealm();
RealmStory userStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", userId).findFirst();
if (userStory != null && userStory.getRealmStoryProtos().size() == 0) {
userStory.deleteFromRealm();
} else if (userStory != null && userStory.getIndexOfSeen() > stories.size()) {
userStory.setIndexOfSeen(0);
}
}
isExist = false;
}
}
realmStory.setProfileColor(profileColor);
realmStory.setVerified(isVerified);
realmStory.setLastCreatedAt(stories.get(stories.size() - 1).createdAt);
realmStory.setDisplayName(displayName);
realmStory.setOrginatorValue(orginatorValue);
realmStory.setSessionId(AccountManager.getInstance().getCurrentUser().getId());
if (orginatorValue == 0) {
realmStory.setUserId(userId);
} else {
realmStory.setRoomId(userId);
}
realmStory.setSeenAll(isSeenAll);
realmStory.setRealmStoryProtos(database, stories);
} catch (Exception e) {
FileLog.e(e);
}
}
use of net.iGap.realm.RealmStory in project iGap-Android by KianIranian-STDG.
the class MessageDataStorage method storySetIndexOfSeen.
public void storySetIndexOfSeen(long userId, long roomId, int position) {
CountDownLatch countDownLatch = new CountDownLatch(1);
storageQueue.postRunnable(() -> {
try {
database.executeTransaction(realm -> {
RealmStory realmStory = realm.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo(roomId != 0 ? "roomId" : "userId", roomId != 0 ? roomId : userId).findFirst();
if (realmStory != null) {
realmStory.setIndexOfSeen(position);
}
});
countDownLatch.countDown();
} catch (Exception e) {
FileLog.e(e);
} finally {
countDownLatch.countDown();
}
});
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
use of net.iGap.realm.RealmStory 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);
}
});
}
use of net.iGap.realm.RealmStory in project iGap-Android by KianIranian-STDG.
the class MessageDataStorage method isAllStorySeen.
public boolean isAllStorySeen(boolean isUser, long userId) {
CountDownLatch countDownLatch = new CountDownLatch(1);
boolean[] result = new boolean[1];
storageQueue.postRunnable(() -> {
try {
database.executeTransaction(realm -> {
RealmStory realmStory = realm.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo(isUser ? "userId" : "roomId", userId).findFirst();
if (realmStory != null) {
result[0] = realmStory.isSeenAll();
}
});
countDownLatch.countDown();
} catch (Exception e) {
FileLog.e(e);
} finally {
countDownLatch.countDown();
}
});
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return result[0];
}
Aggregations