use of net.iGap.realm.RealmStoryProto in project iGap-Android by KianIranian-STDG.
the class MessageDataStorage method deleteExpiredStories.
public void deleteExpiredStories() {
CountDownLatch countDownLatch = new CountDownLatch(1);
storageQueue.postRunnable(() -> {
try {
database.executeTransaction(realm -> {
RealmResults<RealmStoryProto> realmStoryProtos = realm.where(RealmStoryProto.class).lessThan("createdAt", System.currentTimeMillis() - MILLIS_PER_DAY).equalTo("status", MessageObject.STATUS_SENT).findAll();
if (realmStoryProtos != null && realmStoryProtos.size() > 0) {
for (int i = 0; i < realmStoryProtos.size(); i++) {
if (realmStoryProtos.get(i).getFile() != null) {
String filepath = realmStoryProtos.get(i).getFile().getLocalFilePath() != null ? realmStoryProtos.get(i).getFile().getLocalFilePath() : AndroidUtils.getFilePathWithCashId(realmStoryProtos.get(i).getFile().getCacheId(), realmStoryProtos.get(i).getFile().getName(), ProtoGlobal.RoomMessageType.STORY);
if (filepath != null) {
File file = new File(filepath);
if (file.exists())
file.delete();
}
}
}
realmStoryProtos.deleteAllFromRealm();
}
RealmResults<RealmStory> realmStories = realm.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).findAll();
if (realmStories != null && realmStories.size() > 0) {
for (RealmStory realmStory : realmStories) {
if (realmStory != null && realmStory.getRealmStoryProtos().size() == 0) {
realmStory.deleteFromRealm();
}
}
}
});
countDownLatch.countDown();
} catch (Exception e) {
FileLog.e(e);
} finally {
countDownLatch.countDown();
}
});
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
use of net.iGap.realm.RealmStoryProto in project iGap-Android by KianIranian-STDG.
the class MessageDataStorage method getStoryByStatus.
public List<StoryObject> getStoryByStatus(long userId, long roomId, int status, boolean isNotNullToken, boolean isForRoom, String[] fieldSort) {
CountDownLatch countDownLatch = new CountDownLatch(1);
List<RealmStoryProto> stories = new ArrayList<>();
List<StoryObject> storyObjects = new ArrayList<>();
storageQueue.postRunnable(() -> {
try {
if (isNotNullToken) {
if (fieldSort == null) {
if (isForRoom) {
stories.addAll(database.where(RealmStoryProto.class).equalTo("isForRoom", true).equalTo("roomId", roomId).equalTo("userId", userId).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", status).isNotNull("fileToken").findAll());
} else {
stories.addAll(database.where(RealmStoryProto.class).equalTo("userId", userId).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", status).isNotNull("fileToken").findAll());
}
} else {
if (isForRoom) {
stories.addAll(database.where(RealmStoryProto.class).equalTo("isForRoom", true).equalTo("roomId", roomId).equalTo("userId", userId).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", status).isNotNull("fileToken").findAll().sort(fieldSort, new Sort[] { Sort.ASCENDING }));
} else {
stories.addAll(database.where(RealmStoryProto.class).equalTo("userId", userId).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", status).isNotNull("fileToken").findAll().sort(fieldSort, new Sort[] { Sort.ASCENDING }));
}
}
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 });
} else {
if (isForRoom && roomId != 0) {
stories.addAll(database.where(RealmStoryProto.class).equalTo("isForRoom", true).equalTo("roomId", roomId).equalTo("userId", userId).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", status).findAll());
} else {
stories.addAll(database.where(RealmStoryProto.class).equalTo("userId", userId).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", status).findAll());
}
}
for (int i = 0; i < stories.size(); i++) {
storyObjects.add(StoryObject.create(database.copyFromRealm(stories.get(i))));
}
countDownLatch.countDown();
} catch (Exception e) {
FileLog.e(e);
} finally {
countDownLatch.countDown();
}
});
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return storyObjects;
}
use of net.iGap.realm.RealmStoryProto in project iGap-Android by KianIranian-STDG.
the class MessageDataStorage method storySetDisplayName.
public void storySetDisplayName(long roomId, String displayName, boolean isVerified) {
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", roomId).findFirst();
if (realmStory != null) {
realmStory.setDisplayName(displayName);
if (realmStory.getRealmStoryProtos() != null && realmStory.getRealmStoryProtos().size() > 0) {
for (int i = 0; i < realmStory.getRealmStoryProtos().size(); i++) {
if (realmStory.getRealmStoryProtos().get(i) != null) {
realmStory.getRealmStoryProtos().get(i).setDisplayName(displayName);
realmStory.getRealmStoryProtos().get(i).setVerified(isVerified);
}
}
}
G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.STORY_ROOM_INFO, roomId));
} else {
RealmResults<RealmStoryProto> realmStoryProtos = realm.where(RealmStoryProto.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("roomId", roomId).findAll();
if (realmStoryProtos != null && realmStoryProtos.size() > 0) {
for (int i = 0; i < realmStoryProtos.size(); i++) {
realmStoryProtos.get(i).setDisplayName(displayName);
realmStoryProtos.get(i).setVerified(isVerified);
}
G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.STORY_ROOM_INFO, roomId));
}
}
});
countDownLatch.countDown();
} catch (Exception e) {
FileLog.e(e);
} finally {
countDownLatch.countDown();
}
});
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
use of net.iGap.realm.RealmStoryProto in project iGap-Android by KianIranian-STDG.
the class MessageDataStorage method getStoryWithIndexSort.
public List<StoryObject> getStoryWithIndexSort(long userId) {
CountDownLatch countDownLatch = new CountDownLatch(1);
List<StoryObject> storyObjects = new ArrayList<>();
storageQueue.postRunnable(() -> {
try {
database.beginTransaction();
List<RealmStoryProto> realmStories = database.where(RealmStoryProto.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("userId", userId).findAll().sort("index", Sort.DESCENDING);
if (realmStories != null && realmStories.size() > 0) {
for (int i = 0; i < realmStories.size(); i++) {
storyObjects.add(StoryObject.create(realmStories.get(i)));
}
}
database.commitTransaction();
countDownLatch.countDown();
} catch (Exception e) {
FileLog.e(e);
} finally {
countDownLatch.countDown();
}
});
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return storyObjects;
}
use of net.iGap.realm.RealmStoryProto in project iGap-Android by KianIranian-STDG.
the class MessageDataStorage method updateUserAddedStoryWithStoryObjects.
public void updateUserAddedStoryWithStoryObjects(final List<ProtoStoryGetStories.GroupedStories> stories) {
storageQueue.postRunnable(() -> {
try {
database.beginTransaction();
List<StoryObject> storyObjects = new ArrayList<>();
if (stories.size() > 0 && stories.size() >= database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).findAll().size()) {
for (int i = 0; i < stories.size(); i++) {
if (stories.get(i).getStoriesList().size() > 0) {
RealmRegisteredInfo realmRegisteredInfo = database.where(RealmRegisteredInfo.class).equalTo("id", stories.get(i).getOriginatorId()).findFirst();
boolean isForRoom = false;
RealmRoom realmRoom = null;
for (int j = 0; j < stories.get(i).getStoriesList().size(); j++) {
if (stories.get(i).getStoriesList().get(j).getRoomId() != 0) {
realmRoom = database.where(RealmRoom.class).equalTo("id", stories.get(i).getStoriesList().get(j).getRoomId()).findFirst();
isForRoom = true;
if (realmRoom == null) {
new RequestClientGetRoom().clientGetRoom(stories.get(i).getStoriesList().get(j).getRoomId(), RequestClientGetRoom.CreateRoomMode.justInfo);
}
} else {
isForRoom = false;
}
storyObjects.add(StoryObject.create(stories.get(i).getStoriesList().get(j), j, stories.get(i).getSelf() && isForRoom && realmRoom != null ? realmRoom.getTitle() : stories.get(i).getOriginatorName(), !stories.get(i).getSelf() && isForRoom ? realmRoom != null ? realmRoom.getColor() : "#4aca69" : realmRegisteredInfo != null ? realmRegisteredInfo.getColor() : "#4aca69", isForRoom, isForRoom && realmRoom != null && realmRoom.getType() == ProtoGlobal.Room.Type.CHANNEL && realmRoom.getChannelRoom().isVerified()));
}
putStoriesToDatabase(database, stories.get(i).getSeenAllGroupStories(), stories.get(i).getOriginatorId(), storyObjects, stories.get(i).getOriginatorName(), realmRegisteredInfo != null ? realmRegisteredInfo.getColor() : "#4aca69", stories.get(i).getOriginatorValue(), !stories.get(i).getSelf() && stories.get(i).getOriginatorValue() == 1 && (realmRoom != null && realmRoom.getType() == ProtoGlobal.Room.Type.CHANNEL) && realmRoom.getChannelRoom().isVerified());
storyObjects.removeAll(storyObjects);
} else {
RealmStory realmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", stories.get(i).getOriginatorId()).findFirst();
if (realmStory != null) {
realmStory.deleteFromRealm();
}
}
}
} else if (stories.size() != 0 && stories.size() < database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).findAll().size()) {
boolean isExist = false;
List<RealmStory> realmStories = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).findAll();
if (realmStories != null && realmStories.size() > 0) {
for (int i = 0; i < realmStories.size(); i++) {
for (int j = 0; j < stories.size(); j++) {
if (stories.get(j).getOriginatorValue() == 0) {
if (realmStories.get(i).getUserId() == stories.get(j).getOriginatorId()) {
isExist = true;
break;
}
} else {
if (realmStories.get(i).getRoomId() == stories.get(j).getOriginatorId()) {
isExist = true;
break;
}
}
}
if (!isExist) {
long userId = realmStories.get(i).getUserId();
long roomId = realmStories.get(i).getRoomId();
int orginatorValue = realmStories.get(i).getOrginatorValue();
RealmStory realmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo(realmStories.get(i).getOrginatorValue() == 0 ? "userId" : "roomId", realmStories.get(i).getOrginatorValue() == 0 ? userId : roomId).findFirst();
if (realmStory != null && realmStory.isSentAll()) {
realmStory.deleteFromRealm();
}
RealmResults<RealmStoryProto> realmStoryProtos = database.where(RealmStoryProto.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo(orginatorValue == 0 ? "userId" : "roomId", userId).equalTo("status", MessageObject.STATUS_SENT).findAll();
if (realmStoryProtos != null && realmStoryProtos.size() > 0) {
for (int y = 0; y < realmStoryProtos.size(); y++) {
if (realmStoryProtos.get(y).getFile() != null) {
String filepath = realmStoryProtos.get(y).getFile().getLocalFilePath() != null ? realmStoryProtos.get(y).getFile().getLocalFilePath() : AndroidUtils.getFilePathWithCashId(realmStoryProtos.get(y).getFile().getCacheId(), realmStoryProtos.get(y).getFile().getName(), ProtoGlobal.RoomMessageType.STORY);
if (filepath != null) {
File file = new File(filepath);
if (file.exists())
file.delete();
}
}
}
realmStoryProtos.deleteAllFromRealm();
}
}
isExist = false;
}
}
for (int i = 0; i < stories.size(); i++) {
if (stories.get(i).getStoriesList().size() > 0) {
RealmRegisteredInfo realmRegisteredInfo = database.where(RealmRegisteredInfo.class).equalTo("id", stories.get(i).getOriginatorId()).findFirst();
boolean isForRoom = false;
RealmRoom realmRoom = null;
for (int j = 0; j < stories.get(i).getStoriesList().size(); j++) {
if (stories.get(i).getStoriesList().get(j).getRoomId() != 0) {
realmRoom = database.where(RealmRoom.class).equalTo("id", stories.get(i).getStoriesList().get(j).getRoomId()).findFirst();
isForRoom = true;
if (realmRoom == null) {
new RequestClientGetRoom().clientGetRoom(stories.get(i).getStoriesList().get(j).getRoomId(), RequestClientGetRoom.CreateRoomMode.justInfo);
}
} else {
isForRoom = false;
}
storyObjects.add(StoryObject.create(stories.get(i).getStoriesList().get(j), j, stories.get(i).getSelf() && isForRoom && realmRoom != null ? realmRoom.getTitle() : stories.get(i).getOriginatorName(), !stories.get(i).getSelf() && isForRoom ? realmRoom != null ? realmRoom.getColor() : "#4aca69" : realmRegisteredInfo != null ? realmRegisteredInfo.getColor() : "#4aca69", isForRoom, isForRoom && realmRoom != null && realmRoom.getType() == ProtoGlobal.Room.Type.CHANNEL && realmRoom.getChannelRoom().isVerified()));
}
putStoriesToDatabase(database, stories.get(i).getSeenAllGroupStories(), stories.get(i).getOriginatorId(), storyObjects, stories.get(i).getOriginatorName(), realmRegisteredInfo != null ? realmRegisteredInfo.getColor() : "#4aca69", stories.get(i).getOriginatorValue(), !stories.get(i).getSelf() && stories.get(i).getOriginatorValue() == 1 && (realmRoom != null && realmRoom.getType() == ProtoGlobal.Room.Type.CHANNEL) && realmRoom.getChannelRoom().isVerified());
storyObjects.removeAll(storyObjects);
} else {
RealmStory realmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", stories.get(i).getOriginatorId()).findFirst();
if (realmStory != null) {
realmStory.deleteFromRealm();
}
}
}
} else if (stories.size() == 0) {
RealmResults<RealmStoryProto> realmStoryProtos = database.where(RealmStoryProto.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("status", MessageObject.STATUS_SENT).findAll();
if (realmStoryProtos != null && realmStoryProtos.size() > 0) {
for (int y = 0; y < realmStoryProtos.size(); y++) {
if (realmStoryProtos.get(y).getFile() != null) {
String filepath = realmStoryProtos.get(y).getFile().getLocalFilePath() != null ? realmStoryProtos.get(y).getFile().getLocalFilePath() : AndroidUtils.getFilePathWithCashId(realmStoryProtos.get(y).getFile().getCacheId(), realmStoryProtos.get(y).getFile().getName(), ProtoGlobal.RoomMessageType.STORY);
if (filepath != null) {
File file = new File(filepath);
if (file.exists())
file.delete();
}
}
}
realmStoryProtos.deleteAllFromRealm();
}
List<RealmStory> realmStories = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).findAll();
if (realmStories != null && realmStories.size() > 0) {
for (int i = 0; i < realmStories.size(); i++) {
if (realmStories.get(i).getRealmStoryProtos().size() == 0) {
database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", realmStories.get(i).getUserId()).findAll().deleteAllFromRealm();
}
}
}
}
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();
UserLoginResponse.isFetched = true;
G.runOnUiThread(() -> {
if (G.onUnreadChange != null) {
G.onUnreadChange.onChange(storyUnReadCount[0], true);
}
StoryFragment.storyListFetched = false;
getEventManager().postEvent(EventManager.STORY_LIST_FETCHED);
});
} catch (Exception e) {
Log.e("Fskhfjksdhjkshdf", "updateUserAddedStoryWithStoryObjects: " + "/" + e.getMessage());
FileLog.e(e);
}
});
}
Aggregations