90 lines
3.4 KiB
Java
90 lines
3.4 KiB
Java
|
|
package com.example.livestreaming;
|
||
|
|
|
||
|
|
import android.view.LayoutInflater;
|
||
|
|
import android.view.View;
|
||
|
|
import android.view.ViewGroup;
|
||
|
|
|
||
|
|
import androidx.annotation.NonNull;
|
||
|
|
import androidx.recyclerview.widget.DiffUtil;
|
||
|
|
import androidx.recyclerview.widget.ListAdapter;
|
||
|
|
import androidx.recyclerview.widget.RecyclerView;
|
||
|
|
|
||
|
|
import com.example.livestreaming.databinding.ItemConversationBinding;
|
||
|
|
|
||
|
|
public class ConversationsAdapter extends ListAdapter<ConversationItem, ConversationsAdapter.VH> {
|
||
|
|
|
||
|
|
public interface OnConversationClickListener {
|
||
|
|
void onConversationClick(ConversationItem item);
|
||
|
|
}
|
||
|
|
|
||
|
|
private final OnConversationClickListener onConversationClickListener;
|
||
|
|
|
||
|
|
public ConversationsAdapter(OnConversationClickListener onConversationClickListener) {
|
||
|
|
super(DIFF);
|
||
|
|
this.onConversationClickListener = onConversationClickListener;
|
||
|
|
}
|
||
|
|
|
||
|
|
@NonNull
|
||
|
|
@Override
|
||
|
|
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||
|
|
ItemConversationBinding binding = ItemConversationBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
|
||
|
|
return new VH(binding, onConversationClickListener);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void onBindViewHolder(@NonNull VH holder, int position) {
|
||
|
|
holder.bind(getItem(position));
|
||
|
|
}
|
||
|
|
|
||
|
|
static class VH extends RecyclerView.ViewHolder {
|
||
|
|
|
||
|
|
private final ItemConversationBinding binding;
|
||
|
|
private final OnConversationClickListener onConversationClickListener;
|
||
|
|
|
||
|
|
VH(ItemConversationBinding binding, OnConversationClickListener onConversationClickListener) {
|
||
|
|
super(binding.getRoot());
|
||
|
|
this.binding = binding;
|
||
|
|
this.onConversationClickListener = onConversationClickListener;
|
||
|
|
}
|
||
|
|
|
||
|
|
void bind(ConversationItem item) {
|
||
|
|
binding.title.setText(item != null && item.getTitle() != null ? item.getTitle() : "");
|
||
|
|
binding.lastMessage.setText(item != null && item.getLastMessage() != null ? item.getLastMessage() : "");
|
||
|
|
binding.timeText.setText(item != null && item.getTimeText() != null ? item.getTimeText() : "");
|
||
|
|
|
||
|
|
int unread = item != null ? item.getUnreadCount() : 0;
|
||
|
|
if (unread > 0) {
|
||
|
|
binding.unreadBadge.setVisibility(View.VISIBLE);
|
||
|
|
binding.unreadBadge.setText(unread > 99 ? "99+" : String.valueOf(unread));
|
||
|
|
} else {
|
||
|
|
binding.unreadBadge.setVisibility(View.GONE);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (item != null && item.isMuted()) {
|
||
|
|
binding.muteIcon.setVisibility(View.VISIBLE);
|
||
|
|
} else {
|
||
|
|
binding.muteIcon.setVisibility(View.GONE);
|
||
|
|
}
|
||
|
|
|
||
|
|
binding.getRoot().setOnClickListener(v -> {
|
||
|
|
if (item == null) return;
|
||
|
|
if (onConversationClickListener != null) onConversationClickListener.onConversationClick(item);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static final DiffUtil.ItemCallback<ConversationItem> DIFF = new DiffUtil.ItemCallback<ConversationItem>() {
|
||
|
|
@Override
|
||
|
|
public boolean areItemsTheSame(@NonNull ConversationItem oldItem, @NonNull ConversationItem newItem) {
|
||
|
|
String o = oldItem.getId();
|
||
|
|
String n = newItem.getId();
|
||
|
|
return o != null && o.equals(n);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public boolean areContentsTheSame(@NonNull ConversationItem oldItem, @NonNull ConversationItem newItem) {
|
||
|
|
return oldItem.equals(newItem);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
}
|