aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/mygram/droid/Api.java
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-10-22 11:39:34 +0200
committerDanilo M. <danix@danix.xyz>2026-10-22 11:39:34 +0200
commit1ffa52946d8de76806b0b467ccd60f4b2fe57683 (patch)
tree52892e10f178225edd51bdd3b5c174c7e3de2513 /app/src/main/java/mygram/droid/Api.java
parent40b12e5b4cd6a4352e37cd1b6e85b1da10624436 (diff)
downloadmygram-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/Api.java')
-rw-r--r--app/src/main/java/mygram/droid/Api.java49
1 files changed, 36 insertions, 13 deletions
diff --git a/app/src/main/java/mygram/droid/Api.java b/app/src/main/java/mygram/droid/Api.java
index 7cce5b6..f603b1b 100644
--- a/app/src/main/java/mygram/droid/Api.java
+++ b/app/src/main/java/mygram/droid/Api.java
@@ -165,15 +165,42 @@ final class Api {
return out;
}
- /** Every post of a folder, in server order. */
- static List<Post> folderPosts(String slug) throws IOException, JSONException {
- List<Post> all = new ArrayList<>();
- for (int page = 1;; page++) {
- JSONObject o = call("GET", "/folders/" + Uri.encode(slug) + "/images?limit=" + PAGE + "&page=" + page);
- all.addAll(Post.list(o, Prefs.server()));
- if (!o.optBoolean("hasMore"))
- return all;
- }
+ /** Pager over a folder: each call returns the next page, shuffled; empty once all pages are used. */
+ // ponytail: random page order plus in-page shuffle, not a full shuffle; neighbours within a page stay together; upgrade: fetch all ids once if that ever matters.
+ static Callable<List<Post>> folderPages(String slug, int count) {
+ List<Integer> pages = new ArrayList<>();
+ for (int p = 1; p <= (count + PAGE - 1) / PAGE; p++)
+ pages.add(p);
+ Collections.shuffle(pages);
+ String path = "/folders/" + Uri.encode(slug) + "/images?limit=" + PAGE + "&page=";
+ int[] next = {0};
+ return () -> {
+ synchronized (next) {
+ while (next[0] < pages.size()) { // imageCount counts carousel slides, so later pages may be empty
+ List<Post> posts = Post.list(call("GET", path + pages.get(next[0]++)), Prefs.server());
+ if (!posts.isEmpty()) {
+ Collections.shuffle(posts);
+ return posts;
+ }
+ }
+ return new ArrayList<>();
+ }
+ };
+ }
+
+ /** Pager over random Reels with a fixed seed; empty once the server reports no more. */
+ static Callable<List<Post>> reelsPages(int seed) {
+ int[] state = {0, 1}; // page, hasMore
+ return () -> {
+ synchronized (state) {
+ if (state[1] == 0)
+ return new ArrayList<>();
+ JSONObject o = call("GET", "/reels?mode=random&limit=12&seed=" + seed + "&page=" + ++state[0]);
+ if (!o.optBoolean("hasMore"))
+ state[1] = 0;
+ return Post.list(o, Prefs.server());
+ }
+ };
}
/** Up to n random posts of a folder. */
@@ -193,10 +220,6 @@ final class Api {
return Post.list(call("GET", "/feed?mode=random&limit=" + PAGE), Prefs.server());
}
- static JSONObject reels(int seed, int page) throws IOException, JSONException {
- return call("GET", "/reels?mode=random&limit=12&seed=" + seed + "&page=" + page);
- }
-
/** Liked posts, newest like first. */
static List<Post> likes() throws IOException, JSONException {
return Post.list(call("GET", "/likes"), Prefs.server());