From c95dfda3df97035c12492ebf7fd76e19ece65ac8 Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 30 May 2022 16:44:50 +0200 Subject: [PATCH] Add methods for later --- .../android/helper/MastodonHelper.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/app/src/main/java/app/fedilab/android/helper/MastodonHelper.java b/app/src/main/java/app/fedilab/android/helper/MastodonHelper.java index 13bfee20..407cfb5a 100644 --- a/app/src/main/java/app/fedilab/android/helper/MastodonHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/MastodonHelper.java @@ -32,6 +32,7 @@ import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelStoreOwner; import androidx.preference.PreferenceManager; +import androidx.recyclerview.widget.RecyclerView; import androidx.work.Data; import androidx.work.OneTimeWorkRequest; @@ -42,6 +43,7 @@ import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; +import java.util.List; import java.util.Locale; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; @@ -440,6 +442,44 @@ public class MastodonHelper { } + /** + * Insert a single message depending of its publication date + * + * @param adapter - RecyclerView.Adapter + * @param currentStatusList - Current list of messages List + * @param statusToInsert - status to insert - Status + */ + public static void insertStatus(RecyclerView.Adapter adapter, List currentStatusList, Status statusToInsert) { + if (adapter == null || currentStatusList == null || statusToInsert == null) { + return; + } + int i = 0; + while (i < currentStatusList.size() && statusToInsert.created_at.before(currentStatusList.get(i).created_at)) { + i++; + } + currentStatusList.add(i, statusToInsert); + adapter.notifyItemInserted(i); + } + + /** + * Insert a list of messages depending of its publication date + * + * @param adapter - RecyclerView.Adapter + * @param currentStatusList - Current list of messages List + * @param statusesToInsert - statuses to insert - List + */ + public static void insertStatuses(RecyclerView.Adapter adapter, List currentStatusList, List statusesToInsert) { + if (adapter == null || currentStatusList == null || statusesToInsert == null || statusesToInsert.size() == 0) { + return; + } + int i = 0; + while (i < currentStatusList.size() && statusesToInsert.get(statusesToInsert.size() - 1).created_at.before(currentStatusList.get(i).created_at)) { + i++; + } + currentStatusList.addAll(i, statusesToInsert); + adapter.notifyItemRangeInserted(i, statusesToInsert.size()); + } + public enum ScheduleType { BOOST, TIMED_MUTED