use of net.iGap.story.StoryObject 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);
}
});
}
use of net.iGap.story.StoryObject in project iGap-Android by KianIranian-STDG.
the class MessageDataStorage method updateUserAddedStory.
public void updateUserAddedStory(final List<ProtoGlobal.Story> stories) {
storageQueue.postRunnable(() -> {
FileLog.i(TAG, "updateUserAddedStory userId " + stories.get(0).getUserId() + " storiesId " + stories.get(0).getId());
try {
if (stories.get(0).getTypeValue() == 0) {
database.beginTransaction();
RealmRegisteredInfo realmRegisteredInfo = database.where(RealmRegisteredInfo.class).equalTo("id", stories.get(0).getUserId()).findFirst();
if (realmRegisteredInfo != null && realmRegisteredInfo.getDisplayName() != null) {
RealmStory realmStory = database.where(RealmStory.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("userId", stories.get(0).getUserId()).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) {
storyObjects.add(StoryObject.create(stories.get(i), i, realmRegisteredInfo.getDisplayName(), realmRegisteredInfo != null ? realmRegisteredInfo.getColor() : "#4aca69", false, false));
}
}
realmStory.setLastCreatedAt(storyObjects.get(storyObjects.size() - 1).createdAt);
realmStory.setDisplayName(realmRegisteredInfo.getDisplayName());
realmStory.setProfileColor(realmRegisteredInfo.getColor());
realmStory.setSessionId(AccountManager.getInstance().getCurrentUser().getId());
realmStory.setUserId(stories.get(0).getUserId());
realmStory.setOrginatorValue(0);
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 RequestUserInfo().userInfo(stories.get(0).getUserId());
}
}
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of net.iGap.story.StoryObject 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;
}
use of net.iGap.story.StoryObject 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);
}
Aggregations