diff options
Diffstat (limited to 'app/src/main/java/mygram/droid/Api.java')
| -rw-r--r-- | app/src/main/java/mygram/droid/Api.java | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/app/src/main/java/mygram/droid/Api.java b/app/src/main/java/mygram/droid/Api.java index c7d08eb..6e4ce74 100644 --- a/app/src/main/java/mygram/droid/Api.java +++ b/app/src/main/java/mygram/droid/Api.java @@ -172,25 +172,36 @@ final class Api { return out; } - /** 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. + /** Folder orders: RANDOM shuffles, DATE keeps the server's order, NAME sorts by file name. */ + static final int RANDOM = 0, DATE = 1, NAME = 2; + static Callable<List<Post>> folderPages(String slug, int count) { + return folderPages(slug, count, RANDOM); + } + + /** Pager over a folder: each call returns the next page; 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. + // ponytail: NAME fetches every page in its first call, since the server cannot sort by name; slow on folders of thousands. + // DATE is the server's order: newest first unless the instance's default folder order says oldest. + static Callable<List<Post>> folderPages(String slug, int count, int order) { List<Integer> pages = new ArrayList<>(); for (int p = 1; p <= (count + PAGE - 1) / PAGE; p++) pages.add(p); - Collections.shuffle(pages); + if (order == RANDOM) + 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<>(); + List<Post> out = new ArrayList<>(); + // imageCount counts carousel slides, so later pages may be empty + while (next[0] < pages.size() && (order == NAME || out.isEmpty())) + out.addAll(Post.list(call("GET", path + pages.get(next[0]++)), Prefs.server())); + if (order == RANDOM) + Collections.shuffle(out); + else if (order == NAME) + out.sort((a, b) -> String.CASE_INSENSITIVE_ORDER.compare(a.filename, b.filename)); + return out; } }; } |
