Search in sources :

Example 6 with ChatsItem

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();
    });
}
Also used : ChatsItem(com.macbitsgoa.ard.models.ChatsItem)

Example 7 with ChatsItem

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);
}
Also used : MessageItem(com.macbitsgoa.ard.models.MessageItem) DatabaseReference(com.google.firebase.database.DatabaseReference) HashMap(java.util.HashMap) ChatsItem(com.macbitsgoa.ard.models.ChatsItem) Date(java.util.Date)

Aggregations

ChatsItem (com.macbitsgoa.ard.models.ChatsItem)7 MessageItem (com.macbitsgoa.ard.models.MessageItem)6 DatabaseReference (com.google.firebase.database.DatabaseReference)3 Realm (io.realm.Realm)3 RealmList (io.realm.RealmList)3 Intent (android.content.Intent)2 DocumentItem (com.macbitsgoa.ard.models.DocumentItem)2 Date (java.util.Date)2 PendingIntent (android.app.PendingIntent)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 DataSnapshot (com.google.firebase.database.DataSnapshot)1 DatabaseError (com.google.firebase.database.DatabaseError)1 ValueEventListener (com.google.firebase.database.ValueEventListener)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1