From 2ad650ba2f54e1b322bc9410576395b8f021195d Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 7 May 2022 13:55:15 +0200 Subject: [PATCH] Store timelines in db for faster start / switch between them. --- .../android/client/entities/QuickLoad.java | 17 +++++++++++++++++ .../timeline/FragmentMastodonTimeline.java | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/app/fedilab/android/client/entities/QuickLoad.java b/app/src/main/java/app/fedilab/android/client/entities/QuickLoad.java index d2c6ae54..ccc8a0d3 100644 --- a/app/src/main/java/app/fedilab/android/client/entities/QuickLoad.java +++ b/app/src/main/java/app/fedilab/android/client/entities/QuickLoad.java @@ -27,6 +27,7 @@ import java.util.List; import app.fedilab.android.activities.MainActivity; import app.fedilab.android.client.mastodon.entities.Status; import app.fedilab.android.exception.DBException; +import app.fedilab.android.helper.MastodonHelper; import app.fedilab.android.helper.SpannableHelper; import app.fedilab.android.sqlite.Sqlite; @@ -126,6 +127,7 @@ public class QuickLoad { quickLoad.slug = key; quickLoad.instance = MainActivity.currentInstance; quickLoad.user_id = MainActivity.currentUserID; + purge(quickLoad); try { insertOrUpdate(quickLoad); } catch (DBException e) { @@ -180,6 +182,21 @@ public class QuickLoad { return null; } + /** + * Purge the list to avoid long list of Statuses in db + * + * @param quickLoad - QuickLoad to purge + */ + private void purge(QuickLoad quickLoad) { + List statuses = quickLoad.statuses; + int position = quickLoad.position; + int limit = MastodonHelper.statusesPerCall(_mContext) + 10; + int startAt = Math.max(position - limit, 0); + int endAt = Math.min(position + limit, statuses.size()); + quickLoad.position = position - startAt; + quickLoad.statuses = statuses.subList(startAt, endAt); + } + /** * Update a QuickLoad in db * diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java index 429ad6cc..58021a72 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java @@ -408,7 +408,7 @@ public class FragmentMastodonTimeline extends Fragment { new Thread(() -> { QuickLoad quickLoad = new QuickLoad(requireActivity()).getSavedValue(timelineType, ident); - if (quickLoad != null && quickLoad.statuses != null && quickLoad.statuses.size() > 0) { + if (direction == null && quickLoad != null && quickLoad.statuses != null && quickLoad.statuses.size() > 0) { Statuses statuses = new Statuses(); statuses.statuses = quickLoad.statuses; statuses.pagination = new Pagination();