diff options
| author | Danilo M. <danix@danix.xyz> | 2026-10-22 11:39:34 +0200 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-10-22 11:39:34 +0200 |
| commit | 1ffa52946d8de76806b0b467ccd60f4b2fe57683 (patch) | |
| tree | 52892e10f178225edd51bdd3b5c174c7e3de2513 /app/src/main/java/mygram/droid/Playlist.java | |
| parent | 40b12e5b4cd6a4352e37cd1b6e85b1da10624436 (diff) | |
| download | mygram-droid-1ffa52946d8de76806b0b467ccd60f4b2fe57683.tar.gz mygram-droid-1ffa52946d8de76806b0b467ccd60f4b2fe57683.zip | |
Lazy-load folder content in random page order
A folder with several thousand posts took ~10 s to open because
FolderActivity and the Home row OK handler fetched every page before
showing anything. Pages are now fetched one at a time in a shuffled page
order (each page shuffled locally), the grid shows after the first page,
and the viewer shares the list and pager so it keeps paging the folder.
Reels use the same generic pager. Not a full shuffle: neighbours within
a page stay together.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'app/src/main/java/mygram/droid/Playlist.java')
| -rw-r--r-- | app/src/main/java/mygram/droid/Playlist.java | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/app/src/main/java/mygram/droid/Playlist.java b/app/src/main/java/mygram/droid/Playlist.java index 98379fb..b885397 100644 --- a/app/src/main/java/mygram/droid/Playlist.java +++ b/app/src/main/java/mygram/droid/Playlist.java @@ -18,32 +18,43 @@ import android.content.Context; import android.content.Intent; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.concurrent.Callable; import java.util.concurrent.ThreadLocalRandom; /** Hands a post list to ViewerActivity; intents cannot carry hundreds of posts. */ final class Playlist { + /** Shared with the caller (e.g. a folder grid), not copied, so both see appended pages. */ static List<Post> posts = new ArrayList<>(); static int start; - /** Non-zero while the viewer plays endless Reels and pages for more. */ - static int reelsSeed; - static int reelsPage; + /** Fetches the next page of posts; null when there is nothing more. */ + static Callable<List<Post>> more; private Playlist() { } - static void open(Context c, List<Post> list, int startIndex) { - posts = new ArrayList<>(list); + static void open(Context c, List<Post> list, int startIndex, Callable<List<Post>> pager) { + posts = list; start = startIndex; - reelsSeed = 0; + more = pager; c.startActivity(new Intent(c, ViewerActivity.class)); } static void openReels(Context c) { - posts = new ArrayList<>(); - start = 0; - reelsSeed = 1 + ThreadLocalRandom.current().nextInt(1_000_000); - reelsPage = 0; - c.startActivity(new Intent(c, ViewerActivity.class)); + open(c, new ArrayList<>(), 0, Api.reelsPages(1 + ThreadLocalRandom.current().nextInt(1_000_000))); + } + + /** Appends the posts not already in the list; returns how many were added. */ + static int append(List<Post> page) { + Set<Integer> ids = new HashSet<>(); + for (Post p : posts) + ids.add(p.id); + int old = posts.size(); + for (Post p : page) + if (ids.add(p.id)) + posts.add(p); + return posts.size() - old; } } |
