Search in sources :

Example 1 with RecyclerViewSwipeListener

use of com.cometchat.pro.uikit.ui_resources.utils.recycler_touch.RecyclerViewSwipeListener 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)

Aggregations

ProgressDialog (android.app.ProgressDialog)1 Bitmap (android.graphics.Bitmap)1 Handler (android.os.Handler)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 AlertDialog (androidx.appcompat.app.AlertDialog)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 CometChat (com.cometchat.pro.core.CometChat)1 CometChatException (com.cometchat.pro.exceptions.CometChatException)1 Conversation (com.cometchat.pro.models.Conversation)1 Group (com.cometchat.pro.models.Group)1 User (com.cometchat.pro.models.User)1 CustomAlertDialogHelper (com.cometchat.pro.uikit.ui_resources.utils.custom_alertDialog.CustomAlertDialogHelper)1 OnAlertDialogButtonClickListener (com.cometchat.pro.uikit.ui_resources.utils.custom_alertDialog.OnAlertDialogButtonClickListener)1 RecyclerViewSwipeListener (com.cometchat.pro.uikit.ui_resources.utils.recycler_touch.RecyclerViewSwipeListener)1 FeatureRestriction (com.cometchat.pro.uikit.ui_settings.FeatureRestriction)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1