use of com.macbitsgoa.ard.models.ChatsItem in project ARD by MobileApplicationsClub.
the class ChatActivity method updateCounts.
/**
* Update unread message count.
* //TODO fix counts maybe?
*/
private void updateCounts() {
// Remove any unread count if present
database.executeTransactionAsync(r -> {
ChatsItem chat = r.where(ChatsItem.class).equalTo(ChatItemKeys.DB_ID, otherUserId).findFirst();
if (chat == null) {
chat = r.createObject(ChatsItem.class, otherUserId);
chat.setLatest("");
chat.setUpdate(Calendar.getInstance().getTime());
chat.setName(getIntent().getStringExtra("title"));
chat.setPhotoUrl(getIntent().getStringExtra("photoUrl"));
}
chat.setUnreadCount(0);
notifyOfReadStatus();
});
}
use of com.macbitsgoa.ard.models.ChatsItem in project ARD by MobileApplicationsClub.
the class SendService method sendMessage.
/**
* Sends a single message using information from the {@link MessageItem} object.
*
* @param mItem message item object to extact information from.
*/
private void sendMessage(final MessageItem mItem) {
final String messageId = mItem.getMessageId();
final String messageData = mItem.getMessageData();
final String receiverId = mItem.getOtherUserId();
final Date messageTime = mItem.getMessageTime();
// First write to local database
database.executeTransaction(r -> {
MessageItem mi = r.where(MessageItem.class).equalTo(MessageItemKeys.MESSAGE_ID, messageId).findFirst();
if (mi == null) {
mi = r.createObject(MessageItem.class, messageId);
mi.setMessageRcvd(false);
mi.setMessageTime(messageTime);
mi.setMessageRcvdTime(Calendar.getInstance().getTime());
mi.setMessageData(messageData);
mi.setOtherUserId(receiverId);
mi.setMessageStatus(MessageStatusType.MSG_WAIT);
}
});
database.executeTransaction(r -> {
final ChatsItem ci = r.where(ChatsItem.class).equalTo(ChatItemKeys.DB_ID, receiverId).findFirst();
if (ci != null) {
ci.setLatest(messageData);
ci.setUpdate(messageTime);
}
});
if (getUser() == null)
return;
final DatabaseReference sendMessageRef = getRootReference().child(AHC.FDR_CHAT).child(mItem.getOtherUserId()).child(ChatItemKeys.PRIVATE_MESSAGES).child(getUser().getUid());
AHC.logd(TAG, "Sending message with id " + mItem.getMessageId());
// Add new message
final Map<String, Object> messageMap = new HashMap<>();
messageMap.put(MessageItemKeys.FDR_DATA, messageData);
messageMap.put(MessageItemKeys.FDR_DATE, messageTime);
// Update latest sender information
final Map<String, Object> senderMap = new HashMap<>();
senderMap.put(ChatItemKeys.FDR_ID, getUser().getUid());
senderMap.put(ChatItemKeys.FDR_NAME, getUser().getDisplayName());
senderMap.put(ChatItemKeys.FDR_LATEST, messageData);
senderMap.put(ChatItemKeys.FDR_PHOTO_URL, getUser().getPhotoUrl().toString());
senderMap.put(ChatItemKeys.FDR_DATE, messageTime);
sendMessageRef.child(ChatItemKeys.FDR_MESSAGES).child(messageId).setValue(messageMap);
sendMessageRef.child(ChatItemKeys.SENDER).setValue(senderMap);
AHC.logd(TAG, "Calling notify service");
notifyStatus(receiverId);
}
Aggregations