Search in sources :

Example 1 with Conversation

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

the class CometChatConversationList method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_cometchat_conversationlist, container, false);
    rvConversationList = view.findViewById(R.id.rv_conversation_list);
    noConversationView = view.findViewById(R.id.no_conversation_view);
    searchEdit = view.findViewById(R.id.search_bar);
    tvTitle = view.findViewById(R.id.tv_title);
    tvTitle.setTypeface(FontUtils.getInstance(getActivity()).getTypeFace(FontUtils.robotoMedium));
    rlSearchBox = view.findViewById(R.id.rl_search_box);
    conversationShimmer = view.findViewById(R.id.shimmer_layout);
    checkDarkMode();
    CometChatError.init(getContext());
    startConversation = view.findViewById(R.id.start_conversation);
    FeatureRestriction.isStartConversationEnabled(new FeatureRestriction.OnSuccessListener() {

        @Override
        public void onSuccess(Boolean booleanVal) {
            if (booleanVal)
                startConversation.setVisibility(View.VISIBLE);
            else
                startConversation.setVisibility(View.GONE);
        }
    });
    startConversation.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            CometChatStartConversation.launch(getContext());
        }
    });
    searchEdit.setOnEditorActionListener((textView, i, keyEvent) -> {
        if (i == EditorInfo.IME_ACTION_SEARCH) {
            if (!textView.getText().toString().isEmpty()) {
                progressDialog = ProgressDialog.show(getContext(), "", getString(R.string.search));
                refreshConversation(new CometChat.CallbackListener<List<Conversation>>() {

                    @Override
                    public void onSuccess(List<Conversation> conversationList) {
                        if (progressDialog != null)
                            progressDialog.dismiss();
                        rvConversationList.searchConversation(textView.getText().toString());
                    }

                    @Override
                    public void onError(CometChatException e) {
                        if (progressDialog != null)
                            progressDialog.dismiss();
                        CometChatSnackBar.show(getContext(), rvConversationList, CometChatError.localized(e), CometChatSnackBar.ERROR);
                    }
                });
            }
            return true;
        }
        return false;
    });
    // clearSearch.setOnClickListener(new View.OnClickListener() {
    // @Override
    // public void onClick(View view) {
    // searchEdit.setText("");
    // clearSearch.setVisibility(View.GONE);
    // refreshConversation();
    // InputMethodManager inputMethodManager = (InputMethodManager)
    // getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    // // Hide the soft keyboard
    // inputMethodManager.hideSoftInputFromWindow(searchEdit.getWindowToken(),0);
    // }
    // });
    // Uses to fetch next list of conversations if rvConversationList (RecyclerView) is scrolled in upward direction.
    rvConversationList.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            if (!recyclerView.canScrollVertically(1)) {
                makeConversationList();
            }
        }
    });
    // Used to trigger event on click of conversation item in rvConversationList (RecyclerView)
    rvConversationList.setItemClickListener(new OnItemClickListener<Conversation>() {

        @Override
        public void OnItemClick(Conversation conversation, int position) {
            if (events != null)
                events.OnItemClick(conversation, position);
        }
    });
    RecyclerViewSwipeListener swipeHelper = new RecyclerViewSwipeListener(getContext()) {

        @Override
        public void instantiateUnderlayButton(RecyclerView.ViewHolder viewHolder, List<UnderlayButton> underlayButtons) {
            Bitmap deleteBitmap = Utils.drawableToBitmap(getResources().getDrawable(R.drawable.ic_delete_conversation));
            FeatureRestriction.isDeleteConversationEnabled(new FeatureRestriction.OnSuccessListener() {

                @Override
                public void onSuccess(Boolean booleanVal) {
                    if (booleanVal) {
                        underlayButtons.add(new RecyclerViewSwipeListener.UnderlayButton("Delete", deleteBitmap, getResources().getColor(R.color.red), new RecyclerViewSwipeListener.UnderlayButtonClickListener() {

                            @Override
                            public void onClick(final int pos) {
                                Conversation conversation = rvConversationList.getConversation(pos);
                                if (conversation != null) {
                                    String conversationUid = "";
                                    String type = "";
                                    if (conversation.getConversationType().equalsIgnoreCase(CometChatConstants.CONVERSATION_TYPE_GROUP)) {
                                        conversationUid = ((Group) conversation.getConversationWith()).getGuid();
                                        type = CometChatConstants.CONVERSATION_TYPE_GROUP;
                                    } else {
                                        conversationUid = ((User) conversation.getConversationWith()).getUid();
                                        type = CometChatConstants.CONVERSATION_TYPE_USER;
                                    }
                                    String finalConversationUid = conversationUid;
                                    String finalType = type;
                                    new CustomAlertDialogHelper(getContext(), getString(R.string.delete_conversation_message), null, getString(R.string.yes), "", getString(R.string.no), new OnAlertDialogButtonClickListener() {

                                        @Override
                                        public void onButtonClick(AlertDialog alertDialog, View v, int which, int popupId) {
                                            if (which == DialogInterface.BUTTON_POSITIVE) {
                                                ProgressDialog progressDialog = ProgressDialog.show(getContext(), null, getString(R.string.deleting_conversation));
                                                CometChat.deleteConversation(finalConversationUid, finalType, new CometChat.CallbackListener<String>() {

                                                    @Override
                                                    public void onSuccess(String s) {
                                                        Handler handler = new Handler();
                                                        handler.postDelayed(new Runnable() {

                                                            public void run() {
                                                                alertDialog.dismiss();
                                                                progressDialog.dismiss();
                                                            }
                                                        }, 1500);
                                                        rvConversationList.remove(conversation);
                                                    }

                                                    @Override
                                                    public void onError(CometChatException e) {
                                                        progressDialog.dismiss();
                                                        e.printStackTrace();
                                                    }
                                                });
                                            } else if (which == DialogInterface.BUTTON_NEGATIVE) {
                                                alertDialog.dismiss();
                                            }
                                        }
                                    }, 1, true);
                                }
                            }
                        }));
                    }
                }
            });
        }
    };
    swipeHelper.attachToRecyclerView(rvConversationList);
    return view;
}
Also used : CometChatException(com.cometchat.pro.exceptions.CometChatException) AlertDialog(androidx.appcompat.app.AlertDialog) ViewGroup(android.view.ViewGroup) Group(com.cometchat.pro.models.Group) User(com.cometchat.pro.models.User) Conversation(com.cometchat.pro.models.Conversation) ProgressDialog(android.app.ProgressDialog) Bitmap(android.graphics.Bitmap) FeatureRestriction(com.cometchat.pro.uikit.ui_settings.FeatureRestriction) CustomAlertDialogHelper(com.cometchat.pro.uikit.ui_resources.utils.custom_alertDialog.CustomAlertDialogHelper) List(java.util.List) ArrayList(java.util.ArrayList) OnAlertDialogButtonClickListener(com.cometchat.pro.uikit.ui_resources.utils.custom_alertDialog.OnAlertDialogButtonClickListener) CometChat(com.cometchat.pro.core.CometChat) Handler(android.os.Handler) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) RecyclerViewSwipeListener(com.cometchat.pro.uikit.ui_resources.utils.recycler_touch.RecyclerViewSwipeListener) RecyclerView(androidx.recyclerview.widget.RecyclerView)

