BarInsta/app/src/main/java/awais/instagrabber/adapters/NotificationsAdapter.java

98 lines
3.9 KiB
Java
Raw Normal View History

2020-07-31 19:53:32 +00:00
package awais.instagrabber.adapters;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
2020-07-31 19:53:32 +00:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
2021-03-04 20:27:46 +00:00
import java.util.stream.Collectors;
2020-07-31 19:53:32 +00:00
import awais.instagrabber.adapters.viewholder.NotificationViewHolder;
import awais.instagrabber.databinding.ItemNotificationBinding;
import awais.instagrabber.models.enums.NotificationType;
2021-03-22 18:51:44 +00:00
import awais.instagrabber.repositories.responses.notification.Notification;
2020-07-31 19:53:32 +00:00
2021-03-04 20:27:46 +00:00
public final class NotificationsAdapter extends ListAdapter<Notification, NotificationViewHolder> {
private final OnNotificationClickListener notificationClickListener;
2020-07-31 19:53:32 +00:00
2021-03-04 20:27:46 +00:00
private static final DiffUtil.ItemCallback<Notification> DIFF_CALLBACK = new DiffUtil.ItemCallback<Notification>() {
@Override
2021-05-19 16:06:34 +00:00
public boolean areItemsTheSame(final Notification oldItem, final Notification newItem) {
return Objects.requireNonNull(oldItem.getPk()).equals(newItem.getPk());
}
@Override
2021-03-04 20:27:46 +00:00
public boolean areContentsTheSame(@NonNull final Notification oldItem, @NonNull final Notification newItem) {
return Objects.requireNonNull(oldItem.getPk()).equals(newItem.getPk()) && Objects.equals(oldItem.getType(), newItem.getType());
}
};
public NotificationsAdapter(final OnNotificationClickListener notificationClickListener) {
super(DIFF_CALLBACK);
this.notificationClickListener = notificationClickListener;
2020-07-31 19:53:32 +00:00
}
@NonNull
@Override
public NotificationViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int type) {
final LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
final ItemNotificationBinding binding = ItemNotificationBinding.inflate(layoutInflater, parent, false);
return new NotificationViewHolder(binding);
2020-07-31 19:53:32 +00:00
}
@Override
public void onBindViewHolder(@NonNull final NotificationViewHolder holder, final int position) {
2021-03-04 20:27:46 +00:00
final Notification Notification = getItem(position);
holder.bind(Notification, notificationClickListener);
2020-07-31 19:53:32 +00:00
}
@Override
2021-03-04 20:27:46 +00:00
public void submitList(@Nullable final List<Notification> list, @Nullable final Runnable commitCallback) {
if (list == null) {
super.submitList(null, commitCallback);
return;
}
super.submitList(sort(list), commitCallback);
}
@Override
2021-03-04 20:27:46 +00:00
public void submitList(@Nullable final List<Notification> list) {
if (list == null) {
super.submitList(null);
return;
}
super.submitList(sort(list));
}
2021-03-04 20:27:46 +00:00
private List<Notification> sort(final List<Notification> list) {
final List<Notification> listCopy = new ArrayList<>(list).stream()
.filter(i -> i.getType() != null)
.collect(Collectors.toList());
Collections.sort(listCopy, (o1, o2) -> {
// keep requests at top
if (o1.getType() == o2.getType()
&& o1.getType() == NotificationType.REQUEST
&& o2.getType() == NotificationType.REQUEST) return 0;
else if (o1.getType() == NotificationType.REQUEST) return -1;
else if (o2.getType() == NotificationType.REQUEST) return 1;
// timestamp
2021-03-04 20:27:46 +00:00
return Double.compare(o2.getArgs().getTimestamp(), o1.getArgs().getTimestamp());
});
return listCopy;
}
public interface OnNotificationClickListener {
2021-03-04 20:27:46 +00:00
void onNotificationClick(final Notification model);
void onProfileClick(final String username);
2021-03-04 20:27:46 +00:00
void onPreviewClick(final Notification model);
2020-07-31 19:53:32 +00:00
}
}