|
| 1 | +package adapter; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.util.Log; |
| 5 | +import android.view.LayoutInflater; |
| 6 | +import android.view.View; |
| 7 | +import android.view.ViewGroup; |
| 8 | +import android.widget.TextView; |
| 9 | + |
| 10 | +import androidx.annotation.NonNull; |
| 11 | +import androidx.recyclerview.widget.RecyclerView; |
| 12 | +import com.cometchat.pro.uikit.R; |
| 13 | +import com.cometchat.pro.uikit.Reaction.model.Reaction; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +/** |
| 19 | + * Purpose - ReactionListAdapter is a subclass of RecyclerView Adapter which is used to display |
| 20 | + * the list of users. It helps to organize the users in recyclerView. |
| 21 | + * |
| 22 | + * Created on - 20th November 2020 |
| 23 | + * |
| 24 | + * Modified on - 20th November 2020 |
| 25 | + * |
| 26 | + */ |
| 27 | + |
| 28 | +public class ReactionListAdapter extends RecyclerView.Adapter<ReactionListAdapter.ReactionViewHolder> { |
| 29 | + |
| 30 | + private Context context; |
| 31 | + |
| 32 | + private List<Reaction> reactionArrayList = new ArrayList<>(); |
| 33 | + |
| 34 | + private static final String TAG = "reactionListAdapter"; |
| 35 | + |
| 36 | + /** |
| 37 | + * It is a contructor which is used to initialize wherever we needed. |
| 38 | + * |
| 39 | + * @param context is a object of Context. |
| 40 | + */ |
| 41 | + public ReactionListAdapter(Context context) { |
| 42 | + this.context=context; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * It is constructor which takes reactionArrayList as parameter and bind it with reactionArrayList in adapter. |
| 47 | + * |
| 48 | + * @param context is a object of Context. |
| 49 | + * @param reactionArrayList is a list of Reactions used in this adapter. |
| 50 | + */ |
| 51 | + public ReactionListAdapter(Context context, List<Reaction> reactionArrayList) { |
| 52 | + this.reactionArrayList = reactionArrayList; |
| 53 | + this.context= context; |
| 54 | + } |
| 55 | + |
| 56 | + @NonNull |
| 57 | + @Override |
| 58 | + public ReactionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) { |
| 59 | + |
| 60 | + LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); |
| 61 | + |
| 62 | + View view = layoutInflater.inflate(R.layout.reaction_list_row, parent, false); |
| 63 | + |
| 64 | + return new ReactionViewHolder(view); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * This method is used to bind the ReactionViewHolder contents with user at given |
| 69 | + * position. It set username userAvatar in respective ReactionViewHolder content. |
| 70 | + * |
| 71 | + * @param reactionViewHolder is a object of ReactionViewHolder. |
| 72 | + * @param i is a position of item in recyclerView. |
| 73 | + * @see Reaction |
| 74 | + */ |
| 75 | + @Override |
| 76 | + public void onBindViewHolder(@NonNull ReactionViewHolder reactionViewHolder, int i) { |
| 77 | + |
| 78 | + Reaction reaction = reactionArrayList.get(i); |
| 79 | + Log.e(TAG, "onBindViewHolder: "+reaction.getCode()); |
| 80 | + reactionViewHolder.reaction.setText(reaction.getCode()); |
| 81 | + reactionViewHolder.itemView.setTag(R.string.reaction,reaction); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public int getItemCount() { |
| 86 | + return reactionArrayList.size(); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + /** |
| 91 | + * This method is used to update the reactions of reactionArrayList in adapter. |
| 92 | + * |
| 93 | + * @param reactions is a list of updated reactions. |
| 94 | + */ |
| 95 | + public void updateList(List<Reaction> reactions) { |
| 96 | + for (int i = 0; i < reactions.size(); i++) { |
| 97 | + if (reactionArrayList.contains(reactions.get(i))){ |
| 98 | + int index=reactionArrayList.indexOf(reactions.get(i)); |
| 99 | + reactionArrayList.remove(index); |
| 100 | + reactionArrayList.add(index,reactions.get(i)); |
| 101 | + }else { |
| 102 | + reactionArrayList.add(reactions.get(i)); |
| 103 | + } |
| 104 | + } |
| 105 | + notifyDataSetChanged(); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + /** |
| 110 | + * This method is used to update particular user in userArrayList of adapter. |
| 111 | + * |
| 112 | + * @param reaction is a object of ReactionModel which will updated in reactionArrayList. |
| 113 | + * @see Reaction |
| 114 | + */ |
| 115 | + public void updateReaction(Reaction reaction) { |
| 116 | + if (reactionArrayList.contains(reaction)){ |
| 117 | + int index=reactionArrayList.indexOf(reaction); |
| 118 | + reactionArrayList.remove(index); |
| 119 | + reactionArrayList.add(index,reaction); |
| 120 | + notifyItemChanged(index); |
| 121 | + }else { |
| 122 | + reactionArrayList.add(reaction); |
| 123 | + notifyItemInserted(getItemCount()-1); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * This method is used to remove particular reaction from reactionArrayList of adapter. |
| 129 | + * |
| 130 | + * @param reaction is a object of user which will be removed from reactionArrayList. |
| 131 | + * @see Reaction |
| 132 | + */ |
| 133 | + public void remove(Reaction reaction) { |
| 134 | + if (reactionArrayList.contains(reaction)) { |
| 135 | + int index=reactionArrayList.indexOf(reaction); |
| 136 | + this.reactionArrayList.remove(reaction); |
| 137 | + notifyItemRemoved(index); |
| 138 | + } |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * This method is used to add a reaction in reactionArrayList. |
| 144 | + * @param reaction is a object of ReactionModel which will be added in reactionArrayList. |
| 145 | + * @see Reaction |
| 146 | + */ |
| 147 | + public void add(Reaction reaction) { |
| 148 | + updateReaction(reaction); |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + class ReactionViewHolder extends RecyclerView.ViewHolder { |
| 154 | + |
| 155 | + TextView reaction; |
| 156 | + ReactionViewHolder(View view) { |
| 157 | + super(view); |
| 158 | + reaction = view.findViewById(R.id.reaction); |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | +} |
0 commit comments