From e7b4b8aa8100327d83e99f2f0b3362a318e2c0eb Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Sun, 26 Nov 2023 15:55:08 -0500 Subject: Show cations of preview images and gifs. --- .../infinityforreddit/MediaMetadata.java | 3 ++ .../infinityforreddit/comment/ParseComment.java | 2 +- .../markdown/ImageAndGifEntry.java | 34 ++++++++++++++-------- .../infinityforreddit/post/ParsePost.java | 8 ++--- .../infinityforreddit/utils/Utils.java | 22 ++++++++++---- 5 files changed, 47 insertions(+), 22 deletions(-) (limited to 'app/src/main/java/ml/docilealligator/infinityforreddit') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MediaMetadata.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MediaMetadata.java index 3a75e6c1..fd7d2a6a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MediaMetadata.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MediaMetadata.java @@ -12,6 +12,7 @@ public class MediaMetadata implements Parcelable { //E.g. Image public String e; public String fileName; + public String caption; public boolean isGIF; public MediaItem original; public MediaItem downscaled; @@ -30,6 +31,7 @@ public class MediaMetadata implements Parcelable { id = in.readString(); e = in.readString(); fileName = in.readString(); + caption = in.readString(); isGIF = in.readByte() != 0; original = in.readParcelable(MediaItem.class.getClassLoader()); downscaled = in.readParcelable(MediaItem.class.getClassLoader()); @@ -57,6 +59,7 @@ public class MediaMetadata implements Parcelable { dest.writeString(id); dest.writeString(e); dest.writeString(fileName); + dest.writeString(caption); dest.writeByte((byte) (isGIF ? 1 : 0)); dest.writeParcelable(original, flags); dest.writeParcelable(downscaled, flags); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java index 6b162fe6..8a80f1a4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java @@ -270,7 +270,7 @@ public class ParseComment { Map mediaMetadataMap = JSONUtils.parseMediaMetadata(singleCommentData); String commentMarkdown = ""; if (!singleCommentData.isNull(JSONUtils.BODY_KEY)) { - commentMarkdown = Utils.parseInlineRedditImages( + commentMarkdown = Utils.parseRedditImagesBlock( Utils.modifyMarkdown( Utils.trimTrailingWhitespace(singleCommentData.getString(JSONUtils.BODY_KEY))), mediaMetadataMap); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifEntry.java b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifEntry.java index 86287cc0..9611a5dc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifEntry.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifEntry.java @@ -41,6 +41,8 @@ public class ImageAndGifEntry extends MarkwonAdapter.Entry() { @Override @@ -172,17 +193,6 @@ public class ImageAndGifEntry extends MarkwonAdapter.Entry mediaMetadataMap) { + public static String parseRedditImagesBlock(String markdown, @Nullable Map mediaMetadataMap) { if (mediaMetadataMap == null) { return markdown; } @@ -99,27 +99,33 @@ public final class Utils { int previewReddItLength = "https://preview.redd.it/".length(); while (matcher.find(start)) { String id; + String caption = null; if (markdownStringBuilder.charAt(matcher.start()) == '[') { //Has caption int urlStartIndex = markdownStringBuilder.lastIndexOf("https://preview.redd.it/", matcher.end()); id = markdownStringBuilder.substring(previewReddItLength + urlStartIndex, markdownStringBuilder.indexOf(".", previewReddItLength + urlStartIndex)); + //Minus "](".length() + caption = markdownStringBuilder.substring(matcher.start() + 1, urlStartIndex - 2); } else { id = markdownStringBuilder.substring(matcher.start() + previewReddItLength, markdownStringBuilder.indexOf(".", matcher.start() + previewReddItLength)); } - if (!mediaMetadataMap.containsKey(id)) { + MediaMetadata mediaMetadata = mediaMetadataMap.get(id); + if (mediaMetadata == null) { start = matcher.end(); continue; } + mediaMetadata.caption = caption; + if (markdownStringBuilder.charAt(matcher.start()) == '[') { //Has caption markdownStringBuilder.insert(matcher.start(), '!'); start = matcher.end() + 1; } else { - String replacingText = "![img](" + markdownStringBuilder.substring(matcher.start(), matcher.end()) + ")"; + String replacingText = "![](" + markdownStringBuilder.substring(matcher.start(), matcher.end()) + ")"; markdownStringBuilder.replace(matcher.start(), matcher.end(), replacingText); start = replacingText.length() + matcher.start(); } @@ -133,26 +139,32 @@ public final class Utils { int iReddItLength = "https://i.redd.it/".length(); while (matcher.find(start)) { String id; + String caption = null; if (markdownStringBuilder.charAt(matcher.start()) == '[') { //Has caption int urlStartIndex = markdownStringBuilder.lastIndexOf("https://i.redd.it/", matcher.end()); id = markdownStringBuilder.substring(iReddItLength + urlStartIndex, markdownStringBuilder.indexOf(".", iReddItLength + urlStartIndex)); + //Minus "](".length() + caption = markdownStringBuilder.substring(matcher.start() + 1, urlStartIndex - 2); } else { id = markdownStringBuilder.substring(matcher.start() + iReddItLength, matcher.start() + markdownStringBuilder.indexOf(".", iReddItLength)); } - if (!mediaMetadataMap.containsKey(id)) { + MediaMetadata mediaMetadata = mediaMetadataMap.get(id); + if (mediaMetadata == null) { start = matcher.end(); continue; } + mediaMetadata.caption = caption; + if (markdownStringBuilder.charAt(matcher.start()) == '[') { //Has caption markdownStringBuilder.insert(matcher.start(), '!'); start = matcher.end() + 1; } else { - String replacingText = "![img](" + markdownStringBuilder.substring(matcher.start(), matcher.end()) + ")"; + String replacingText = "![](" + markdownStringBuilder.substring(matcher.start(), matcher.end()) + ")"; markdownStringBuilder.replace(matcher.start(), matcher.end(), replacingText); start = replacingText.length() + matcher.start(); } -- cgit v1.2.3