From e89cf7af46e267d0350e719334ade29975e3fc82 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sat, 19 Sep 2020 17:17:43 +0900 Subject: [PATCH] Keep follow requests at top in notification viewer --- .../adapters/NotificationsAdapter.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/app/src/main/java/awais/instagrabber/adapters/NotificationsAdapter.java b/app/src/main/java/awais/instagrabber/adapters/NotificationsAdapter.java index dac5f477..10e5867a 100644 --- a/app/src/main/java/awais/instagrabber/adapters/NotificationsAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/NotificationsAdapter.java @@ -4,13 +4,19 @@ 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; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import awais.instagrabber.adapters.viewholder.NotificationViewHolder; import awais.instagrabber.databinding.ItemNotificationBinding; import awais.instagrabber.interfaces.MentionClickListener; import awais.instagrabber.models.NotificationModel; +import awais.instagrabber.models.enums.NotificationType; public final class NotificationsAdapter extends ListAdapter { private final OnNotificationClickListener notificationClickListener; @@ -49,6 +55,36 @@ public final class NotificationsAdapter extends ListAdapter list, @Nullable final Runnable commitCallback) { + if (list == null) { + super.submitList(null, commitCallback); + return; + } + super.submitList(sort(list), commitCallback); + } + + @Override + public void submitList(@Nullable final List list) { + if (list == null) { + super.submitList(null); + return; + } + super.submitList(sort(list)); + } + + private List sort(final List list) { + final List listCopy = new ArrayList<>(list); + Collections.sort(listCopy, (o1, o2) -> { + if (o1.getType() == o2.getType()) return 0; + // keep requests at top + if (o1.getType() == NotificationType.REQUEST) return -1; + if (o2.getType() == NotificationType.REQUEST) return 1; + return 0; + }); + return listCopy; + } + public interface OnNotificationClickListener { void onNotificationClick(final NotificationModel model); }