168 lines
6.0 KiB
Java
168 lines
6.0 KiB
Java
|
|
package com.example.livestreaming;
|
||
|
|
|
||
|
|
import android.text.TextUtils;
|
||
|
|
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.bumptech.glide.Glide;
|
||
|
|
import com.example.livestreaming.databinding.ItemNotificationBinding;
|
||
|
|
|
||
|
|
import java.text.SimpleDateFormat;
|
||
|
|
import java.util.Date;
|
||
|
|
import java.util.Locale;
|
||
|
|
|
||
|
|
public class NotificationsAdapter extends ListAdapter<NotificationItem, NotificationsAdapter.VH> {
|
||
|
|
|
||
|
|
public interface OnNotificationClickListener {
|
||
|
|
void onNotificationClick(NotificationItem item);
|
||
|
|
}
|
||
|
|
|
||
|
|
private final OnNotificationClickListener onNotificationClickListener;
|
||
|
|
|
||
|
|
public NotificationsAdapter(OnNotificationClickListener onNotificationClickListener) {
|
||
|
|
super(DIFF);
|
||
|
|
this.onNotificationClickListener = onNotificationClickListener;
|
||
|
|
}
|
||
|
|
|
||
|
|
@NonNull
|
||
|
|
@Override
|
||
|
|
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||
|
|
ItemNotificationBinding binding = ItemNotificationBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
|
||
|
|
return new VH(binding, onNotificationClickListener);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void onBindViewHolder(@NonNull VH holder, int position) {
|
||
|
|
holder.bind(getItem(position));
|
||
|
|
}
|
||
|
|
|
||
|
|
static class VH extends RecyclerView.ViewHolder {
|
||
|
|
|
||
|
|
private final ItemNotificationBinding binding;
|
||
|
|
private final OnNotificationClickListener onNotificationClickListener;
|
||
|
|
private final SimpleDateFormat timeFormat = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());
|
||
|
|
private final SimpleDateFormat todayFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
|
||
|
|
|
||
|
|
VH(ItemNotificationBinding binding, OnNotificationClickListener onNotificationClickListener) {
|
||
|
|
super(binding.getRoot());
|
||
|
|
this.binding = binding;
|
||
|
|
this.onNotificationClickListener = onNotificationClickListener;
|
||
|
|
}
|
||
|
|
|
||
|
|
void bind(NotificationItem item) {
|
||
|
|
if (item == null) return;
|
||
|
|
|
||
|
|
// 设置标题和内容
|
||
|
|
binding.title.setText(item.getTitle() != null ? item.getTitle() : "");
|
||
|
|
binding.content.setText(item.getContent() != null ? item.getContent() : "");
|
||
|
|
|
||
|
|
// 设置时间
|
||
|
|
Date timestamp = item.getTimestamp();
|
||
|
|
if (timestamp != null) {
|
||
|
|
String timeText = formatTime(timestamp);
|
||
|
|
binding.timeText.setText(timeText);
|
||
|
|
} else {
|
||
|
|
binding.timeText.setText("");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 设置未读标记
|
||
|
|
if (!item.isRead()) {
|
||
|
|
binding.unreadDot.setVisibility(View.VISIBLE);
|
||
|
|
} else {
|
||
|
|
binding.unreadDot.setVisibility(View.GONE);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 加载头像
|
||
|
|
loadAvatar(item);
|
||
|
|
|
||
|
|
// 设置点击事件
|
||
|
|
binding.getRoot().setOnClickListener(v -> {
|
||
|
|
if (onNotificationClickListener != null) {
|
||
|
|
onNotificationClickListener.onNotificationClick(item);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private String formatTime(Date date) {
|
||
|
|
if (date == null) return "";
|
||
|
|
|
||
|
|
Date now = new Date();
|
||
|
|
long diff = now.getTime() - date.getTime();
|
||
|
|
|
||
|
|
// 如果是今天,只显示时间
|
||
|
|
if (diff < 24 * 60 * 60 * 1000 && isToday(date, now)) {
|
||
|
|
return todayFormat.format(date);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 否则显示日期和时间
|
||
|
|
return timeFormat.format(date);
|
||
|
|
}
|
||
|
|
|
||
|
|
private boolean isToday(Date date, Date now) {
|
||
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
|
||
|
|
return dateFormat.format(date).equals(dateFormat.format(now));
|
||
|
|
}
|
||
|
|
|
||
|
|
private void loadAvatar(NotificationItem item) {
|
||
|
|
if (binding.avatar == null) return;
|
||
|
|
|
||
|
|
String avatarUrl = item.getAvatarUrl();
|
||
|
|
if (!TextUtils.isEmpty(avatarUrl)) {
|
||
|
|
Glide.with(binding.avatar)
|
||
|
|
.load(avatarUrl)
|
||
|
|
.circleCrop()
|
||
|
|
.placeholder(R.drawable.ic_account_circle_24)
|
||
|
|
.error(R.drawable.ic_account_circle_24)
|
||
|
|
.into(binding.avatar);
|
||
|
|
} else {
|
||
|
|
// 根据通知类型设置默认图标
|
||
|
|
int iconRes = getIconForType(item.getType());
|
||
|
|
Glide.with(binding.avatar)
|
||
|
|
.load(iconRes)
|
||
|
|
.circleCrop()
|
||
|
|
.into(binding.avatar);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private int getIconForType(NotificationItem.Type type) {
|
||
|
|
if (type == null) return R.drawable.ic_notifications_24;
|
||
|
|
|
||
|
|
switch (type) {
|
||
|
|
case SYSTEM:
|
||
|
|
return R.drawable.ic_notifications_24;
|
||
|
|
case INTERACTION:
|
||
|
|
return R.drawable.ic_chat_24;
|
||
|
|
case FOLLOW:
|
||
|
|
return R.drawable.ic_people_24;
|
||
|
|
case MESSAGE:
|
||
|
|
return R.drawable.ic_chat_24;
|
||
|
|
case LIVE:
|
||
|
|
return R.drawable.ic_voice_24;
|
||
|
|
default:
|
||
|
|
return R.drawable.ic_notifications_24;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static final DiffUtil.ItemCallback<NotificationItem> DIFF = new DiffUtil.ItemCallback<NotificationItem>() {
|
||
|
|
@Override
|
||
|
|
public boolean areItemsTheSame(@NonNull NotificationItem oldItem, @NonNull NotificationItem newItem) {
|
||
|
|
String o = oldItem.getId();
|
||
|
|
String n = newItem.getId();
|
||
|
|
return o != null && o.equals(n);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public boolean areContentsTheSame(@NonNull NotificationItem oldItem, @NonNull NotificationItem newItem) {
|
||
|
|
return oldItem.equals(newItem) && oldItem.isRead() == newItem.isRead();
|
||
|
|
}
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|