Search in sources :

Example 6 with Conversation

use of com.cometchat.pro.models.Conversation in project android-java-chat-push-notification-app by cometchat-pro.

the class CometChatConversationsAdapter method onBindViewHolder.

/**
 *  This method is used to bind the ConversationViewHolder contents with conversation at given
 *  position. It set avatar, name, lastMessage, unreadMessageCount and messageTime of conversation
 *  in a respective ConversationViewHolder content. It checks whether conversation type is user
 *  or group and set name and avatar as accordingly. It also checks whether last message is text, media
 *  or file and modify txtUserMessage view accordingly.
 *
 * @param conversationViewHolder is a object of ConversationViewHolder.
 * @param position is a position of item in recyclerView.
 *
 * @see Conversation
 */
@Override
public void onBindViewHolder(@NonNull ConversationViewHolder conversationViewHolder, int position) {
    Conversation conversation = filterConversationList.get(position);
    String avatar;
    String name;
    String status;
    String lastMessageText = null;
    BaseMessage baseMessage = conversation.getLastMessage();
    conversationViewHolder.conversationListRowBinding.setConversation(conversation);
    conversationViewHolder.conversationListRowBinding.executePendingBindings();
    String type = null;
    String category = null;
    if (baseMessage != null) {
        type = baseMessage.getType();
        category = baseMessage.getCategory();
        setStatusIcon(conversationViewHolder.conversationListRowBinding.messageTime, baseMessage);
        conversationViewHolder.conversationListRowBinding.messageTime.setVisibility(View.VISIBLE);
        conversationViewHolder.conversationListRowBinding.messageTime.setText(Utils.getLastMessageDate(context, baseMessage.getSentAt()));
        lastMessageText = Utils.getLastMessage(context, baseMessage);
        if (conversation.getLastMessage().getParentMessageId() != 0) {
            conversationViewHolder.conversationListRowBinding.txtInThread.setVisibility(View.VISIBLE);
        } else {
            conversationViewHolder.conversationListRowBinding.txtInThread.setVisibility(View.GONE);
        }
        if (UIKitSettings.isHideDeleteMessage() && baseMessage.getDeletedAt() > 0) {
            conversationViewHolder.conversationListRowBinding.txtInThread.setVisibility(View.GONE);
            conversationViewHolder.conversationListRowBinding.txtUserMessage.setVisibility(View.GONE);
            lastMessageText = "";
        } else {
            conversationViewHolder.conversationListRowBinding.txtUserMessage.setVisibility(View.VISIBLE);
        }
    } else {
        lastMessageText = context.getResources().getString(R.string.tap_to_start_conversation);
        conversationViewHolder.conversationListRowBinding.txtUserMessage.setMarqueeRepeatLimit(100);
        conversationViewHolder.conversationListRowBinding.txtUserMessage.setHorizontallyScrolling(true);
        conversationViewHolder.conversationListRowBinding.txtUserMessage.setSingleLine(true);
        conversationViewHolder.conversationListRowBinding.messageTime.setVisibility(View.GONE);
    }
    if (lastMessageText.trim().isEmpty())
        conversationViewHolder.conversationListRowBinding.txtInThread.setVisibility(View.GONE);
    conversationViewHolder.conversationListRowBinding.txtUserMessage.setText(lastMessageText);
    if (baseMessage != null) {
        boolean isSentimentNegative = Extensions.checkSentiment(baseMessage);
        if (isSentimentNegative && !baseMessage.getSender().getUid().equals(CometChat.getLoggedInUser().getUid())) {
            conversationViewHolder.conversationListRowBinding.txtUserMessage.setText(context.getResources().getString(R.string.sentiment_content));
        }
    }
    conversationViewHolder.conversationListRowBinding.txtUserMessage.setTypeface(fontUtils.getTypeFace(FontUtils.robotoRegular));
    conversationViewHolder.conversationListRowBinding.txtUserName.setTypeface(fontUtils.getTypeFace(FontUtils.robotoMedium));
    conversationViewHolder.conversationListRowBinding.messageTime.setTypeface(fontUtils.getTypeFace(FontUtils.robotoRegular));
    if (conversation.getConversationType().equals(CometChatConstants.RECEIVER_TYPE_USER)) {
        User conversationUser = ((User) conversation.getConversationWith());
        name = conversationUser.getName();
        avatar = conversationUser.getAvatar();
        status = conversationUser.getStatus();
        if (status.equals(CometChatConstants.USER_STATUS_ONLINE)) {
            conversationViewHolder.conversationListRowBinding.userStatus.setVisibility(View.VISIBLE);
            conversationViewHolder.conversationListRowBinding.userStatus.setUserStatus(status);
        } else
            conversationViewHolder.conversationListRowBinding.userStatus.setVisibility(View.GONE);
    } else {
        name = ((Group) conversation.getConversationWith()).getName();
        avatar = ((Group) conversation.getConversationWith()).getIcon();
        conversationViewHolder.conversationListRowBinding.userStatus.setVisibility(View.GONE);
    }
    conversationViewHolder.conversationListRowBinding.messageCount.setCount(conversation.getUnreadMessageCount());
    conversationViewHolder.conversationListRowBinding.txtUserName.setText(name);
    conversationViewHolder.conversationListRowBinding.messageCount.setCountBackground(Color.parseColor(UIKitSettings.getColor()));
    if (typingIndicator != null) {
        if (typingIndicator.getReceiverType().equalsIgnoreCase(CometChatConstants.RECEIVER_TYPE_USER)) {
            conversationViewHolder.conversationListRowBinding.typingIndicator.setText(context.getString(R.string.is_typing));
        } else {
            conversationViewHolder.conversationListRowBinding.typingIndicator.setText(typingIndicator.getSender().getName() + " " + context.getString(R.string.is_typing));
        }
        if (isTypingVisible) {
            conversationViewHolder.conversationListRowBinding.txtUserMessage.setVisibility(View.VISIBLE);
            conversationViewHolder.conversationListRowBinding.typingIndicator.setVisibility(View.GONE);
        } else {
            conversationViewHolder.conversationListRowBinding.txtUserMessage.setVisibility(View.GONE);
            conversationViewHolder.conversationListRowBinding.typingIndicator.setVisibility(View.VISIBLE);
        }
    }
    if (avatar != null && !avatar.isEmpty()) {
        conversationViewHolder.conversationListRowBinding.avUser.setAvatar(avatar);
    } else {
        conversationViewHolder.conversationListRowBinding.avUser.setInitials(name);
    }
    if (Utils.isDarkMode(context)) {
        conversationViewHolder.conversationListRowBinding.txtUserName.setTextColor(context.getResources().getColor(R.color.textColorWhite));
        conversationViewHolder.conversationListRowBinding.tvSeprator.setBackgroundColor(context.getResources().getColor(R.color.grey));
    } else {
        conversationViewHolder.conversationListRowBinding.txtUserName.setTextColor(context.getResources().getColor(R.color.primaryTextColor));
        conversationViewHolder.conversationListRowBinding.tvSeprator.setBackgroundColor(context.getResources().getColor(R.color.light_grey));
    }
    conversationViewHolder.conversationListRowBinding.getRoot().setTag(R.string.conversation, conversation);
}
Also used : User(com.cometchat.pro.models.User) BaseMessage(com.cometchat.pro.models.BaseMessage) Conversation(com.cometchat.pro.models.Conversation)

