diff options
Diffstat (limited to 'app/src/main')
17 files changed, 301 insertions, 94 deletions
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<Comment private BaseActivity mActivity; private Retrofit mOauthRetrofit; private Locale mLocale; + private ImageAndGifPlugin mImageAndGifPlugin; private Markwon mMarkwon; private RecyclerView.RecycledViewPool recycledViewPool; private String mAccessToken; @@ -168,8 +170,9 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment } return true; }; + mImageAndGifPlugin = new ImageAndGifPlugin(); mMarkwon = MarkdownUtils.createFullRedditMarkwon(mActivity, - miscPlugin, mCommentColor, commentSpoilerBackgroundColor, onLinkLongClickListener); + miscPlugin, mImageAndGifPlugin, mCommentColor, commentSpoilerBackgroundColor, onLinkLongClickListener); recycledViewPool = new RecyclerView.RecycledViewPool(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java index 298214d3..85ba9dae 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java @@ -67,6 +67,7 @@ import ml.docilealligator.infinityforreddit.customviews.SwipeLockInterface; import ml.docilealligator.infinityforreddit.customviews.SwipeLockLinearLayoutManager; import ml.docilealligator.infinityforreddit.databinding.ItemCommentBinding; import ml.docilealligator.infinityforreddit.fragments.ViewPostDetailFragment; +import ml.docilealligator.infinityforreddit.markdown.ImageAndGifPlugin; import ml.docilealligator.infinityforreddit.markdown.MarkdownUtils; import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.utils.APIUtils; @@ -93,6 +94,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi private Executor mExecutor; private Retrofit mRetrofit; private Retrofit mOauthRetrofit; + private ImageAndGifPlugin mImageAndGifPlugin; private Markwon mCommentMarkwon; private String mAccessToken; private String mAccountName; @@ -203,8 +205,9 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi } return true; }; + mImageAndGifPlugin = new ImageAndGifPlugin(); mCommentMarkwon = MarkdownUtils.createFullRedditMarkwon(mActivity, - miscPlugin, mCommentTextColor, commentSpoilerBackgroundColor, onLinkLongClickListener); + miscPlugin, mImageAndGifPlugin, mCommentTextColor, commentSpoilerBackgroundColor, onLinkLongClickListener); recycledViewPool = new RecyclerView.RecycledViewPool(); mAccessToken = accessToken; mAccountName = accountName; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java index 0b90112e..30a3068f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -48,7 +48,7 @@ import com.google.android.material.button.MaterialButton; import com.google.common.collect.ImmutableList; import com.libRG.CustomTextView; -import java.util.ArrayList; +import java.util.List; import java.util.Locale; import java.util.concurrent.Executor; @@ -103,6 +103,7 @@ import ml.docilealligator.infinityforreddit.databinding.ItemPostDetailVideoAndGi import ml.docilealligator.infinityforreddit.databinding.ItemPostDetailVideoAutoplayBinding; import ml.docilealligator.infinityforreddit.databinding.ItemPostDetailVideoAutoplayLegacyControllerBinding; import ml.docilealligator.infinityforreddit.fragments.ViewPostDetailFragment; +import ml.docilealligator.infinityforreddit.markdown.ImageAndGifPlugin; import ml.docilealligator.infinityforreddit.markdown.MarkdownUtils; import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.post.PostPagingSource; @@ -142,6 +143,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler private SharedPreferences mCurrentAccountSharedPreferences; private RequestManager mGlide; private SaveMemoryCenterInisdeDownsampleStrategy mSaveMemoryCenterInsideDownsampleStrategy; + private ImageAndGifPlugin mImageAndGifPlugin; private Markwon mPostDetailMarkwon; private final MarkwonAdapter mMarkwonAdapter; private String mAccessToken; @@ -287,8 +289,9 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler } return true; }; + mImageAndGifPlugin = new ImageAndGifPlugin(); mPostDetailMarkwon = MarkdownUtils.createFullRedditMarkwon(mActivity, - miscPlugin, markdownColor, postSpoilerBackgroundColor, onLinkLongClickListener); + miscPlugin, mImageAndGifPlugin, markdownColor, postSpoilerBackgroundColor, onLinkLongClickListener); mMarkwonAdapter = MarkdownUtils.createTablesAdapter(activity, mGlide); mSeparatePostAndComments = separatePostAndComments; @@ -646,6 +649,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler if (mPost.getSelfText() != null && !mPost.getSelfText().equals("")) { ((PostDetailBaseViewHolder) holder).contentMarkdownView.setVisibility(View.VISIBLE); ((PostDetailBaseViewHolder) holder).contentMarkdownView.setAdapter(mMarkwonAdapter); + mImageAndGifPlugin.setMediaMetadataMap(mPost.getMediaMetadataMap()); mMarkwonAdapter.setMarkdown(mPostDetailMarkwon, mPost.getSelfText()); // noinspection NotifyDataSetChanged mMarkwonAdapter.notifyDataSetChanged(); @@ -813,7 +817,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler } @Nullable - private Post.Preview getSuitablePreview(ArrayList<Post.Preview> previews) { + private Post.Preview getSuitablePreview(List<Post.Preview> 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<RulesRecyclerViewAdapter.RuleViewHolder> { private BaseActivity activity; + private ImageAndGifPlugin imageAndGifPlugin; private Markwon markwon; @Nullable private final SliderPanel sliderPanel; @@ -83,8 +85,9 @@ public class RulesRecyclerViewAdapter extends RecyclerView.Adapter<RulesRecycler } return true; }; + imageAndGifPlugin = new ImageAndGifPlugin(); markwon = MarkdownUtils.createFullRedditMarkwon(activity, - miscPlugin, mPrimaryTextColor, spoilerBackgroundColor, onLinkLongClickListener); + miscPlugin, imageAndGifPlugin, mPrimaryTextColor, spoilerBackgroundColor, onLinkLongClickListener); } @NonNull diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java index b55f12ce..e0fd9975 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java @@ -44,6 +44,7 @@ import ml.docilealligator.infinityforreddit.bottomsheetfragments.CopyTextBottomS import ml.docilealligator.infinityforreddit.bottomsheetfragments.UrlMenuBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed; +import ml.docilealligator.infinityforreddit.markdown.ImageAndGifPlugin; import ml.docilealligator.infinityforreddit.markdown.MarkdownUtils; import ml.docilealligator.infinityforreddit.subreddit.FetchSubredditData; import ml.docilealligator.infinityforreddit.subreddit.SubredditData; @@ -144,8 +145,9 @@ public class SidebarFragment extends Fragment { urlMenuBottomSheetFragment.show(getChildFragmentManager(), null); return true; }; + ImageAndGifPlugin imageAndGifPlugin = new ImageAndGifPlugin(); Markwon markwon = MarkdownUtils.createFullRedditMarkwon(activity, - miscPlugin, markdownColor, spoilerBackgroundColor, onLinkLongClickListener); + miscPlugin, imageAndGifPlugin, markdownColor, spoilerBackgroundColor, onLinkLongClickListener); MarkwonAdapter markwonAdapter = MarkdownUtils.createTablesAdapter(activity, Glide.with(this)); linearLayoutManager = new LinearLayoutManagerBugFixed(activity); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifBlock.java b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifBlock.java index deea10c3..ce456f92 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifBlock.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifBlock.java @@ -2,5 +2,12 @@ package ml.docilealligator.infinityforreddit.markdown; import org.commonmark.node.CustomBlock; +import ml.docilealligator.infinityforreddit.post.Post; + public class ImageAndGifBlock extends CustomBlock { + public Post.MediaMetadata mediaMetadata; + + public ImageAndGifBlock(Post.MediaMetadata mediaMetadata) { + this.mediaMetadata = mediaMetadata; + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifBlockParser.java b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifBlockParser.java index aa312eb1..0da8a93e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifBlockParser.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifBlockParser.java @@ -10,15 +10,18 @@ import org.commonmark.parser.block.BlockStart; import org.commonmark.parser.block.MatchedBlockParser; import org.commonmark.parser.block.ParserState; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; +import ml.docilealligator.infinityforreddit.post.Post; + public class ImageAndGifBlockParser extends AbstractBlockParser { private ImageAndGifBlock imageAndGifBlock; - ImageAndGifBlockParser() { - this.imageAndGifBlock = new ImageAndGifBlock(); + ImageAndGifBlockParser(Post.MediaMetadata mediaMetadata) { + this.imageAndGifBlock = new ImageAndGifBlock(mediaMetadata); } @Override @@ -33,6 +36,8 @@ public class ImageAndGifBlockParser extends AbstractBlockParser { public static class Factory extends AbstractBlockParserFactory { private Pattern redditPreviewPattern = Pattern.compile("!\\[img]\\(https://preview.redd.it/\\w+.(jpg|png)((\\?+[-a-zA-Z0-9()@:%_+.~#?&/=]*)|)\\)"); + private Map<String, Post.MediaMetadata> 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<String, Post.MediaMetadata> 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<ImageAndGifBlock, Ima public void bindHolder(@NonNull Markwon markwon, @NonNull Holder holder, @NonNull ImageAndGifBlock node) { holder.binding.progressBarMarkdownImageAndGifBlock.setVisibility(View.VISIBLE); - RequestBuilder<Drawable> 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<Drawable> 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<String, Post.MediaMetadata> 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<String, Post.MediaMetadata> 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<String, Post.MediaMetadata> parseMediaMetadata(JSONObject data) { + try { + if (data.has(JSONUtils.MEDIA_METADATA_KEY)) { + Map<String, Post.MediaMetadata> mediaMetadataMap = new HashMap<>(); + JSONObject mediaMetadataJSON = data.getJSONObject(JSONUtils.MEDIA_METADATA_KEY); + for (Iterator<String> 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<Post.Preview> previews, + String authorFlair, String authorFlairHTML, long postTimeMillis, String title, + ArrayList<Post.Preview> previews, Map<String, Post.MediaMetadata> 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<Post> CREATOR = new Creator<Post>() { - @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<Preview> previews = new ArrayList<>(); + @Nullable + private Map<String, MediaMetadata> mediaMetadataMap; private ArrayList<Gallery> 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<String, MediaMetadata>) in.readValue(getClass().getClassLoader()); + gallery = in.createTypedArrayList(Gallery.CREATOR); } + public static final Creator<Post> CREATOR = new Creator<Post>() { + @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<String, MediaMetadata> getMediaMetadataMap() { + return mediaMetadataMap; + } + + public void setMediaMetadataMap(Map<String, MediaMetadata> mediaMetadataMap) { + this.mediaMetadataMap = mediaMetadataMap; + } + public ArrayList<Gallery> getGallery() { return gallery; } @@ -526,56 +592,6 @@ public class Post implements Parcelable { } @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)) { return false; @@ -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<MediaMetadata> CREATOR = new Creator<MediaMetadata>() { + @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<MediaItem> CREATOR = new Creator<MediaItem>() { + @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(); |