package com.kaixed.kchat.view.adapter;

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.kaixed.kchat.R;
import com.kaixed.kchat.model.user.User;

import java.util.List;

/**
 * @Author: kaixed
 * @Date: 2024/6/1 20:28
 * @Description: TODO
 */
public class UserListAdapter extends RecyclerView.Adapter<UserListAdapter.MyViewHolder> {

    private List<User> userList;

    public UserListAdapter(List<User> userList) {
        this.userList = userList;
    }

    public void setData(List<User> userList) {
        this.userList = userList;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        User user = userList.get(position);
        holder.bindData(user);
    }

    @Override
    public int getItemCount() {
        return userList.isEmpty() ? 0 : userList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private final TextView mTvNickname;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            mTvNickname = itemView.findViewById(R.id.tv_nickname);
        }

        public void bindData(User user) {
            mTvNickname.setText(user.getNickname());
        }
    }
}