Example 2 with Conversation

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

the class CometChatConversationsAdapter method getFilter.

/**
 * It is used to perform search operation in filterConversationList. It will check
 * whether searchKeyword is similar to username or group name and modify filterConversationList
 * accordingly. In case if searchKeyword is empty it will set filterConversationList = conversationList
 *
 * @return
 */
@Override
public Filter getFilter() {
    return new Filter() {

        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            String searchKeyword = charSequence.toString();
            if (searchKeyword.isEmpty()) {
                filterConversationList = conversationList;
            } else {
                List<Conversation> tempFilter = new ArrayList<>();
                for (Conversation conversation : filterConversationList) {
                    if (conversation.getConversationType().equals(CometChatConstants.CONVERSATION_TYPE_USER) && ((User) conversation.getConversationWith()).getName().toLowerCase().contains(searchKeyword)) {
                        tempFilter.add(conversation);
                    } else if (conversation.getConversationType().equals(CometChatConstants.CONVERSATION_TYPE_GROUP) && ((Group) conversation.getConversationWith()).getName().toLowerCase().contains(searchKeyword)) {
                        tempFilter.add(conversation);
                    } else if (conversation.getLastMessage() != null && conversation.getLastMessage().getCategory().equals(CometChatConstants.CATEGORY_MESSAGE) && conversation.getLastMessage().getType().equals(CometChatConstants.MESSAGE_TYPE_TEXT) && ((TextMessage) conversation.getLastMessage()).getText() != null && ((TextMessage) conversation.getLastMessage()).getText().contains(searchKeyword)) {
                        tempFilter.add(conversation);
                    }
                }
                filterConversationList = tempFilter;
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = filterConversationList;
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            filterConversationList = (List<Conversation>) filterResults.values;
            notifyDataSetChanged();
        }
    };
}
Also used : Group(com.cometchat.pro.models.Group) ViewGroup(android.view.ViewGroup) Filter(android.widget.Filter) ArrayList(java.util.ArrayList) Conversation(com.cometchat.pro.models.Conversation) TextMessage(com.cometchat.pro.models.TextMessage)

