diff options
| author | Danilo M. <danix@danix.xyz> | 2026-10-22 11:09:59 +0200 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-10-22 11:09:59 +0200 |
| commit | 324b600f5795735c74eae81a27beb4953f40653e (patch) | |
| tree | 6de17d5bf33d1acfdbb09879c3c9c24e2474421d /app/src/main/java/mygram/droid/Post.java | |
| parent | 3605f4e081283b2f32451c7075fe07648e76e99d (diff) | |
| download | mygram-droid-324b600f5795735c74eae81a27beb4953f40653e.tar.gz mygram-droid-324b600f5795735c74eae81a27beb4953f40653e.zip | |
Add post parsing, URL helpers, health check and double-OK logic with tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'app/src/main/java/mygram/droid/Post.java')
| -rw-r--r-- | app/src/main/java/mygram/droid/Post.java | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/app/src/main/java/mygram/droid/Post.java b/app/src/main/java/mygram/droid/Post.java new file mode 100644 index 0000000..b4211cf --- /dev/null +++ b/app/src/main/java/mygram/droid/Post.java @@ -0,0 +1,73 @@ +/* + * mygram-droid: Android TV client for Foldergram. + * Copyright (C) 2026 Danilo M. <danix@danix.xyz> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + + +package mygram.droid; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +/** One Foldergram post: a photo, a video, or a carousel of slides. */ +final class Post { + /** One slide. A single post has exactly one. */ + static final class Media { + final boolean video; + final int width, height; + final long durationMs; + final String thumbnailUrl, previewUrl; + + Media(JSONObject o, String base) { + video = "video".equals(o.optString("mediaType")); + width = o.optInt("width"); + height = o.optInt("height"); + durationMs = o.optLong("durationMs"); + thumbnailUrl = Api.resolve(base, str(o, "thumbnailUrl")); + previewUrl = Api.resolve(base, str(o, "previewUrl")); + } + } + + final int id; + final String folderSlug, folderName; + final List<Media> media = new ArrayList<>(); + + Post(JSONObject o, String base) { + id = o.optInt("id"); + folderSlug = o.optString("folderSlug"); + folderName = o.optString("folderName"); + JSONArray items = o.optJSONArray("mediaItems"); + if (items != null) + for (int i = 0; i < items.length(); i++) + media.add(new Media(items.optJSONObject(i), base)); + if (media.isEmpty()) + media.add(new Media(o, base)); + } + + /** The posts in a paged response's "items". */ + static List<Post> list(JSONObject page, String base) { + List<Post> out = new ArrayList<>(); + JSONArray items = page.optJSONArray("items"); + if (items != null) + for (int i = 0; i < items.length(); i++) + out.add(new Post(items.optJSONObject(i), base)); + return out; + } + + /** optString turns JSON null into "null"; this returns null. */ + static String str(JSONObject o, String key) { + return o.isNull(key) ? null : o.optString(key); + } +} |
