aaaaa

 avatar
hacker98
plain_text
5 months ago
5.9 kB
1
Indexable
package com.example.chatappdemo;

import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.chatappdemo.model.Conversation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapter.ConversationViewHolder> {
    private List<Conversation> conversationList;
    private OnConversationClickListener listener;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss a");
    public ConversationAdapter(List<Conversation> conversationList, OnConversationClickListener listener) {
        this.conversationList = conversationList;
        this.listener=listener;

    }

    @NonNull
    @Override
    public ConversationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_conversation, parent, false);
        return new ConversationViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ConversationViewHolder holder, int position) {
        Conversation conversation = conversationList.get(position);
        holder.textViewConversationName.setText(conversation.getName());
        holder.textViewLastMessageTime.setText(conversation.getLast_mess_time());
        holder.textViewLastMessage.setText(conversation.getLast_mess());
        holder.bind(conversation, listener);
    }

    @Override
    public int getItemCount() {
        return conversationList.size();
    }
    // Hàm cập nhật toàn bộ danh sách conversation
    public void updateAllConversation(List<Conversation> newConversationList) {
        // Cập nhật lại danh sách dữ liệu mới
        this.conversationList.clear();
        this.conversationList.addAll(newConversationList);
        // Thông báo cho adapter rằng dữ liệu đã thay đổi
        notifyDataSetChanged();
    }
    public void updateConversation(Conversation NewConversation, int newMsg,String nowIP){
        if(nowIP.equals("") || !nowIP.equals(NewConversation.getIPreceive())){
            for(int i=0; i<conversationList.size(); i++){
                if(conversationList.get(i).getIPreceive().equals(NewConversation.getIPreceive())){
                    conversationList.get(i).setLast_mess(NewConversation.getLast_mess());
                    conversationList.get(i).setLast_mess_time(NewConversation.getLast_mess_time());
                    conversationList.get(i).setIsNewMessage(1);

                    conversationList.add(0,conversationList.remove(i));
                    notifyDataSetChanged();
                    return;
                }
            }
            conversationList.add(0,NewConversation);
            notifyDataSetChanged();
        }else {

        }

    }
    // Sắp xếp danh sách cuộc hội thoại theo thời gian tin nhắn cuối cùng
    public void sortConversationsByTime() {
        Collections.sort(conversationList, new Comparator<Conversation>() {
            @Override
            public int compare(Conversation c1, Conversation c2) {
                try {
                    Date date1 = dateFormat.parse(c1.getLast_mess_time());
                    Date date2 = dateFormat.parse(c2.getLast_mess_time());
                    return date2.compareTo(date1); // Sắp xếp giảm dần theo thời gian
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
    }
    public static class ConversationViewHolder extends RecyclerView.ViewHolder {
        TextView textViewConversationName;
        TextView textViewLastMessageTime;
        TextView textViewLastMessage;
        public ConversationViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewConversationName = itemView.findViewById(R.id.textViewConversationName);
            textViewLastMessageTime = itemView.findViewById(R.id.textViewLastMessageTime);
            textViewLastMessage=itemView.findViewById(R.id.textViewLastMessage);
        }
        public void bind(Conversation conversation, OnConversationClickListener listener){
            textViewConversationName.setText(conversation.getName()); // hoặc tên cuộc trò chuyện
            textViewLastMessage.setText(conversation.getLast_mess());
            textViewLastMessageTime.setText(conversation.getLast_mess_time());
            String test= Integer.toString(conversation.getIsNewMessage());

            Log.i("conversation","cap nhat cuoc tro chuyen"+ test);
            if(conversation.getIsNewMessage()==1){
                textViewConversationName.setTypeface(null, Typeface.BOLD);
                textViewLastMessage.setTypeface(null, Typeface.BOLD);
                textViewLastMessageTime.setTypeface(null, Typeface.BOLD);
            }else{
                textViewConversationName.setTypeface(null, Typeface.NORMAL);
                textViewLastMessage.setTypeface(null, Typeface.NORMAL);
                textViewLastMessageTime.setTypeface(null, Typeface.NORMAL);
            }

            // Xử lý sự kiện click vào item
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onConverstionClick(conversation);
                }
            });
        }
    }
}
Editor is loading...
Leave a Comment