Advertisement
Guest User

Adapter

a guest
Aug 20th, 2017
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.42 KB | None | 0 0
  1. public class Single_chat_adapter extends RecyclerView.Adapter{
  2.  
  3.     private static final int VIEW_TYPE_MESSAGE_SENT = 1;
  4.     private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
  5.  
  6.  
  7.     private Context mContext;
  8.     private List<Datum2> data;
  9.     private  String userID;
  10.  
  11.     private UserAuthenticationKey userAuthenticationKey;
  12.     private SharedPreferences sharedPreferences;
  13.     private SharedPreferences.Editor editor;
  14.  
  15.  
  16.     public Single_chat_adapter(Context mContext, List<Datum2> data, String userID) {
  17.         this.mContext = mContext;
  18.         this.data = data;
  19.         this.userID = userID;
  20.     }
  21.  
  22.     @Override
  23.     public int getItemCount() {
  24.         return data.size();
  25.     }
  26.  
  27.  
  28.     // Determines the appropriate ViewType according to the sender of the message.
  29.     @Override
  30.     public int getItemViewType(int position) {
  31.         Datum2 message = (Datum2) data.get(position);
  32.  
  33.         if (message.getOriginatorId().equals(userID)) {
  34.             // If the current user is the sender of the message
  35.             return VIEW_TYPE_MESSAGE_SENT;
  36.         } else {
  37.             // If some other user sent the message
  38.             return VIEW_TYPE_MESSAGE_RECEIVED;
  39.         }
  40.     }
  41.  
  42.  
  43.     // Inflates the appropriate layout according to the ViewType.
  44.     @Override
  45.     public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
  46.  
  47.         View view;
  48.         if (viewType == VIEW_TYPE_MESSAGE_SENT) {
  49.             view = LayoutInflater.from(parent.getContext())
  50.                     .inflate(R.layout.item_message_sent, parent, false);
  51.             return new SentMessageHolder(view);
  52.  
  53.         } else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED){
  54.             view = LayoutInflater.from(parent.getContext())
  55.                     .inflate(R.layout.item_message_received, parent, false);
  56.             return new ReceivedMessageHolder(view);
  57.         }
  58.         return null;
  59.     }
  60.  
  61.  
  62.  
  63.     // Passes the message object to a ViewHolder so that the contents can be bound to UI.
  64.     @Override
  65.     public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  66.         Datum2 message = (Datum2) data.get(position);
  67.  
  68.         switch (holder.getItemViewType()) {
  69.             case VIEW_TYPE_MESSAGE_SENT:
  70.                 ((SentMessageHolder) holder).bind(message);
  71.  
  72.                 break;
  73.             case VIEW_TYPE_MESSAGE_RECEIVED:
  74.                 ((ReceivedMessageHolder) holder).bind(message);
  75.         }
  76.  
  77.     }
  78.  
  79.     // Sent message view holder
  80.     private class SentMessageHolder extends RecyclerView.ViewHolder {
  81.         TextView messageText, timeText;
  82.         ImageButton iB;
  83.         MsgDltAPIService msgDltAPIService;
  84.         String rec_id;
  85.         String content_id;
  86.         int[] ids = new int[100];
  87.  
  88.         SentMessageHolder(final View itemView) {
  89.             super(itemView);
  90.  
  91.             messageText = itemView.findViewById(R.id.text_message_body);
  92.             timeText = itemView.findViewById(R.id.text_message_time);
  93.             iB = itemView.findViewById(R.id.sentMessageTextDelete);
  94.             msgDltAPIService = RestClient.getClient().create(MsgDltAPIService.class);
  95.  
  96.             iB.setOnClickListener(new View.OnClickListener() {
  97.                 @Override
  98.                 public void onClick(View view) {
  99.                     userAuthenticationKey = new UserAuthenticationKey(mContext.getApplicationContext());
  100.                     sharedPreferences =  mContext.getApplicationContext().getSharedPreferences("user authentication", MODE_PRIVATE);
  101.                     editor = sharedPreferences.edit();
  102.                     ids[0] = Integer.parseInt(content_id);
  103.                     final MsgDltRequest msgDltRequest = new MsgDltRequest(
  104.                             ids,
  105.                             rec_id);
  106.                     Call<MsgDltResponse> call =
  107.                             msgDltAPIService.msgDlt(userAuthenticationKey.getUserTokenKey(),
  108.                                     msgDltRequest);
  109.                     call.enqueue(new Callback<MsgDltResponse>() {
  110.                         @Override
  111.                         public void onResponse(Call<MsgDltResponse> call, Response<MsgDltResponse> response) {
  112.  
  113.                             Toast.makeText(mContext.getApplicationContext(), "" + response.body().getData(),
  114.                                     Toast.LENGTH_LONG).show();
  115.                         }
  116.  
  117.                         @Override
  118.                         public void onFailure(Call<MsgDltResponse> call, Throwable t) {
  119.                             Toast.makeText(mContext.getApplicationContext(), "please try again",
  120.                                     Toast.LENGTH_LONG).show();
  121.                         }
  122.                     });
  123.                 }
  124.             });
  125.         }
  126.  
  127.         void bind(Datum2 message) {
  128.             messageText.setText(message.getMsg());
  129.  
  130.             // Format the stored timestamp into a readable String using method.
  131.             timeText.setText(message.getCreatedAt().getFormatTime());
  132.             content_id.valueOf(message.getContentId().toString()) ;
  133.             if (! message.getOriginatorId().equals(userID)){
  134.                 rec_id.valueOf(message.getOriginatorId());
  135.             }
  136.             else {
  137.                 rec_id = null;
  138.             }
  139.                             }
  140.  
  141.     }
  142.  
  143.     // Received message view holder
  144.     private class ReceivedMessageHolder extends RecyclerView.ViewHolder {
  145.         TextView messageText, timeText, nameText;
  146.         ImageView profileImage;
  147.  
  148.         ReceivedMessageHolder(View itemView) {
  149.             super(itemView);
  150.  
  151.             messageText = itemView.findViewById(R.id.text_message_body);
  152.             timeText = itemView.findViewById(R.id.text_message_time);
  153.             nameText = itemView.findViewById(R.id.text_message_name);
  154.             profileImage = itemView.findViewById(R.id.image_message_profile);
  155.         }
  156.  
  157.         void bind(Datum2 message) {
  158.             messageText.setText(message.getMsg());
  159.  
  160.             // Format the stored timestamp into a readable String using method.
  161.             timeText.setText(message.getCreatedAt().getFormatTime());
  162.             nameText.setText(message.getOriginator().getFullName());
  163.  
  164.             // Insert the profile image from the URL into the ImageView.
  165.             Glide.with(mContext).load("/" + data.get(getLayoutPosition()).getOriginator().getAvatar()).apply(RequestOptions.circleCropTransform()).into(profileImage);
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement