From 178f6927dbef5a8e69452d5e3a3422ed84c092d3 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Fri, 17 Nov 2023 17:20:34 -0500 Subject: Continue implementing showing images from preview.redd.it in posts. --- .../activities/CommentActivity.java | 4 +- .../activities/FullMarkdownActivity.java | 4 +- .../infinityforreddit/activities/WikiActivity.java | 5 +- .../CommentsListingRecyclerViewAdapter.java | 5 +- .../adapters/CommentsRecyclerViewAdapter.java | 5 +- .../adapters/PostDetailRecyclerViewAdapter.java | 10 +- .../adapters/RulesRecyclerViewAdapter.java | 5 +- .../fragments/SidebarFragment.java | 4 +- .../markdown/ImageAndGifBlock.java | 7 + .../markdown/ImageAndGifBlockParser.java | 20 +- .../markdown/ImageAndGifEntry.java | 2 +- .../markdown/ImageAndGifPlugin.java | 18 +- .../infinityforreddit/markdown/MarkdownUtils.java | 3 +- .../infinityforreddit/post/ParsePost.java | 62 +++++- .../infinityforreddit/post/Post.java | 233 +++++++++++++++------ .../infinityforreddit/utils/JSONUtils.java | 1 + .../infinityforreddit/utils/Utils.java | 7 +- 17 files changed, 301 insertions(+), 94 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java index 2a03a058..e62f8773 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java @@ -64,6 +64,7 @@ import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed; import ml.docilealligator.infinityforreddit.databinding.ActivityCommentBinding; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; +import ml.docilealligator.infinityforreddit.markdown.ImageAndGifPlugin; import ml.docilealligator.infinityforreddit.markdown.MarkdownUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -207,8 +208,9 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA builder.linkColor(linkColor); } }; + ImageAndGifPlugin imageAndGifPlugin = new ImageAndGifPlugin(); Markwon postBodyMarkwon = MarkdownUtils.createFullRedditMarkwon(this, - miscPlugin, parentTextColor, parentSpoilerBackgroundColor, null); + miscPlugin, imageAndGifPlugin, parentTextColor, parentSpoilerBackgroundColor, null); MarkwonAdapter markwonAdapter = MarkdownUtils.createTablesAdapter(this, mGlide); binding.commentContentMarkdownView.setLayoutManager(new LinearLayoutManagerBugFixed(this)); binding.commentContentMarkdownView.setAdapter(markwonAdapter); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java index aac1cc82..d92ecda7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java @@ -44,6 +44,7 @@ import ml.docilealligator.infinityforreddit.customviews.SwipeLockInterface; import ml.docilealligator.infinityforreddit.customviews.SwipeLockLinearLayoutManager; import ml.docilealligator.infinityforreddit.customviews.slidr.Slidr; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; +import ml.docilealligator.infinityforreddit.markdown.ImageAndGifPlugin; import ml.docilealligator.infinityforreddit.markdown.MarkdownUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -139,8 +140,9 @@ public class FullMarkdownActivity extends BaseActivity { builder.linkColor(linkColor); } }; + ImageAndGifPlugin imageAndGifPlugin = new ImageAndGifPlugin(); Markwon markwon = MarkdownUtils.createFullRedditMarkwon(this, - miscPlugin, markdownColor, spoilerBackgroundColor, null); + miscPlugin, imageAndGifPlugin, markdownColor, spoilerBackgroundColor, null); MarkwonAdapter markwonAdapter = MarkdownUtils.createTablesAdapter(this, Glide.with(this)); LinearLayoutManagerBugFixed linearLayoutManager = new SwipeLockLinearLayoutManager(this, new SwipeLockInterface() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java index 78b957d3..56bef8fb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java @@ -52,6 +52,7 @@ import ml.docilealligator.infinityforreddit.customviews.SwipeLockInterface; import ml.docilealligator.infinityforreddit.customviews.SwipeLockLinearLayoutManager; import ml.docilealligator.infinityforreddit.customviews.slidr.Slidr; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; +import ml.docilealligator.infinityforreddit.markdown.ImageAndGifPlugin; import ml.docilealligator.infinityforreddit.markdown.MarkdownUtils; import ml.docilealligator.infinityforreddit.utils.JSONUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -95,6 +96,7 @@ public class WikiActivity extends BaseActivity { @Inject CustomThemeWrapper mCustomThemeWrapper; private String wikiMarkdown; + private ImageAndGifPlugin imageAndGifPlugin; private Markwon markwon; private MarkwonAdapter markwonAdapter; private boolean isRefreshing = false; @@ -175,8 +177,9 @@ public class WikiActivity extends BaseActivity { urlMenuBottomSheetFragment.show(getSupportFragmentManager(), null); return true; }; + imageAndGifPlugin = new ImageAndGifPlugin(); markwon = MarkdownUtils.createFullRedditMarkwon(this, - miscPlugin, markdownColor, spoilerBackgroundColor, onLinkLongClickListener); + miscPlugin, imageAndGifPlugin, markdownColor, spoilerBackgroundColor, onLinkLongClickListener); markwonAdapter = MarkdownUtils.createTablesAdapter(this, mGlide); LinearLayoutManagerBugFixed linearLayoutManager = new SwipeLockLinearLayoutManager(this, new SwipeLockInterface() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java index a3b5b5db..76c0cc64 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java @@ -55,6 +55,7 @@ import ml.docilealligator.infinityforreddit.customviews.SpoilerOnClickTextView; import ml.docilealligator.infinityforreddit.customviews.SwipeLockInterface; import ml.docilealligator.infinityforreddit.customviews.SwipeLockLinearLayoutManager; import ml.docilealligator.infinityforreddit.databinding.ItemCommentBinding; +import ml.docilealligator.infinityforreddit.markdown.ImageAndGifPlugin; import ml.docilealligator.infinityforreddit.markdown.MarkdownUtils; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -79,6 +80,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter previews) { + private Post.Preview getSuitablePreview(List previews) { Post.Preview preview; if (!previews.isEmpty()) { int previewIndex; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java index 9218dd34..42f114fd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java @@ -34,10 +34,12 @@ import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.SwipeLockInterface; import ml.docilealligator.infinityforreddit.customviews.SwipeLockLinearLayoutManager; import ml.docilealligator.infinityforreddit.customviews.slidr.widget.SliderPanel; +import ml.docilealligator.infinityforreddit.markdown.ImageAndGifPlugin; import ml.docilealligator.infinityforreddit.markdown.MarkdownUtils; public class RulesRecyclerViewAdapter extends RecyclerView.Adapter { private BaseActivity activity; + private ImageAndGifPlugin imageAndGifPlugin; private Markwon markwon; @Nullable private final SliderPanel sliderPanel; @@ -83,8 +85,9 @@ public class RulesRecyclerViewAdapter extends RecyclerView.Adapter mediaMetadataMap; + private int fromIndex = "![img](https://preview.redd.it/".length(); @Override public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) { @@ -41,10 +46,19 @@ public class ImageAndGifBlockParser extends AbstractBlockParser { Matcher matcher = redditPreviewPattern.matcher(line); if (matcher.find()) { if (matcher.end() == line.length()) { - return BlockStart.of(new ImageAndGifBlockParser()); + int endIndex = line.indexOf('.', fromIndex); + if (endIndex > 0) { + String id = line.substring(fromIndex, endIndex); + Log.i("asdfasdf", "s " + id); + return mediaMetadataMap.containsKey(id) ? BlockStart.of(new ImageAndGifBlockParser(mediaMetadataMap.get(id))) : BlockStart.none(); + } } } return BlockStart.none(); } + + public void setMediaMetadataMap(Map mediaMetadataMap) { + this.mediaMetadataMap = 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 e09388e4..1e5678e1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifEntry.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifEntry.java @@ -41,7 +41,7 @@ public class ImageAndGifEntry extends MarkwonAdapter.Entry imageRequestBuilder = glide.load("https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/1280px-Image_created_with_a_mobile_phone.png").listener(holder.requestListener); + RequestBuilder imageRequestBuilder = glide.load(node.mediaMetadata.original.url).listener(holder.requestListener); boolean blurImage = false; if (blurImage) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifPlugin.java b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifPlugin.java index 04e95b23..86209454 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifPlugin.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifPlugin.java @@ -6,18 +6,32 @@ import androidx.annotation.NonNull; import org.commonmark.parser.Parser; +import java.util.Map; + import io.noties.markwon.AbstractMarkwonPlugin; +import ml.docilealligator.infinityforreddit.post.Post; public class ImageAndGifPlugin extends AbstractMarkwonPlugin { + + private ImageAndGifBlockParser.Factory factory; + + public ImageAndGifPlugin() { + this.factory = new ImageAndGifBlockParser.Factory(); + } + @NonNull @Override public String processMarkdown(@NonNull String markdown) { - Log.i("asdfa", "imageandgifplugin " + markdown + "fuck"); + Log.i("asdfa", "imageandgifplugin " + markdown + "ooooo"); return super.processMarkdown(markdown); } @Override public void configureParser(@NonNull Parser.Builder builder) { - builder.customBlockParserFactory(new ImageAndGifBlockParser.Factory()); + builder.customBlockParserFactory(factory); + } + + public void setMediaMetadataMap(Map mediaMetadataMap) { + factory.setMediaMetadataMap(mediaMetadataMap); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/MarkdownUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/MarkdownUtils.java index aaaef7c8..d3d4387d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/MarkdownUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/MarkdownUtils.java @@ -34,6 +34,7 @@ public class MarkdownUtils { @NonNull public static Markwon createFullRedditMarkwon(@NonNull Context context, @NonNull MarkwonPlugin miscPlugin, + @NonNull ImageAndGifPlugin imageAndGifPlugin, int markdownColor, int spoilerBackgroundColor, @Nullable BetterLinkMovementMethod.OnLinkLongClickListener onLinkLongClickListener) { @@ -50,7 +51,7 @@ public class MarkdownUtils { .usePlugin(MovementMethodPlugin.create(new SpoilerAwareMovementMethod() .setOnLinkLongClickListener(onLinkLongClickListener))) .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) - .usePlugin(new ImageAndGifPlugin()) + .usePlugin(imageAndGifPlugin) .usePlugin(TableEntryPlugin.create(context)) .build(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java index 14fc4187..08fd7023 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java @@ -5,14 +5,19 @@ import android.os.Handler; import android.text.Html; import android.text.TextUtils; +import androidx.annotation.Nullable; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.concurrent.Executor; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -226,6 +231,8 @@ public class ParsePost { previews.add(new Post.Preview(thumbnailPreviewUrl, thumbnailPreviewWidth, thumbnailPreviewHeight, "", "")); } } + + Map mediaMetadataMap = parseMediaMetadata(data); if (data.has(JSONUtils.CROSSPOST_PARENT_LIST)) { //Cross post //data.getJSONArray(JSONUtils.CROSSPOST_PARENT_LIST).getJSONObject(0) out of bounds???????????? @@ -233,7 +240,7 @@ public class ParsePost { Post crosspostParent = parseBasicData(data); Post post = parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed, author, authorFlair, authorFlairHTMLBuilder.toString(), - postTime, title, previews, + postTime, title, previews, mediaMetadataMap, score, voteType, nComments, upvoteRatio, flair, awardingsBuilder.toString(), nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, deleted, removed, true, distinguished, suggestedSort); @@ -242,21 +249,61 @@ public class ParsePost { } else { return parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed, author, authorFlair, authorFlairHTMLBuilder.toString(), - postTime, title, previews, + postTime, title, previews, mediaMetadataMap, score, voteType, nComments, upvoteRatio, flair, awardingsBuilder.toString(), nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, deleted, removed, false, distinguished, suggestedSort); } } + @Nullable + private static Map parseMediaMetadata(JSONObject data) { + try { + if (data.has(JSONUtils.MEDIA_METADATA_KEY)) { + Map mediaMetadataMap = new HashMap<>(); + JSONObject mediaMetadataJSON = data.getJSONObject(JSONUtils.MEDIA_METADATA_KEY); + for (Iterator it = mediaMetadataJSON.keys(); it.hasNext();) { + try { + String k = it.next(); + JSONObject media = mediaMetadataJSON.getJSONObject(k); + JSONArray downscales = media.getJSONArray(JSONUtils.P_KEY); + JSONObject downscaledItemJSON; + if (downscales.length() <= 3) { + downscaledItemJSON = downscales.getJSONObject(downscales.length() - 1); + + } else { + downscaledItemJSON = downscales.getJSONObject(3); + } + Post.MediaMetadata.MediaItem downscaledItem = new Post.MediaMetadata.MediaItem(downscaledItemJSON.getInt(JSONUtils.X_KEY), + downscaledItemJSON.getInt(JSONUtils.Y_KEY), downscaledItemJSON.getString(JSONUtils.U_KEY)); + JSONObject originalItemJSON = media.getJSONObject(JSONUtils.S_KEY); + Post.MediaMetadata.MediaItem originalItem = new Post.MediaMetadata.MediaItem(originalItemJSON.getInt(JSONUtils.X_KEY), + originalItemJSON.getInt(JSONUtils.Y_KEY), originalItemJSON.getString(JSONUtils.U_KEY)); + + String id = media.getString(JSONUtils.ID_KEY); + mediaMetadataMap.put(id, new Post.MediaMetadata(id, media.getString(JSONUtils.E_KEY), + originalItem, downscaledItem)); + } catch (JSONException e) { + e.printStackTrace(); + } + } + return mediaMetadataMap; + } + } catch (JSONException e) { + e.printStackTrace(); + } + + return null; + } + private static Post parseData(JSONObject data, String permalink, String id, String fullName, String subredditName, String subredditNamePrefixed, String author, - String authorFlair, String authorFlairHTML, - long postTimeMillis, String title, ArrayList previews, + String authorFlair, String authorFlairHTML, long postTimeMillis, String title, + ArrayList previews, Map mediaMetadataMap, int score, int voteType, int nComments, int upvoteRatio, String flair, - String awards, int nAwards, boolean hidden, boolean spoiler, - boolean nsfw, boolean stickied, boolean archived, boolean locked, - boolean saved, boolean deleted, boolean removed, boolean isCrosspost, + String awards, int nAwards, boolean hidden, boolean spoiler, boolean nsfw, + boolean stickied, boolean archived, boolean locked, boolean saved, + boolean deleted, boolean removed, boolean isCrosspost, String distinguished, String suggestedSort) throws JSONException { Post post; @@ -750,6 +797,7 @@ public class ParsePost { } } + post.setMediaMetadataMap(mediaMetadataMap); return post; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java index 137c9fdc..e91062b2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java @@ -3,9 +3,11 @@ package ml.docilealligator.infinityforreddit.post; import android.os.Parcel; import android.os.Parcelable; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import java.util.ArrayList; +import java.util.Map; import ml.docilealligator.infinityforreddit.utils.APIUtils; @@ -22,17 +24,7 @@ public class Post implements Parcelable { public static final int GIF_TYPE = 4; public static final int NO_PREVIEW_LINK_TYPE = 5; public static final int GALLERY_TYPE = 6; - public static final Creator CREATOR = new Creator() { - @Override - public Post createFromParcel(Parcel in) { - return new Post(in); - } - @Override - public Post[] newArray(int size) { - return new Post[size]; - } - }; private String id; private String fullName; private String subredditName; @@ -78,6 +70,8 @@ public class Post implements Parcelable { private String distinguished; private String suggestedSort; private ArrayList previews = new ArrayList<>(); + @Nullable + private Map mediaMetadataMap; private ArrayList gallery = new ArrayList<>(); //Text and video posts @@ -162,10 +156,9 @@ public class Post implements Parcelable { subredditIconUrl = in.readString(); author = in.readString(); authorNamePrefixed = in.readString(); + authorIconUrl = in.readString(); authorFlair = in.readString(); authorFlairHTML = in.readString(); - authorIconUrl = in.readString(); - postTimeMillis = in.readLong(); title = in.readString(); selfText = in.readString(); selfTextPlain = in.readString(); @@ -182,6 +175,7 @@ public class Post implements Parcelable { loadGfyOrStreamableVideoSuccess = in.readByte() != 0; permalink = in.readString(); flair = in.readString(); + postTimeMillis = in.readLong(); score = in.readInt(); postType = in.readInt(); voteType = in.readInt(); @@ -199,10 +193,23 @@ public class Post implements Parcelable { crosspostParentId = in.readString(); distinguished = in.readString(); suggestedSort = in.readString(); - in.readTypedList(previews, Preview.CREATOR); - in.readTypedList(gallery, Gallery.CREATOR); + previews = in.createTypedArrayList(Preview.CREATOR); + mediaMetadataMap = (Map) in.readValue(getClass().getClassLoader()); + gallery = in.createTypedArrayList(Gallery.CREATOR); } + public static final Creator CREATOR = new Creator() { + @Override + public Post createFromParcel(Parcel in) { + return new Post(in); + } + + @Override + public Post[] newArray(int size) { + return new Post[size]; + } + }; + public String getId() { return id; } @@ -469,6 +476,57 @@ public class Post implements Parcelable { return 0; } + @Override + public void writeToParcel(@NonNull Parcel dest, int flags) { + dest.writeString(id); + dest.writeString(fullName); + dest.writeString(subredditName); + dest.writeString(subredditNamePrefixed); + dest.writeString(subredditIconUrl); + dest.writeString(author); + dest.writeString(authorNamePrefixed); + dest.writeString(authorIconUrl); + dest.writeString(authorFlair); + dest.writeString(authorFlairHTML); + dest.writeString(title); + dest.writeString(selfText); + dest.writeString(selfTextPlain); + dest.writeString(selfTextPlainTrimmed); + dest.writeString(url); + dest.writeString(videoUrl); + dest.writeString(videoDownloadUrl); + dest.writeString(gfycatId); + dest.writeString(streamableShortCode); + dest.writeByte((byte) (isImgur ? 1 : 0)); + dest.writeByte((byte) (isGfycat ? 1 : 0)); + dest.writeByte((byte) (isRedgifs ? 1 : 0)); + dest.writeByte((byte) (isStreamable ? 1 : 0)); + dest.writeByte((byte) (loadGfyOrStreamableVideoSuccess ? 1 : 0)); + dest.writeString(permalink); + dest.writeString(flair); + dest.writeLong(postTimeMillis); + dest.writeInt(score); + dest.writeInt(postType); + dest.writeInt(voteType); + dest.writeInt(nComments); + dest.writeInt(upvoteRatio); + dest.writeByte((byte) (hidden ? 1 : 0)); + dest.writeByte((byte) (spoiler ? 1 : 0)); + dest.writeByte((byte) (nsfw ? 1 : 0)); + dest.writeByte((byte) (stickied ? 1 : 0)); + dest.writeByte((byte) (archived ? 1 : 0)); + dest.writeByte((byte) (locked ? 1 : 0)); + dest.writeByte((byte) (saved ? 1 : 0)); + dest.writeByte((byte) (isCrosspost ? 1 : 0)); + dest.writeByte((byte) (isRead ? 1 : 0)); + dest.writeString(crosspostParentId); + dest.writeString(distinguished); + dest.writeString(suggestedSort); + dest.writeTypedList(previews); + dest.writeValue(mediaMetadataMap); + dest.writeTypedList(gallery); + } + public boolean isStickied() { return stickied; } @@ -517,6 +575,14 @@ public class Post implements Parcelable { this.previews = previews; } + public Map getMediaMetadataMap() { + return mediaMetadataMap; + } + + public void setMediaMetadataMap(Map mediaMetadataMap) { + this.mediaMetadataMap = mediaMetadataMap; + } + public ArrayList getGallery() { return gallery; } @@ -525,56 +591,6 @@ public class Post implements Parcelable { this.gallery = gallery; } - @Override - public void writeToParcel(Parcel parcel, int i) { - parcel.writeString(id); - parcel.writeString(fullName); - parcel.writeString(subredditName); - parcel.writeString(subredditNamePrefixed); - parcel.writeString(subredditIconUrl); - parcel.writeString(author); - parcel.writeString(authorNamePrefixed); - parcel.writeString(authorFlair); - parcel.writeString(authorFlairHTML); - parcel.writeString(authorIconUrl); - parcel.writeLong(postTimeMillis); - parcel.writeString(title); - parcel.writeString(selfText); - parcel.writeString(selfTextPlain); - parcel.writeString(selfTextPlainTrimmed); - parcel.writeString(url); - parcel.writeString(videoUrl); - parcel.writeString(videoDownloadUrl); - parcel.writeString(gfycatId); - parcel.writeString(streamableShortCode); - parcel.writeByte((byte) (isImgur ? 1 : 0)); - parcel.writeByte((byte) (isGfycat ? 1 : 0)); - parcel.writeByte((byte) (isRedgifs ? 1 : 0)); - parcel.writeByte((byte) (isStreamable ? 1 : 0)); - parcel.writeByte((byte) (loadGfyOrStreamableVideoSuccess ? 1 : 0)); - parcel.writeString(permalink); - parcel.writeString(flair); - parcel.writeInt(score); - parcel.writeInt(postType); - parcel.writeInt(voteType); - parcel.writeInt(nComments); - parcel.writeInt(upvoteRatio); - parcel.writeByte((byte) (hidden ? 1 : 0)); - parcel.writeByte((byte) (spoiler ? 1 : 0)); - parcel.writeByte((byte) (nsfw ? 1 : 0)); - parcel.writeByte((byte) (stickied ? 1 : 0)); - parcel.writeByte((byte) (archived ? 1 : 0)); - parcel.writeByte((byte) (locked ? 1 : 0)); - parcel.writeByte((byte) (saved ? 1 : 0)); - parcel.writeByte((byte) (isCrosspost ? 1 : 0)); - parcel.writeByte((byte) (isRead ? 1 : 0)); - parcel.writeString(crosspostParentId); - parcel.writeString(distinguished); - parcel.writeString(suggestedSort); - parcel.writeTypedList(previews); - parcel.writeTypedList(gallery); - } - @Override public boolean equals(@Nullable Object obj) { if (!(obj instanceof Post)) { @@ -750,4 +766,93 @@ public class Post implements Parcelable { parcel.writeString(previewCaptionUrl); } } + + public static class MediaMetadata implements Parcelable { + public String id; + //E.g. Image + public String e; + public MediaItem original; + public MediaItem downscaled; + + public MediaMetadata(String id, String e, MediaItem original, MediaItem downscaled) { + this.id = id; + this.e = e; + this.original = original; + this.downscaled = downscaled; + } + + protected MediaMetadata(Parcel in) { + id = in.readString(); + e = in.readString(); + original = in.readParcelable(MediaItem.class.getClassLoader()); + downscaled = in.readParcelable(MediaItem.class.getClassLoader()); + } + + public static final Creator CREATOR = new Creator() { + @Override + public MediaMetadata createFromParcel(Parcel in) { + return new MediaMetadata(in); + } + + @Override + public MediaMetadata[] newArray(int size) { + return new MediaMetadata[size]; + } + }; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(@NonNull Parcel dest, int flags) { + dest.writeString(id); + dest.writeString(e); + dest.writeParcelable(original, flags); + dest.writeParcelable(downscaled, flags); + } + + public static class MediaItem implements Parcelable { + public int x; + public int y; + public String url; + + public MediaItem(int x, int y, String url) { + this.x = x; + this.y = y; + this.url = url; + } + + protected MediaItem(Parcel in) { + x = in.readInt(); + y = in.readInt(); + url = in.readString(); + } + + public static final Creator CREATOR = new Creator() { + @Override + public MediaItem createFromParcel(Parcel in) { + return new MediaItem(in); + } + + @Override + public MediaItem[] newArray(int size) { + return new MediaItem[size]; + } + }; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(@NonNull Parcel dest, int flags) { + dest.writeInt(x); + dest.writeInt(y); + dest.writeString(url); + } + } + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java index 7bd43af2..c43608be 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java @@ -182,4 +182,5 @@ public class JSONUtils { public static final String URLS_KEY = "urls"; public static final String HD_KEY = "hd"; public static final String SUGGESTED_SORT_KEY = "suggested_sort"; + public static final String P_KEY = "p"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java index 9ddd9643..8504b621 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java @@ -19,7 +19,6 @@ import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.style.TypefaceSpan; import android.util.DisplayMetrics; -import android.util.Log; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; @@ -156,14 +155,10 @@ public final class Utils { Matcher matcher = inlineRedditImagePattern.matcher(markdownStringBuilder); int start = 0; while (matcher.find(start)) { - int fileNameStartIndex = "https://preview.redd.it/".length(); - String replacingText = "![" + /*markdownStringBuilder.substring(fileNameStartIndex, - markdownStringBuilder.indexOf(".", fileNameStartIndex))*/"img" - + "](" + markdownStringBuilder.substring(matcher.start(), matcher.end()) + ")"; + String replacingText = "![img](" + markdownStringBuilder.substring(matcher.start(), matcher.end()) + ")"; markdownStringBuilder.replace(matcher.start(), matcher.end(), replacingText); start = replacingText.length() + matcher.start(); matcher = inlineRedditImagePattern.matcher(markdownStringBuilder); - Log.i("asdf", "S " + start + markdownStringBuilder.length()); } return markdownStringBuilder.toString(); -- cgit v1.2.3