Example 7 with Conversation

use of com.cometchat.pro.models.Conversation in project android-java-chat-push-notification-app by cometchat-pro.

the class CometChatConversationList method updateConversation.

/**
 * This method is used to update conversation received in real-time.
 * @param baseMessage is object of BaseMessage.class used to get respective Conversation.
 * @param isRemove is boolean used to check whether conversation needs to be removed or not.
 *
 * @see CometChatHelper#getConversationFromMessage(BaseMessage) This method return the conversation
 * of receiver using baseMessage.
 */
private void updateConversation(BaseMessage baseMessage, boolean isRemove) {
    if (rvConversationList != null) {
        Conversation conversation = CometChatHelper.getConversationFromMessage(baseMessage);
        if (isRemove)
            rvConversationList.remove(conversation);
        else
            rvConversationList.update(conversation);
        checkNoConverstaion();
    }
}
Also used : Conversation(com.cometchat.pro.models.Conversation)

Example 8 with Conversation

use of com.cometchat.pro.models.Conversation in project android-java-chat-push-notification-app by cometchat-pro.

the class CometChatForwardMessageActivity method init.

/**
 * This method is used to initialize the views
 */
public void init() {
    // Inflate the layout
    MaterialToolbar toolbar = findViewById(R.id.forward_toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    if (Utils.changeToolbarFont(toolbar) != null) {
        Utils.changeToolbarFont(toolbar).setTypeface(fontUtils.getTypeFace(FontUtils.robotoMedium));
    }
    selectedUsers = findViewById(R.id.selected_user);
    forwardBtn = findViewById(R.id.btn_forward);
    rvConversationList = findViewById(R.id.rv_conversation_list);
    etSearch = findViewById(R.id.search_bar);
    clearSearch = findViewById(R.id.clear_search);
    etSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (charSequence.length() > 1)
                clearSearch.setVisibility(View.VISIBLE);
        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (editable.toString().length() != 0) {
                if (conversationListAdapter != null)
                    conversationListAdapter.getFilter().filter(editable.toString());
            }
        }
    });
    etSearch.setOnEditorActionListener((textView, i, keyEvent) -> {
        if (i == EditorInfo.IME_ACTION_SEARCH) {
            if (conversationListAdapter != null)
                conversationListAdapter.getFilter().filter(textView.getText().toString());
            clearSearch.setVisibility(View.VISIBLE);
            return true;
        }
        return false;
    });
    clearSearch.setOnClickListener(view1 -> {
        etSearch.setText("");
        clearSearch.setVisibility(View.GONE);
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        // Hide the soft keyboard
        inputMethodManager.hideSoftInputFromWindow(etSearch.getWindowToken(), 0);
    });
    rvConversationList.setItemClickListener(new OnItemClickListener<Conversation>() {

        @Override
        public void OnItemClick(Conversation conversation, int position) {
            if (userList != null && userList.size() < 5) {
                if (!userList.containsKey(conversation.getConversationId())) {
                    userList.put(conversation.getConversationId(), conversation);
                    Chip chip = new Chip(CometChatForwardMessageActivity.this);
                    if (conversation.getConversationType().equals(CometChatConstants.CONVERSATION_TYPE_USER)) {
                        name = ((User) conversation.getConversationWith()).getName();
                        avatar = ((User) conversation.getConversationWith()).getAvatar();
                    } else {
                        name = ((Group) conversation.getConversationWith()).getName();
                        avatar = ((Group) conversation.getConversationWith()).getIcon();
                    }
                    chip.setText(name);
                    Glide.with(CometChatForwardMessageActivity.this).load(avatar).placeholder(R.drawable.ic_contacts).transform(new CircleCrop()).into(new SimpleTarget<Drawable>() {

                        @Override
                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                            chip.setChipIcon(resource);
                        }
                    });
                    chip.setCloseIconVisible(true);
                    chip.setOnCloseIconClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View vw) {
                            userList.remove(conversation.getConversationId());
                            selectedUsers.removeView(vw);
                            checkUserList();
                        }
                    });
                    selectedUsers.addView(chip, 0);
                }
                checkUserList();
            } else {
                CometChatSnackBar.show(CometChatForwardMessageActivity.this, selectedUsers, getString(R.string.forward_to_5_at_a_time), CometChatSnackBar.WARNING);
            }
        }
    });
    // It sends message to selected users present in userList using thread. So UI thread doesn't get heavy.
    forwardBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (messageCategory.equals(CometChatConstants.CATEGORY_MESSAGE)) {
                if (messageType != null && messageType.equals(CometChatConstants.MESSAGE_TYPE_TEXT)) {
                    new Thread(() -> {
                        for (int i = 0; i <= userList.size() - 1; i++) {
                            Conversation conversation = new ArrayList<>(userList.values()).get(i);
                            TextMessage message;
                            String uid;
                            String type;
                            Log.e(TAG, "run: " + conversation.getConversationId());
                            if (conversation.getConversationType().equals(CometChatConstants.CONVERSATION_TYPE_USER)) {
                                uid = ((User) conversation.getConversationWith()).getUid();
                                type = CometChatConstants.RECEIVER_TYPE_USER;
                            } else {
                                uid = ((Group) conversation.getConversationWith()).getGuid();
                                type = CometChatConstants.RECEIVER_TYPE_GROUP;
                            }
                            message = new TextMessage(uid, textMessage, type);
                            sendMessage(message);
                            if (i == userList.size() - 1) {
                                Intent intent = new Intent(CometChatForwardMessageActivity.this, CometChatUI.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                startActivity(intent);
                                finish();
                            }
                        }
                    }).start();
                } else if (messageType != null && (messageType.equals(CometChatConstants.MESSAGE_TYPE_IMAGE) || messageType.equals(CometChatConstants.MESSAGE_TYPE_AUDIO) || messageType.equals(CometChatConstants.MESSAGE_TYPE_FILE) || messageType.equals(CometChatConstants.MESSAGE_TYPE_VIDEO))) {
                    new Thread(() -> {
                        for (int i = 0; i <= userList.size() - 1; i++) {
                            Conversation conversation = new ArrayList<>(userList.values()).get(i);
                            MediaMessage message;
                            String uid;
                            String type;
                            Log.e(TAG, "run: " + conversation.getConversationId());
                            if (conversation.getConversationType().equals(CometChatConstants.CONVERSATION_TYPE_USER)) {
                                uid = ((User) conversation.getConversationWith()).getUid();
                                type = CometChatConstants.RECEIVER_TYPE_USER;
                            } else {
                                uid = ((Group) conversation.getConversationWith()).getGuid();
                                type = CometChatConstants.RECEIVER_TYPE_GROUP;
                            }
                            message = new MediaMessage(uid, null, messageType, type);
                            Attachment attachment = new Attachment();
                            attachment.setFileUrl(mediaMessageUrl);
                            attachment.setFileMimeType(mediaMessageMime);
                            attachment.setFileSize(mediaMessageSize);
                            attachment.setFileExtension(mediaMessageExtension);
                            attachment.setFileName(mediaMessageName);
                            message.setAttachment(attachment);
                            Log.e(TAG, "onClick: " + attachment.toString());
                            sendMediaMessage(message, i);
                        // if (i == userList.size() - 1) {
                        // Intent intent = new Intent(CometChatForwardMessageActivity.this, CometChatUI.class);
                        // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        // startActivity(intent);
                        // finish();
                        // }
                        }
                    }).start();
                } else if (messageType != null && (messageType.equalsIgnoreCase(UIKitConstants.IntentStrings.INTENT_MEDIA_IMAGE_MESSAGE) || messageType.equalsIgnoreCase(UIKitConstants.IntentStrings.INTENT_MEDIA_VIDEO_MESSAGE) || messageType.equalsIgnoreCase(UIKitConstants.IntentStrings.INTENT_MEDIA_AUDIO_MESSAGE) || messageType.equalsIgnoreCase(UIKitConstants.IntentStrings.INTENT_MEDIA_FILE_MESSAGE))) {
                    new Thread(() -> {
                        for (int i = 0; i <= userList.size() - 1; i++) {
                            Conversation conversation = new ArrayList<>(userList.values()).get(i);
                            MediaMessage message = null;
                            String uid;
                            String type;
                            Log.e(TAG, "run: " + conversation.getConversationId());
                            if (conversation.getConversationType().equals(CometChatConstants.CONVERSATION_TYPE_USER)) {
                                uid = ((User) conversation.getConversationWith()).getUid();
                                type = CometChatConstants.RECEIVER_TYPE_USER;
                            } else {
                                uid = ((Group) conversation.getConversationWith()).getGuid();
                                type = CometChatConstants.RECEIVER_TYPE_GROUP;
                            }
                            File file = MediaUtils.getRealPath(CometChatForwardMessageActivity.this, Uri.parse(mediaMessageUrl), true);
                            if (messageType.equalsIgnoreCase(UIKitConstants.IntentStrings.INTENT_MEDIA_IMAGE_MESSAGE))
                                message = new MediaMessage(uid, file, CometChatConstants.MESSAGE_TYPE_IMAGE, type);
                            else if (messageType.equalsIgnoreCase(UIKitConstants.IntentStrings.INTENT_MEDIA_VIDEO_MESSAGE))
                                message = new MediaMessage(uid, file, CometChatConstants.MESSAGE_TYPE_VIDEO, type);
                            else if (messageType.equalsIgnoreCase(UIKitConstants.IntentStrings.INTENT_MEDIA_AUDIO_MESSAGE))
                                message = new MediaMessage(uid, file, CometChatConstants.MESSAGE_TYPE_AUDIO, type);
                            else if (messageType.equalsIgnoreCase(UIKitConstants.IntentStrings.INTENT_MEDIA_FILE_MESSAGE))
                                message = new MediaMessage(uid, file, CometChatConstants.MESSAGE_TYPE_FILE, type);
                            try {
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put("path", mediaMessageUrl);
                                message.setMetadata(jsonObject);
                            } catch (Exception e) {
                                Log.e(TAG, "onError: " + e.getMessage());
                            }
                            sendMediaMessage(message, i);
                        }
                    }).start();
                } else {
                    if (messageType != null && messageType.equalsIgnoreCase(UIKitConstants.IntentStrings.LOCATION)) {
                        new Thread(() -> {
                            for (int i = 0; i <= userList.size() - 1; i++) {
                                Conversation conversation = new ArrayList<>(userList.values()).get(i);
                                CustomMessage message;
                                String uid;
                                JSONObject customData = new JSONObject();
                                String type;
                                Log.e(TAG, "run: " + conversation.getConversationId());
                                if (conversation.getConversationType().equals(CometChatConstants.CONVERSATION_TYPE_USER)) {
                                    uid = ((User) conversation.getConversationWith()).getUid();
                                    type = CometChatConstants.RECEIVER_TYPE_USER;
                                } else {
                                    uid = ((Group) conversation.getConversationWith()).getGuid();
                                    type = CometChatConstants.RECEIVER_TYPE_GROUP;
                                }
                                try {
                                    customData = new JSONObject();
                                    customData.put("latitude", lat);
                                    customData.put("longitude", lon);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                message = new CustomMessage(uid, type, UIKitConstants.IntentStrings.LOCATION, customData);
                                sendLocationMessage(message);
                                if (i == userList.size() - 1) {
                                    Intent intent = new Intent(CometChatForwardMessageActivity.this, CometChatUI.class);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                    startActivity(intent);
                                    finish();
                                }
                            }
                        }).start();
                    }
                }
            }
        }
    });
    rvConversationList.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            if (!recyclerView.canScrollVertically(1)) {
                makeConversationList();
            }
        }
    });
}
Also used : ChipGroup(com.google.android.material.chip.ChipGroup) Group(com.cometchat.pro.models.Group) User(com.cometchat.pro.models.User) MediaMessage(com.cometchat.pro.models.MediaMessage) ArrayList(java.util.ArrayList) InputMethodManager(android.view.inputmethod.InputMethodManager) Conversation(com.cometchat.pro.models.Conversation) Attachment(com.cometchat.pro.models.Attachment) Chip(com.google.android.material.chip.Chip) SimpleTarget(com.bumptech.glide.request.target.SimpleTarget) MaterialToolbar(com.google.android.material.appbar.MaterialToolbar) NonNull(androidx.annotation.NonNull) CustomMessage(com.cometchat.pro.models.CustomMessage) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) Drawable(android.graphics.drawable.Drawable) JSONException(org.json.JSONException) Intent(android.content.Intent) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) CometChatException(com.cometchat.pro.exceptions.CometChatException) JSONException(org.json.JSONException) CircleCrop(com.bumptech.glide.load.resource.bitmap.CircleCrop) JSONObject(org.json.JSONObject) Transition(com.bumptech.glide.request.transition.Transition) RecyclerView(androidx.recyclerview.widget.RecyclerView) File(java.io.File) Nullable(androidx.annotation.Nullable) TextMessage(com.cometchat.pro.models.TextMessage)

