| 21 | import models.MessageModel; |
| 22 | |
| 23 | public class messageAdapter extends RecyclerView.Adapter { |
| 24 | |
| 25 | ArrayList<MessageModel> msgData; |
| 26 | Context context; |
| 27 | FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(); |
| 28 | |
| 29 | final int SENDER_VIEWHOLDER = 0; |
| 30 | final int RECEIVER_VIEWHOLDER = 1; |
| 31 | |
| 32 | |
| 33 | public messageAdapter(ArrayList<MessageModel> msgData, Context context) { |
| 34 | |
| 35 | this.msgData = msgData; |
| 36 | this.context = context; |
| 37 | |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public int getItemViewType(int position) { |
| 42 | |
| 43 | if (msgData.get(position).getuId().equals(firebaseAuth.getUid())) |
| 44 | return SENDER_VIEWHOLDER; |
| 45 | else |
| 46 | return RECEIVER_VIEWHOLDER; |
| 47 | |
| 48 | } |
| 49 | |
| 50 | @NonNull |
| 51 | @Override |
| 52 | public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
| 53 | |
| 54 | if (viewType == SENDER_VIEWHOLDER) { |
| 55 | View view = LayoutInflater.from(context).inflate(R.layout.sender_listitem, parent, false); |
| 56 | return new OutgoingViewholder(view); |
| 57 | } else { |
| 58 | View view = LayoutInflater.from(context).inflate(R.layout.receiver_listitem, parent, false); |
| 59 | return new IncomingViewholder(view); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { |
| 65 | |
| 66 | if (holder.getClass() == OutgoingViewholder.class) { |
| 67 | ((OutgoingViewholder) holder).outgoingMsg.setText(msgData.get(position).getMsgText()); |
| 68 | |
| 69 | long time = msgData.get(position).getMsgTime(); |
| 70 | final Calendar cal = Calendar.getInstance(); |
| 71 | cal.setTimeInMillis(time); |
| 72 | final String timeString = |
| 73 | new SimpleDateFormat("HH:mm").format(cal.getTime()); |
| 74 | |
| 75 | ((OutgoingViewholder) holder).outgoingMsgTime.setText(timeString); |
| 76 | } else { |
| 77 | |
| 78 | ((IncomingViewholder) holder).incomingMsg.setText(msgData.get(position).getMsgText()); |
| 79 | long time = msgData.get(position).getMsgTime(); |
| 80 | final Calendar cal = Calendar.getInstance(); |
nothing calls this directly
no outgoing calls
no test coverage detected