Example 3 with Conversation

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

the class CometChatConversationsAdapter method update.

/**
 * This method is used to update conversation in filterConversationList.
 *
 * @param conversation is an object of Conversation. It is used to update the previous conversation
 *                     in list
 * @see Conversation
 */
public void update(Conversation conversation) {
    if (filterConversationList.contains(conversation)) {
        Conversation oldConversation = filterConversationList.get(filterConversationList.indexOf(conversation));
        filterConversationList.remove(oldConversation);
        JSONObject metadata = conversation.getLastMessage().getMetadata();
        boolean incrementUnreadCount = false;
        boolean isCategoryMessage = conversation.getLastMessage().getCategory().equalsIgnoreCase(CometChatConstants.CATEGORY_MESSAGE);
        try {
            if (metadata.has("incrementUnreadCount"))
                incrementUnreadCount = metadata.getBoolean("incrementUnreadCount");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (incrementUnreadCount || isCategoryMessage)
            conversation.setUnreadMessageCount(oldConversation.getUnreadMessageCount() + 1);
        filterConversationList.add(0, conversation);
    } else {
        filterConversationList.add(0, conversation);
    }
    notifyDataSetChanged();
}
Also used : JSONObject(org.json.JSONObject) Conversation(com.cometchat.pro.models.Conversation)

Example 4 with Conversation

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

the class CometChatConversationsAdapter method setTypingIndicator.

public void setTypingIndicator(TypingIndicator typingIndicator, boolean b) {
    for (Conversation conversation : filterConversationList) {
        if (typingIndicator.getReceiverType().equalsIgnoreCase(CometChatConstants.RECEIVER_TYPE_USER)) {
            if (conversation.getConversationId().contains(typingIndicator.getSender().getUid())) {
                this.typingIndicator = typingIndicator;
                isTypingVisible = b;
                int index = filterConversationList.indexOf(conversation);
                notifyItemChanged(index);
            }
        } else {
            if (conversation.getConversationId().contains(typingIndicator.getReceiverId())) {
                this.typingIndicator = typingIndicator;
                isTypingVisible = b;
                int index = filterConversationList.indexOf(conversation);
                notifyItemChanged(index);
            }
        }
    }
}
Also used : Conversation(com.cometchat.pro.models.Conversation)

Example 5 with Conversation

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

the class CometChatConversationsAdapter method setReadReceipts.

public void setReadReceipts(MessageReceipt readReceipts) {
    for (int i = 0; i < filterConversationList.size() - 1; i++) {
        Conversation conversation = filterConversationList.get(i);
        if (conversation.getConversationType().equals(CometChatConstants.RECEIVER_TYPE_USER) && readReceipts.getSender().getUid().equals(((User) conversation.getConversationWith()).getUid())) {
            BaseMessage baseMessage = filterConversationList.get(i).getLastMessage();
            if (baseMessage != null && baseMessage.getReadAt() == 0) {
                baseMessage.setReadAt(readReceipts.getReadAt());
                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