diff options
Diffstat (limited to 'app/src/main/java/mygram/droid/Api.java')
| -rw-r--r-- | app/src/main/java/mygram/droid/Api.java | 49 |
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()); |
