use of com.macbitsgoa.ard.models.MessageItem in project ARD by MobileApplicationsClub.
the class ChatMsgAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
MessageItem mi = messages.get(position);
if (mi.hasAttachments()) {
DocumentItem di = mi.getDocuments().get(0);
if (!di.getLocalUri().equals("") && di.getMimeType().contains("image")) {
ImageViewHolder imvh = (ImageViewHolder) holder;
String imageUrl = di.getLocalThumbnailUri();
if (imageUrl == null)
imageUrl = di.getLocalUri();
if (imageUrl == null)
imageUrl = di.getRemoteThumbnailUrl();
if (imageUrl == null)
imageUrl = di.getRemoteUrl();
imvh.setUri(Uri.parse(di.getLocalUri()), Uri.parse(imageUrl), RequestOptions.centerCropTransform());
} else {
((ImageViewHolder) holder).setUri(di.getLocalUri(), AHC.getImageUrlFromMimeType(di.getMimeType()));
}
} else if (holder.getItemViewType() == RECEIVER || holder.getItemViewType() == SENDER) {
ChatMsgViewHolder cmvh = (ChatMsgViewHolder) holder;
cmvh.populate(mi);
if (position < getItemCount() - 1 && getItemViewType(position + 1) == holder.getItemViewType()) {
cmvh.emptySpace.setVisibility(View.GONE);
} else
cmvh.emptySpace.setVisibility(View.VISIBLE);
}
}
use of com.macbitsgoa.ard.models.MessageItem in project ARD by MobileApplicationsClub.
the class SendDocumentService method sendAll.
/**
* If {@link #onHandleIntent(Intent)} has {@code null} intent, then automatically check for
* any unsent messages.
*/
private void sendAll() {
final RealmList<MessageItem> notSentMessages = new RealmList<>();
Realm database = Realm.getDefaultInstance();
notSentMessages.addAll(database.where(MessageItem.class).equalTo(MessageItemKeys.MESSAGE_STATUS, MessageStatusType.MSG_WAIT).isNotEmpty(MessageItemKeys.DB_DOCUMENTS).findAllSorted(MessageItemKeys.DB_MESSAGE_TIME, Sort.ASCENDING));
for (int i = 0; i < notSentMessages.size(); i++) {
sendDocument(notSentMessages.get(i));
}
database.close();
}
use of com.macbitsgoa.ard.models.MessageItem 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);
}
use of com.macbitsgoa.ard.models.MessageItem in project ARD by MobileApplicationsClub.
the class SendService method sendMessage.
/**
* Method to create a temporary {@link MessageItem} object. This method is called for all
* messages that haven't yet been added to database.
*
* @param messageData Message Data to send.
* @param otherUserId Receiving user's unique user id.
*/
private void sendMessage(final String messageData, final String otherUserId) {
// Get current system time and include this is in message id
final String messageId = AHC.generateUniqueId(messageData);
// Init an empty message item, with defaults loaded.
final MessageItem mi = new MessageItem();
mi.setMessageId(messageId);
// As we are sending message, this is false.
mi.setMessageRcvd(false);
mi.setMessageData(messageData);
mi.setOtherUserId(otherUserId);
sendMessage(mi);
}
Aggregations