Example 9 with Conversation

use of com.cometchat.pro.models.Conversation in project android-java-chat-push-notification-app by cometchat-pro.

the class CometChatConversations method refreshConversation.

/**
 * This method is used to refresh conversation list if any new conversation is initiated or updated.
 * It converts the message recieved from message listener using <code>CometChatHelper.getConversationFromMessage(message)</code>
 *
 * @param message
 * @see CometChatHelper#getConversationFromMessage(BaseMessage)
 * @see Conversation
 */
public void refreshConversation(BaseMessage message) {
    Conversation newConversation = CometChatHelper.getConversationFromMessage(message);
    update(newConversation);
}
Also used : Conversation(com.cometchat.pro.models.Conversation)

Example 10 with Conversation

use of com.cometchat.pro.models.Conversation in project android-java-chat-push-notification-app by cometchat-pro.

the class CometChatConversationsAdapter method setDeliveredReceipts.

public void setDeliveredReceipts(MessageReceipt deliveryReceipts) {
    for (int i = 0; i < filterConversationList.size() - 1; i++) {
        Conversation conversation = filterConversationList.get(i);
        if (conversation.getConversationType().equals(CometChatConstants.RECEIVER_TYPE_USER) && deliveryReceipts.getSender().getUid().equals(((User) conversation.getConversationWith()).getUid())) {
            BaseMessage baseMessage = filterConversationList.get(i).getLastMessage();
            if (baseMessage != null && baseMessage.getDeliveredAt() == 0) {
                baseMessage.setReadAt(deliveryReceipts.getDeliveredAt());
                int index = filterConversationList.indexOf(filterConversationList.get(i));
                filterConversationList.remove(index);
                conversation.setLastMessage(baseMessage);
                filterConversationList.add(index, conversation);
            }
        }
    }
    notifyDataSetChanged();
}
Also used : User(com.cometchat.pro.models.User) BaseMessage(com.cometchat.pro.models.BaseMessage) Conversation(com.cometchat.pro.models.Conversation)

Aggregations

Conversation (com.cometchat.pro.models.Conversation)10 User (com.cometchat.pro.models.User)5 BaseMessage (com.cometchat.pro.models.BaseMessage)3 Group (com.cometchat.pro.models.Group)3 ArrayList (java.util.ArrayList)3 View (android.view.View)2 ViewGroup (android.view.ViewGroup)2 ImageView (android.widget.ImageView)2 RecyclerView (androidx.recyclerview.widget.RecyclerView)2 CometChatException (com.cometchat.pro.exceptions.CometChatException)2 TextMessage (com.cometchat.pro.models.TextMessage)2 JSONObject (org.json.JSONObject)2 ProgressDialog (android.app.ProgressDialog)1 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 Drawable (android.graphics.drawable.Drawable)1 Handler (android.os.Handler)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 InputMethodManager (android.view.inputmethod.InputMethodManager)1