Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Single_chat_adapter extends RecyclerView.Adapter{
- private static final int VIEW_TYPE_MESSAGE_SENT = 1;
- private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
- private Context mContext;
- private List<Datum2> data;
- private String userID;
- private UserAuthenticationKey userAuthenticationKey;
- private SharedPreferences sharedPreferences;
- private SharedPreferences.Editor editor;
- public Single_chat_adapter(Context mContext, List<Datum2> data, String userID) {
- this.mContext = mContext;
- this.data = data;
- this.userID = userID;
- }
- @Override
- public int getItemCount() {
- return data.size();
- }
- // Determines the appropriate ViewType according to the sender of the message.
- @Override
- public int getItemViewType(int position) {
- Datum2 message = (Datum2) data.get(position);
- if (message.getOriginatorId().equals(userID)) {
- // If the current user is the sender of the message
- return VIEW_TYPE_MESSAGE_SENT;
- } else {
- // If some other user sent the message
- return VIEW_TYPE_MESSAGE_RECEIVED;
- }
- }
- // Inflates the appropriate layout according to the ViewType.
- @Override
- public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
- View view;
- if (viewType == VIEW_TYPE_MESSAGE_SENT) {
- view = LayoutInflater.from(parent.getContext())
- .inflate(R.layout.item_message_sent, parent, false);
- return new SentMessageHolder(view);
- } else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED){
- view = LayoutInflater.from(parent.getContext())
- .inflate(R.layout.item_message_received, parent, false);
- return new ReceivedMessageHolder(view);
- }
- return null;
- }
- // Passes the message object to a ViewHolder so that the contents can be bound to UI.
- @Override
- public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
- Datum2 message = (Datum2) data.get(position);
- switch (holder.getItemViewType()) {
- case VIEW_TYPE_MESSAGE_SENT:
- ((SentMessageHolder) holder).bind(message);
- break;
- case VIEW_TYPE_MESSAGE_RECEIVED:
- ((ReceivedMessageHolder) holder).bind(message);
- }
- }
- // Sent message view holder
- private class SentMessageHolder extends RecyclerView.ViewHolder {
- TextView messageText, timeText;
- ImageButton iB;
- MsgDltAPIService msgDltAPIService;
- String rec_id;
- String content_id;
- int[] ids = new int[100];
- SentMessageHolder(final View itemView) {
- super(itemView);
- messageText = itemView.findViewById(R.id.text_message_body);
- timeText = itemView.findViewById(R.id.text_message_time);
- iB = itemView.findViewById(R.id.sentMessageTextDelete);
- msgDltAPIService = RestClient.getClient().create(MsgDltAPIService.class);
- iB.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- userAuthenticationKey = new UserAuthenticationKey(mContext.getApplicationContext());
- sharedPreferences = mContext.getApplicationContext().getSharedPreferences("user authentication", MODE_PRIVATE);
- editor = sharedPreferences.edit();
- ids[0] = Integer.parseInt(content_id);
- final MsgDltRequest msgDltRequest = new MsgDltRequest(
- ids,
- rec_id);
- Call<MsgDltResponse> call =
- msgDltAPIService.msgDlt(userAuthenticationKey.getUserTokenKey(),
- msgDltRequest);
- call.enqueue(new Callback<MsgDltResponse>() {
- @Override
- public void onResponse(Call<MsgDltResponse> call, Response<MsgDltResponse> response) {
- Toast.makeText(mContext.getApplicationContext(), "" + response.body().getData(),
- Toast.LENGTH_LONG).show();
- }
- @Override
- public void onFailure(Call<MsgDltResponse> call, Throwable t) {
- Toast.makeText(mContext.getApplicationContext(), "please try again",
- Toast.LENGTH_LONG).show();
- }
- });
- }
- });
- }
- void bind(Datum2 message) {
- messageText.setText(message.getMsg());
- // Format the stored timestamp into a readable String using method.
- timeText.setText(message.getCreatedAt().getFormatTime());
- content_id.valueOf(message.getContentId().toString()) ;
- if (! message.getOriginatorId().equals(userID)){
- rec_id.valueOf(message.getOriginatorId());
- }
- else {
- rec_id = null;
- }
- }
- }
- // Received message view holder
- private class ReceivedMessageHolder extends RecyclerView.ViewHolder {
- TextView messageText, timeText, nameText;
- ImageView profileImage;
- ReceivedMessageHolder(View itemView) {
- super(itemView);
- messageText = itemView.findViewById(R.id.text_message_body);
- timeText = itemView.findViewById(R.id.text_message_time);
- nameText = itemView.findViewById(R.id.text_message_name);
- profileImage = itemView.findViewById(R.id.image_message_profile);
- }
- void bind(Datum2 message) {
- messageText.setText(message.getMsg());
- // Format the stored timestamp into a readable String using method.
- timeText.setText(message.getCreatedAt().getFormatTime());
- nameText.setText(message.getOriginator().getFullName());
- // Insert the profile image from the URL into the ImageView.
- Glide.with(mContext).load("/" + data.get(getLayoutPosition()).getOriginator().getAvatar()).apply(RequestOptions.circleCropTransform()).into(profileImage);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement