use of net.iGap.realm.RealmRoom in project iGap-Android by KianIranian-STDG.
the class HelperUrl method isInCurrentChat.
private static boolean isInCurrentChat(String userName) {
if (FragmentChat.lastChatRoomId > 0) {
return DbManager.getInstance().doRealmTask(realm -> {
RealmRoom realmRoom = realm.where(RealmRoom.class).equalTo("id", FragmentChat.lastChatRoomId).findFirst();
String currentChatUserName = "";
String currentChatInviteLink = "";
if (realmRoom != null) {
if (realmRoom.getChannelRoom() != null) {
currentChatUserName = realmRoom.getChannelRoom().getUsername();
currentChatInviteLink = realmRoom.getChannelRoom().getInviteLink();
} else if (realmRoom.getGroupRoom() != null) {
currentChatUserName = realmRoom.getGroupRoom().getUsername();
currentChatInviteLink = realmRoom.getGroupRoom().getInvite_link();
} else if (realmRoom.getChatRoom() != null) {
RealmRegisteredInfo registeredInfo = RealmRegisteredInfo.getRegistrationInfo(realm, realmRoom.getChatRoom().getPeerId());
if (registeredInfo != null) {
currentChatUserName = registeredInfo.getUsername();
}
}
}
if (currentChatUserName != null && currentChatUserName.toLowerCase().equals(userName.toLowerCase())) {
return true;
}
if (currentChatInviteLink != null && currentChatInviteLink.length() > 0) {
int index = currentChatInviteLink.lastIndexOf("/");
if (index != -1) {
String st = currentChatInviteLink.toLowerCase().substring(index + 1);
return st.equals(userName.toLowerCase());
}
}
return false;
});
}
return false;
}
use of net.iGap.realm.RealmRoom in project iGap-Android by KianIranian-STDG.
the class HelperUrl method goToActivityFromFCM.
public static void goToActivityFromFCM(FragmentActivity activity, final long roomId, final long peerId) {
if (roomId != FragmentChat.lastChatRoomId) {
DbManager.getInstance().doRealmTask(realm -> {
RealmRoom realmRoom = realm.where(RealmRoom.class).equalTo("id", roomId).findFirst();
if (realmRoom != null) {
// room with given roomID exists.
new GoToChatActivity(realmRoom.getId()).startActivity(activity);
} else if (peerId > 0) {
realmRoom = realm.where(RealmRoom.class).equalTo("chatRoom.peer_id", peerId).findFirst();
if (realmRoom != null) {
new GoToChatActivity(realmRoom.getId()).startActivity(activity);
} else {
G.onChatGetRoom = new OnChatGetRoom() {
@Override
public void onChatGetRoom(final ProtoGlobal.Room room) {
DbManager.getInstance().doRealmTask(new DbManager.RealmTask() {
@Override
public void doTask(Realm realm1) {
realm1.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
if (realm.where(RealmRoom.class).equalTo("id", room.getId()).findFirst() == null) {
RealmRoom realmRoom1 = RealmRoom.putOrUpdate(room, realm);
realmRoom1.setDeleted(true);
} else {
RealmRoom.putOrUpdate(room, realm);
}
}
});
}
});
G.handler.postDelayed(new Runnable() {
@Override
public void run() {
new GoToChatActivity(room.getId()).setPeerID(peerId).startActivity(activity);
G.onChatGetRoom = null;
}
}, 500);
}
@Override
public void onChatGetRoomTimeOut() {
}
@Override
public void onChatGetRoomError(int majorCode, int minorCode) {
}
};
new RequestChatGetRoom().chatGetRoom(peerId);
}
} else {
G.onClientGetRoomResponse = new OnClientGetRoomResponse() {
@Override
public void onClientGetRoomResponse(ProtoGlobal.Room room, ProtoClientGetRoom.ClientGetRoomResponse.Builder builder, RequestClientGetRoom.IdentityClientGetRoom identity) {
G.onClientGetRoomResponse = null;
G.handler.postDelayed(() -> {
new GoToChatActivity(room.getId()).setPeerID(peerId).startActivity(activity);
G.onChatGetRoom = null;
}, 500);
}
@Override
public void onError(int majorCode, int minorCode) {
}
@Override
public void onTimeOut() {
}
};
new RequestClientGetRoom().clientGetRoom(roomId, null);
Toast.makeText(activity, "Please Wait...", Toast.LENGTH_SHORT).show();
}
});
}
}
use of net.iGap.realm.RealmRoom in project iGap-Android by KianIranian-STDG.
the class HelperUrl method goToRoom.
private static void goToRoom(FragmentActivity activity, String username, final ProtoGlobal.Room room, long messageId) {
DbManager.getInstance().doRealmTask(realm -> {
RealmRoom realmRoom = realm.where(RealmRoom.class).equalTo("id", room.getId()).findFirst();
if (realmRoom != null) {
if (realmRoom.isDeleted()) {
addRoomToDataBaseAndGoToRoom(activity, username, room, messageId);
} else {
closeDialogWaiting();
if (room.getId() != FragmentChat.lastChatRoomId) {
new GoToChatActivity(room.getId()).setMessageID(messageId).startActivity(activity);
} else {
try {
if (activity != null) {
activity.getSupportFragmentManager().popBackStack();
new GoToChatActivity(room.getId()).setMessageID(messageId).startActivity(activity);
}
} catch (Exception e) {
HelperLog.getInstance().setErrorLog(e);
e.printStackTrace();
}
}
}
} else {
addRoomToDataBaseAndGoToRoom(activity, username, room, messageId);
}
});
}
use of net.iGap.realm.RealmRoom in project iGap-Android by KianIranian-STDG.
the class HelperLogMessage method gotToUserRoom.
private static void gotToUserRoom(final long id) {
DbManager.getInstance().doRealmTask(realm -> {
RealmRoom realmRoom = realm.where(RealmRoom.class).equalTo("chatRoom.peer_id", id).findFirst();
if (realmRoom != null) {
// Intent intent = new Intent(G.currentActivity, ActivityChat.class);
// intent.putExtra("RoomId", realmRoom.getId());
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// G.currentActivity.startActivity(intent);
// Intent intent = new Intent(G.context, ActivityContactsProfile.class);
// intent.putExtra("peerId", id);
// intent.putExtra("RoomId", realmRoom.getId());
// intent.putExtra("enterFrom", "GROUP");
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// G.currentActivity.startActivity(intent);
FragmentContactsProfile contactsProfile = new FragmentContactsProfile();
Bundle bundle = new Bundle();
bundle.putLong("peerId", id);
bundle.putLong("RoomId", realmRoom.getId());
bundle.putString("enterFrom", "GROUP");
contactsProfile.setArguments(bundle);
// ToDo:fixed it and change to do not use G.currentActivity
new HelperFragment(G.currentActivity.getSupportFragmentManager(), contactsProfile).setReplace(false).load();
} else {
G.onChatGetRoom = new OnChatGetRoom() {
@Override
public void onChatGetRoom(final ProtoGlobal.Room room) {
G.handler.post(new Runnable() {
@Override
public void run() {
FragmentContactsProfile contactsProfile = new FragmentContactsProfile();
Bundle bundle = new Bundle();
bundle.putLong("peerId", id);
bundle.putLong("RoomId", room.getId());
bundle.putString("enterFrom", "GROUP");
contactsProfile.setArguments(bundle);
// ToDo:fixed it and change to do not use G.currentActivity
new HelperFragment(G.currentActivity.getSupportFragmentManager(), contactsProfile).setReplace(false).load();
G.onChatGetRoom = null;
}
});
}
@Override
public void onChatGetRoomTimeOut() {
}
@Override
public void onChatGetRoomError(int majorCode, int minorCode) {
}
};
new RequestChatGetRoom().chatGetRoom(id);
}
});
}
use of net.iGap.realm.RealmRoom in project iGap-Android by KianIranian-STDG.
the class HelperMessageResponse method handleMessage.
public static void handleMessage(final long roomId, final ProtoGlobal.RoomMessage roomMessage, final ProtoGlobal.Room.Type roomType, final ProtoResponse.Response response, final String identity) {
DbManager.getInstance().doRealmTransaction(realm -> {
/**
* put message to realm
*/
FileLog.i("HelperMessageResponse roomType -> " + roomType.toString() + " messageId -> " + roomMessage.getMessageId() + " roomId -> " + roomId);
RealmRoomMessage realmRoomMessage = RealmRoomMessage.putOrUpdate(realm, roomId, roomMessage, new StructMessageOption().setGap());
final RealmRoom room = realm.where(RealmRoom.class).equalTo("id", roomId).findFirst();
/**
* because user may have more than one device, his another device should not
* be recipient but sender. so I check current userId with room message user id,
* and if not equals and response is null, so we sure recipient is another user
*/
if (!roomMessage.getAuthor().getHash().equals(RealmUserInfo.getCurrentUserAuthorHash())) {
/**
* i'm recipient
*
* if author has user check that client have latest info for this user or no
* if author don't have use this means that message is from channel so client
* don't have user id for message sender for get info
*/
if (roomMessage.getAuthor().hasUser()) {
RealmRegisteredInfo.needUpdateUser(roomMessage.getAuthor().getUser().getUserId(), roomMessage.getAuthor().getUser().getCacheId());
}
// } else if (!response.getId().isEmpty()) {
// /**
// * i'm the sender
// *
// * delete message that created with fake messageId as identity
// * because in new version of realm client can't update primary key
// */
// RealmRoomMessage.deleteMessage(realm, Long.parseLong(identity));
}
if (identity != null && identity.length() > 0) {
RealmRoomMessage.deleteMessage(realm, Long.parseLong(identity), roomId);
}
if (room == null) {
/**
* if first message received but the room doesn't exist, send request for create new room
*/
new RequestClientGetRoom().clientGetRoom(roomId, null);
} else {
if (room.getType() == ProtoGlobal.Room.Type.CHAT)
room.setDeleted(false);
if (!roomMessage.getAuthor().getHash().equals(RealmUserInfo.getCurrentUserAuthorHash()) && (room.getLastMessage() == null || (room.getLastMessage() != null && room.getLastMessage().getMessageId() < roomMessage.getMessageId()))) {
room.setUnreadCount(room.getUnreadCount() + 1);
}
if (!roomMessage.getAuthor().getHash().equals(RealmUserInfo.getCurrentUserAuthorHash())) {
if (room.getFirstUnreadMessage() == null) {
room.setFirstUnreadMessage(realmRoomMessage);
}
if (roomMessage.getStatus() != ProtoGlobal.RoomMessageStatus.SEEN && RealmNotificationRoomMessage.canShowNotif(realm, roomMessage.getMessageId(), roomId)) {
RealmNotificationRoomMessage.putToDataBase(realm, roomMessage.getMessageId(), roomId);
HelperNotification.getInstance().addMessage(roomId, roomMessage, roomType, room, realm, AccountManager.getInstance().getCurrentUser());
}
}
/**
* update last message sent/received in room table
*/
if (room.getLastMessage() != null) {
if (room.getLastMessage().getMessageId() <= roomMessage.getMessageId()) {
room.setLastMessage(realmRoomMessage);
}
} else {
room.setLastMessage(realmRoomMessage);
}
}
});
if (response.getId().isEmpty()) {
/**
* invoke following callback when i'm not the sender, because i already done everything after sending message
*/
ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).onMessageReceive(roomId, roomMessage.getMessage(), roomMessage.getMessageType(), roomMessage, roomType);
} else {
/**
* invoke following callback when I'm the sender and the message has updated
*/
ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).onMessageUpdate(roomId, roomMessage.getMessageId(), roomMessage.getStatus(), identity, roomMessage);
}
if ((roomMessage.getAuthor().getUser().getUserId() == AccountManager.getInstance().getCurrentUser().getId() || roomMessage.getAuthor().getUser().getUserId() == 0) && roomMessage.getAttachment() != null) {
G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.ON_UPLOAD_COMPLETED, roomMessage.getMessageType(), roomMessage.getMessageId(), roomMessage.getAttachment().getCacheId(), roomMessage.getAttachment().getToken()));
}
}
Aggregations