From 1ffa52946d8de76806b0b467ccd60f4b2fe57683 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Thu, 22 Oct 2026 11:39:34 +0200 Subject: 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 --- app/src/main/java/mygram/droid/Playlist.java | 33 ++++++++++++++++++---------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'app/src/main/java/mygram/droid/Playlist.java') 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 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> more; private Playlist() { } - static void open(Context c, List list, int startIndex) { - posts = new ArrayList<>(list); + static void open(Context c, List list, int startIndex, Callable> 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 page) { + Set 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; } } -- cgit v1.2.3