From 033c8bd24184258904f68bee13b96b6c491fec05 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 29 Apr 2020 18:13:28 +0800 Subject: Buggy video and Gif autoplay. --- app/build.gradle | 2 + .../Adapter/PostRecyclerViewAdapter.java | 1395 +++++++++++++++----- .../infinityforreddit/AppModule.java | 19 + .../infinityforreddit/Fragment/PostFragment.java | 33 +- .../exo_player_autoplay_button_background.xml | 9 + .../layout/exo_autoplay_playback_control_view.xml | 70 + app/src/main/res/layout/fragment_post.xml | 8 +- app/src/main/res/layout/item_post_gif_type.xml | 314 +++++ app/src/main/res/layout/item_post_image_type.xml | 314 +++++ app/src/main/res/layout/item_post_link_type.xml | 324 +++++ .../res/layout/item_post_no_preview_link_type.xml | 293 ++++ app/src/main/res/layout/item_post_text_type.xml | 288 ++++ .../res/layout/item_post_video_type_autoplay.xml | 289 ++++ build.gradle | 1 + 14 files changed, 3016 insertions(+), 343 deletions(-) create mode 100644 app/src/main/res/drawable/exo_player_autoplay_button_background.xml create mode 100644 app/src/main/res/layout/exo_autoplay_playback_control_view.xml create mode 100644 app/src/main/res/layout/item_post_gif_type.xml create mode 100644 app/src/main/res/layout/item_post_image_type.xml create mode 100644 app/src/main/res/layout/item_post_link_type.xml create mode 100644 app/src/main/res/layout/item_post_no_preview_link_type.xml create mode 100644 app/src/main/res/layout/item_post_text_type.xml create mode 100644 app/src/main/res/layout/item_post_video_type_autoplay.xml diff --git a/app/build.gradle b/app/build.gradle index 6a76e789..707333bf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -86,4 +86,6 @@ dependencies { implementation 'com.atlassian.commonmark:commonmark:0.13.1' implementation 'com.google.code.gson:gson:2.8.6' implementation 'me.zhanghai.android.fastscroll:library:1.1.2' + implementation "im.ene.toro3:toro:$rootProject.toroVersion" + implementation "im.ene.toro3:toro-ext-exoplayer:$rootProject.toroVersion" } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index 38aa8b00..55a75184 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -1,14 +1,13 @@ package ml.docilealligator.infinityforreddit.Adapter; import android.content.Intent; +import android.content.SharedPreferences; import android.content.res.ColorStateList; import android.graphics.ColorFilter; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; -import android.text.Html; -import android.text.Spannable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -38,6 +37,8 @@ import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; +import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; +import com.google.android.exoplayer2.ui.PlayerView; import com.google.android.material.card.MaterialCardView; import com.libRG.CustomTextView; @@ -45,6 +46,13 @@ import org.greenrobot.eventbus.EventBus; import butterknife.BindView; import butterknife.ButterKnife; +import im.ene.toro.CacheManager; +import im.ene.toro.ToroPlayer; +import im.ene.toro.ToroUtil; +import im.ene.toro.exoplayer.ExoCreator; +import im.ene.toro.exoplayer.ExoPlayerViewHelper; +import im.ene.toro.media.PlaybackInfo; +import im.ene.toro.widget.Container; import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import ml.docilealligator.infinityforreddit.Activity.FilteredThingActivity; @@ -68,7 +76,6 @@ import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SaveThing; import ml.docilealligator.infinityforreddit.User.UserDao; -import ml.docilealligator.infinityforreddit.Utils.GlideImageGetter; import ml.docilealligator.infinityforreddit.Utils.RedditUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; @@ -79,10 +86,16 @@ import retrofit2.Retrofit; * Created by alex on 2/25/18. */ -public class PostRecyclerViewAdapter extends PagedListAdapter { - private static final int VIEW_TYPE_DATA = 0; - private static final int VIEW_TYPE_ERROR = 1; - private static final int VIEW_TYPE_LOADING = 2; +public class PostRecyclerViewAdapter extends PagedListAdapter implements CacheManager { + private static final int VIEW_TYPE_POST_CARD_VIDEO_TYPE_AUTOPLAY = 1; + private static final int VIEW_TYPE_POST_CARD_GIF_TYPE = 2; + private static final int VIEW_TYPE_POST_CARD_IMAGE_TYPE = 3; + private static final int VIEW_TYPE_POST_CARD_LINK_TYPE = 4; + private static final int VIEW_TYPE_POST_CARD_NO_PREVIEW_LINK_TYPE = 5; + private static final int VIEW_TYPE_POST_CARD_TEXT_TYPE = 6; + private static final int VIEW_TYPE_POST_COMPACT = 7; + private static final int VIEW_TYPE_ERROR = 8; + private static final int VIEW_TYPE_LOADING = 9; private static final DiffUtil.ItemCallback DIFF_CALLBACK = new DiffUtil.ItemCallback() { @Override public boolean areItemsTheSame(@NonNull Post post, @NonNull Post t1) { @@ -140,12 +153,79 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { + ((PostBaseViewHolder) holder).cardView.setOnClickListener(view -> { if (canStartActivity) { canStartActivity = false; @@ -260,9 +378,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { + ((PostBaseViewHolder) holder).subredditTextView.setText(subredditNamePrefixed); + ((PostBaseViewHolder) holder).userTextView.setText(authorPrefixed); + ((PostBaseViewHolder) holder).userTextView.setOnClickListener(view -> { if (canStartActivity) { canStartActivity = false; Intent intent = new Intent(mActivity, ViewUserDetailActivity.class); @@ -279,13 +397,13 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0) { @@ -298,11 +416,11 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0) { @@ -331,15 +449,15 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { + ((PostBaseViewHolder) holder).subredditTextView.setOnClickListener(view -> { if (canStartActivity) { canStartActivity = false; if (post.getSubredditNamePrefixed().startsWith("u/")) { @@ -356,8 +474,8 @@ public class PostRecyclerViewAdapter extends PagedListAdapter - ((PostViewHolder) holder).subredditTextView.performClick()); + ((PostBaseViewHolder) holder).iconGifImageView.setOnClickListener(view -> + ((PostBaseViewHolder) holder).subredditTextView.performClick()); } else { if (post.getAuthorIconUrl() == null) { String authorName = post.getAuthor().equals("[deleted]") ? post.getSubredditNamePrefixed().substring(2) : post.getAuthor(); @@ -366,13 +484,13 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0) { @@ -385,14 +503,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { + ((PostBaseViewHolder) holder).subredditTextView.setOnClickListener(view -> { if (canStartActivity) { canStartActivity = false; if (post.getSubredditNamePrefixed().startsWith("u/")) { @@ -408,27 +526,27 @@ public class PostRecyclerViewAdapter extends PagedListAdapter - ((PostViewHolder) holder).userTextView.performClick()); + ((PostBaseViewHolder) holder).iconGifImageView.setOnClickListener(view -> + ((PostBaseViewHolder) holder).userTextView.performClick()); } if (mShowElapsedTime) { - ((PostViewHolder) holder).postTimeTextView.setText( + ((PostBaseViewHolder) holder).postTimeTextView.setText( Utils.getElapsedTime(mActivity, post.getPostTimeMillis())); } else { - ((PostViewHolder) holder).postTimeTextView.setText(postTime); + ((PostBaseViewHolder) holder).postTimeTextView.setText(post.getPostTime()); } - ((PostViewHolder) holder).titleTextView.setText(title); - ((PostViewHolder) holder).scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, post.getScore() + post.getVoteType())); + ((PostBaseViewHolder) holder).titleTextView.setText(post.getTitle()); + ((PostBaseViewHolder) holder).scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, post.getScore() + post.getVoteType())); if (post.isLocked()) { - ((PostViewHolder) holder).lockedImageView.setVisibility(View.VISIBLE); + ((PostBaseViewHolder) holder).lockedImageView.setVisibility(View.VISIBLE); } - if (nsfw) { + if (post.isNSFW()) { if (!(mActivity instanceof FilteredThingActivity)) { - ((PostViewHolder) holder).nsfwTextView.setOnClickListener(view -> { + ((PostBaseViewHolder) holder).nsfwTextView.setOnClickListener(view -> { Intent intent = new Intent(mActivity, FilteredThingActivity.class); intent.putExtra(FilteredThingActivity.EXTRA_NAME, post.getSubredditNamePrefixed().substring(2)); intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); @@ -436,176 +554,71 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= android.os.Build.VERSION_CODES.N) { - flairHTML = (Spannable) Html.fromHtml(flair, Html.FROM_HTML_MODE_LEGACY, glideImageGetter, null); - } else { - flairHTML = (Spannable) Html.fromHtml(flair, glideImageGetter, null); - } - ((PostViewHolder) holder).flairTextView.setText(flairHTML); + Utils.setHTMLWithImageToTextView(((PostBaseViewHolder) holder).flairTextView, flair); } if (nAwards > 0) { - ((PostViewHolder) holder).awardsTextView.setVisibility(View.VISIBLE); + ((PostBaseViewHolder) holder).awardsTextView.setVisibility(View.VISIBLE); if (nAwards == 1) { - ((PostViewHolder) holder).awardsTextView.setText(mActivity.getString(R.string.one_award)); + ((PostBaseViewHolder) holder).awardsTextView.setText(mActivity.getString(R.string.one_award)); } else { - ((PostViewHolder) holder).awardsTextView.setText(mActivity.getString(R.string.n_awards, nAwards)); + ((PostBaseViewHolder) holder).awardsTextView.setText(mActivity.getString(R.string.n_awards, nAwards)); } } - switch (voteType) { + switch (post.getVoteType()) { case 1: //Upvoted - ((PostViewHolder) holder).upvoteButton.setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); - ((PostViewHolder) holder).scoreTextView.setTextColor(mUpvotedColor); + ((PostBaseViewHolder) holder).upvoteButton.setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); + ((PostBaseViewHolder) holder).scoreTextView.setTextColor(mUpvotedColor); break; case -1: //Downvoted - ((PostViewHolder) holder).downvoteButton.setColorFilter(mDownvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); - ((PostViewHolder) holder).scoreTextView.setTextColor(mDownvotedColor); + ((PostBaseViewHolder) holder).downvoteButton.setColorFilter(mDownvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); + ((PostBaseViewHolder) holder).scoreTextView.setTextColor(mDownvotedColor); break; } - if (post.getPostType() != Post.TEXT_TYPE && post.getPostType() != Post.NO_PREVIEW_LINK_TYPE) { - ((PostViewHolder) holder).relativeLayout.setVisibility(View.VISIBLE); - ((PostViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostViewHolder) holder).imageView.setVisibility(View.VISIBLE); - ((PostViewHolder) holder).imageView - .setRatio((float) post.getPreviewHeight() / post.getPreviewWidth()); - loadImage(holder, post); - } - if (mPostType == PostDataSource.TYPE_SUBREDDIT && !mDisplaySubredditName && post.isStickied()) { - ((PostViewHolder) holder).stickiedPostImageView.setVisibility(View.VISIBLE); - mGlide.load(R.drawable.ic_thumbtack_24dp).into(((PostViewHolder) holder).stickiedPostImageView); + ((PostBaseViewHolder) holder).stickiedPostImageView.setVisibility(View.VISIBLE); + mGlide.load(R.drawable.ic_thumbtack_24dp).into(((PostBaseViewHolder) holder).stickiedPostImageView); } if (isArchived) { - ((PostViewHolder) holder).archivedImageView.setVisibility(View.VISIBLE); + ((PostBaseViewHolder) holder).archivedImageView.setVisibility(View.VISIBLE); - ((PostViewHolder) holder).upvoteButton + ((PostBaseViewHolder) holder).upvoteButton .setColorFilter(mVoteAndReplyUnavailableVoteButtonColor, android.graphics.PorterDuff.Mode.SRC_IN); - ((PostViewHolder) holder).downvoteButton + ((PostBaseViewHolder) holder).downvoteButton .setColorFilter(mVoteAndReplyUnavailableVoteButtonColor, android.graphics.PorterDuff.Mode.SRC_IN); } if (post.isCrosspost()) { - ((PostViewHolder) holder).crosspostImageView.setVisibility(View.VISIBLE); + ((PostBaseViewHolder) holder).crosspostImageView.setVisibility(View.VISIBLE); } if (!(mActivity instanceof FilteredThingActivity)) { - ((PostViewHolder) holder).typeTextView.setOnClickListener(view -> mCallback.typeChipClicked(post.getPostType())); + ((PostBaseViewHolder) holder).typeTextView.setOnClickListener(view -> mCallback.typeChipClicked(post.getPostType())); } switch (post.getPostType()) { - case Post.IMAGE_TYPE: - ((PostViewHolder) holder).typeTextView.setText(R.string.image); - - final String imageUrl = post.getUrl(); - ((PostViewHolder) holder).imageView.setOnClickListener(view -> { - Intent intent = new Intent(mActivity, ViewImageActivity.class); - intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, imageUrl); - intent.putExtra(ViewImageActivity.FILE_NAME_KEY, subredditName - + "-" + id + ".jpg"); - intent.putExtra(ViewImageActivity.POST_TITLE_KEY, post.getTitle()); - mActivity.startActivity(intent); - }); - - if (post.getPreviewWidth() <= 0 || post.getPreviewHeight() <= 0) { - ((PostViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); - ((PostViewHolder) holder).imageView.getLayoutParams().height = (int) (400 * mScale); - } - break; - case Post.LINK_TYPE: - ((PostViewHolder) holder).typeTextView.setText(R.string.link); - - ((PostViewHolder) holder).linkTextView.setVisibility(View.VISIBLE); - String domain = Uri.parse(post.getUrl()).getHost(); - ((PostViewHolder) holder).linkTextView.setText(domain); - - ((PostViewHolder) holder).imageView.setOnClickListener(view -> { - Intent intent = new Intent(mActivity, LinkResolverActivity.class); - Uri uri = Uri.parse(post.getUrl()); - if (uri.getScheme() == null && uri.getHost() == null) { - intent.setData(LinkResolverActivity.getRedditUriByPath(post.getUrl())); - } else { - intent.setData(uri); - } - mActivity.startActivity(intent); - }); - break; case Post.GIF_TYPE: - ((PostViewHolder) holder).typeTextView.setText(R.string.gif); - - final Uri gifVideoUri = Uri.parse(post.getVideoUrl()); - ((PostViewHolder) holder).imageView.setOnClickListener(view -> { - Intent intent = new Intent(mActivity, ViewGIFActivity.class); - intent.setData(gifVideoUri); - intent.putExtra(ViewGIFActivity.FILE_NAME_KEY, subredditName - + "-" + id + ".gif"); - intent.putExtra(ViewGIFActivity.GIF_URL_KEY, post.getVideoUrl()); - intent.putExtra(ViewGIFActivity.POST_TITLE_KEY, post.getTitle()); - mActivity.startActivity(intent); - }); - - ((PostViewHolder) holder).playButtonImageView.setVisibility(View.VISIBLE); + ((PostBaseViewHolder) holder).typeTextView.setText(R.string.gif); break; case Post.VIDEO_TYPE: - ((PostViewHolder) holder).typeTextView.setText(R.string.video); - - final Uri videoUri = Uri.parse(post.getVideoUrl()); - ((PostViewHolder) holder).imageView.setOnClickListener(view -> { - Intent intent = new Intent(mActivity, ViewVideoActivity.class); - intent.setData(videoUri); - intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); - intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, subredditName); - intent.putExtra(ViewVideoActivity.EXTRA_ID, fullName); - intent.putExtra(ViewVideoActivity.EXTRA_POST_TITLE, post.getTitle()); - mActivity.startActivity(intent); - }); - - ((PostViewHolder) holder).playButtonImageView.setVisibility(View.VISIBLE); - break; - case Post.NO_PREVIEW_LINK_TYPE: - ((PostViewHolder) holder).typeTextView.setText(R.string.link); - - String noPreviewLinkUrl = post.getUrl(); - ((PostViewHolder) holder).linkTextView.setVisibility(View.VISIBLE); - String noPreviewLinkDomain = Uri.parse(noPreviewLinkUrl).getHost(); - ((PostViewHolder) holder).linkTextView.setText(noPreviewLinkDomain); - ((PostViewHolder) holder).noPreviewLinkImageView.setVisibility(View.VISIBLE); - ((PostViewHolder) holder).noPreviewLinkImageView.setOnClickListener(view -> { - Intent intent = new Intent(mActivity, LinkResolverActivity.class); - Uri uri = Uri.parse(post.getUrl()); - if (uri.getScheme() == null && uri.getHost() == null) { - intent.setData(LinkResolverActivity.getRedditUriByPath(post.getUrl())); - } else { - intent.setData(uri); - } - mActivity.startActivity(intent); - }); - break; - case Post.TEXT_TYPE: - ((PostViewHolder) holder).typeTextView.setText(R.string.text); - if (post.getSelfTextPlainTrimmed() != null && !post.getSelfTextPlainTrimmed().equals("")) { - ((PostViewHolder) holder).contentTextView.setVisibility(View.VISIBLE); - ((PostViewHolder) holder).contentTextView.setText(post.getSelfTextPlainTrimmed()); - } + ((PostBaseViewHolder) holder).typeTextView.setText(R.string.video); break; } - ((PostViewHolder) holder).upvoteButton.setOnClickListener(view -> { + ((PostBaseViewHolder) holder).upvoteButton.setOnClickListener(view -> { if (mAccessToken == null) { Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); return; @@ -616,48 +629,48 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { + ((PostBaseViewHolder) holder).downvoteButton.setOnClickListener(view -> { if (mAccessToken == null) { Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); return; @@ -687,48 +700,48 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { + ((PostBaseViewHolder) holder).saveButton.setOnClickListener(view -> { if (mAccessToken == null) { Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); return; } if (post.isSaved()) { - ((PostViewHolder) holder).saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp); + ((PostBaseViewHolder) holder).saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp); SaveThing.unsaveThing(mOauthRetrofit, mAccessToken, post.getFullName(), new SaveThing.SaveThingListener() { @Override public void success() { post.setSaved(false); - ((PostViewHolder) holder).saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp); + ((PostBaseViewHolder) holder).saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp); Toast.makeText(mActivity, R.string.post_unsaved_success, Toast.LENGTH_SHORT).show(); EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post)); } @@ -776,19 +789,19 @@ public class PostRecyclerViewAdapter extends PagedListAdapter shareLink(post)); + ((PostBaseViewHolder) holder).shareButton.setOnClickListener(view -> shareLink(post)); + + if (holder instanceof PostVideoAutoplayViewHolder) { + /*final Uri gifVideoUri = Uri.parse(post.getVideoUrl()); + ((PostBaseViewHolder) holder).imageView.setOnClickListener(view -> { + Intent intent = new Intent(mActivity, ViewGIFActivity.class); + intent.setData(gifVideoUri); + intent.putExtra(ViewGIFActivity.FILE_NAME_KEY, subredditName + + "-" + id + ".gif"); + intent.putExtra(ViewGIFActivity.GIF_URL_KEY, post.getVideoUrl()); + intent.putExtra(ViewGIFActivity.POST_TITLE_KEY, post.getTitle()); + mActivity.startActivity(intent); + }); + + ((PostBaseViewHolder) holder).playButtonImageView.setVisibility(View.VISIBLE);*/ + + + /*final Uri videoUri = Uri.parse(post.getVideoUrl()); + ((PostBaseViewHolder) holder).imageView.setOnClickListener(view -> { + Intent intent = new Intent(mActivity, ViewVideoActivity.class); + intent.setData(videoUri); + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); + intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, subredditName); + intent.putExtra(ViewVideoActivity.EXTRA_ID, fullName); + intent.putExtra(ViewVideoActivity.EXTRA_POST_TITLE, post.getTitle()); + mActivity.startActivity(intent); + }); + + ((PostBaseViewHolder) holder).playButtonImageView.setVisibility(View.VISIBLE);*/ + /*((PostVideoAutoplayViewHolder) holder).videoPlayer.setRatio( + (float) post.getPreviewHeight() / post.getPreviewWidth() + );*/ + ((PostVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio((float) post.getPreviewWidth() / post.getPreviewHeight()); + ((PostVideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(post.getVideoUrl())); + } else if (holder instanceof PostGifTypeViewHolder) { + ((PostGifTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostGifTypeViewHolder) holder).imageView + .setRatio((float) post.getPreviewHeight() / post.getPreviewWidth()); + loadImage(holder, post); + + final String imageUrl = post.getUrl(); + ((PostGifTypeViewHolder) holder).imageView.setOnClickListener(view -> { + Intent intent = new Intent(mActivity, ViewImageActivity.class); + intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, imageUrl); + intent.putExtra(ViewImageActivity.FILE_NAME_KEY, subredditName + + "-" + id + ".jpg"); + intent.putExtra(ViewImageActivity.POST_TITLE_KEY, post.getTitle()); + mActivity.startActivity(intent); + }); + + if (post.getPreviewWidth() <= 0 || post.getPreviewHeight() <= 0) { + ((PostGifTypeViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); + ((PostGifTypeViewHolder) holder).imageView.getLayoutParams().height = (int) (400 * mScale); + } + } else if (holder instanceof PostImageTypeViewHolder) { + ((PostImageTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostImageTypeViewHolder) holder).imageView + .setRatio((float) post.getPreviewHeight() / post.getPreviewWidth()); + loadImage(holder, post); + + final String imageUrl = post.getUrl(); + ((PostImageTypeViewHolder) holder).imageView.setOnClickListener(view -> { + Intent intent = new Intent(mActivity, ViewImageActivity.class); + intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, imageUrl); + intent.putExtra(ViewImageActivity.FILE_NAME_KEY, subredditName + + "-" + id + ".jpg"); + intent.putExtra(ViewImageActivity.POST_TITLE_KEY, post.getTitle()); + mActivity.startActivity(intent); + }); + + if (post.getPreviewWidth() <= 0 || post.getPreviewHeight() <= 0) { + ((PostImageTypeViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); + ((PostImageTypeViewHolder) holder).imageView.getLayoutParams().height = (int) (400 * mScale); + } + } else if (holder instanceof PostLinkTypeViewHolder) { + ((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostLinkTypeViewHolder) holder).imageView + .setRatio((float) post.getPreviewHeight() / post.getPreviewWidth()); + loadImage(holder, post); + + ((PostLinkTypeViewHolder) holder).linkTextView.setVisibility(View.VISIBLE); + String domain = Uri.parse(post.getUrl()).getHost(); + ((PostLinkTypeViewHolder) holder).linkTextView.setText(domain); + + ((PostLinkTypeViewHolder) holder).imageView.setOnClickListener(view -> { + Intent intent = new Intent(mActivity, LinkResolverActivity.class); + Uri uri = Uri.parse(post.getUrl()); + if (uri.getScheme() == null && uri.getHost() == null) { + intent.setData(LinkResolverActivity.getRedditUriByPath(post.getUrl())); + } else { + intent.setData(uri); + } + mActivity.startActivity(intent); + }); + } else if (holder instanceof PostNoPreviewLinkTypeViewHolder) { + String noPreviewLinkUrl = post.getUrl(); + ((PostNoPreviewLinkTypeViewHolder) holder).linkTextView.setVisibility(View.VISIBLE); + String noPreviewLinkDomain = Uri.parse(noPreviewLinkUrl).getHost(); + ((PostNoPreviewLinkTypeViewHolder) holder).linkTextView.setText(noPreviewLinkDomain); + ((PostNoPreviewLinkTypeViewHolder) holder).noPreviewLinkImageView.setOnClickListener(view -> { + Intent intent = new Intent(mActivity, LinkResolverActivity.class); + Uri uri = Uri.parse(post.getUrl()); + if (uri.getScheme() == null && uri.getHost() == null) { + intent.setData(LinkResolverActivity.getRedditUriByPath(post.getUrl())); + } else { + intent.setData(uri); + } + mActivity.startActivity(intent); + }); + } else if (holder instanceof PostTextTypeViewHolder) { + if (post.getSelfTextPlainTrimmed() != null && !post.getSelfTextPlainTrimmed().equals("")) { + ((PostTextTypeViewHolder) holder).contentTextView.setVisibility(View.VISIBLE); + ((PostTextTypeViewHolder) holder).contentTextView.setText(post.getSelfTextPlainTrimmed()); + } + } + + + } } else if (holder instanceof PostCompactViewHolder) { Post post = getItem(position); @@ -1359,15 +1489,72 @@ public class PostRecyclerViewAdapter extends PagedListAdapter imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + ((PostImageTypeViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostImageTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); + ((PostImageTypeViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { + ((PostImageTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostImageTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + loadImage(holder, post); + }); + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + ((PostImageTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((PostImageTypeViewHolder) holder).progressBar.setVisibility(View.GONE); + return false; + } + }); + + if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) { + imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) + .into(((PostImageTypeViewHolder) holder).imageView); + } else { + imageRequestBuilder.into(((PostImageTypeViewHolder) holder).imageView); + } + } else if (holder instanceof PostGifTypeViewHolder) { + String url = mAutoplay ? post.getUrl() : post.getPreviewUrl(); + RequestBuilder imageRequestBuilder = mGlide.load(url).listener(new RequestListener() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + ((PostGifTypeViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostGifTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); + ((PostGifTypeViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { + ((PostGifTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostGifTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + loadImage(holder, post); + }); + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + ((PostGifTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((PostGifTypeViewHolder) holder).progressBar.setVisibility(View.GONE); + return false; + } + }); + + if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) { + imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) + .into(((PostGifTypeViewHolder) holder).imageView); + } else { + imageRequestBuilder.into(((PostGifTypeViewHolder) holder).imageView); + } + } else if (holder instanceof PostLinkTypeViewHolder) { RequestBuilder imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - ((PostViewHolder) holder).progressBar.setVisibility(View.GONE); - ((PostViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); - ((PostViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { - ((PostViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostLinkTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); + ((PostLinkTypeViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { + ((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostLinkTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); loadImage(holder, post); }); return false; @@ -1375,17 +1562,17 @@ public class PostRecyclerViewAdapter extends PagedListAdapter target, DataSource dataSource, boolean isFirstResource) { - ((PostViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - ((PostViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostLinkTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.GONE); return false; } }); if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) - .into(((PostViewHolder) holder).imageView); + .into(((PostLinkTypeViewHolder) holder).imageView); } else { - imageRequestBuilder.into(((PostViewHolder) holder).imageView); + imageRequestBuilder.into(((PostLinkTypeViewHolder) holder).imageView); } } else if (holder instanceof PostCompactViewHolder) { String previewUrl = post.getThumbnailPreviewUrl().equals("") ? post.getPreviewUrl() : post.getThumbnailPreviewUrl(); @@ -1434,20 +1621,6 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= super.getItemCount()) { + return null; + } + return getItem(order); + } + + @Nullable + @Override + public Integer getOrderForKey(@NonNull Object key) { + if (getCurrentList() != null && key instanceof Post) { + return getCurrentList().indexOf(key); + } + + return null; + } + public interface Callback { void retryLoadingMore(); void typeChipClicked(int filter); } - class PostViewHolder extends RecyclerView.ViewHolder { - @BindView(R.id.card_view_item_post) + class PostBaseViewHolder extends RecyclerView.ViewHolder { MaterialCardView cardView; - @BindView(R.id.icon_gif_image_view_item_post) AspectRatioGifImageView iconGifImageView; - @BindView(R.id.subreddit_name_text_view_item_post) TextView subredditTextView; - @BindView(R.id.user_text_view_item_post) TextView userTextView; - @BindView(R.id.stickied_post_image_view_item_post) ImageView stickiedPostImageView; - @BindView(R.id.post_time_text_view_best_item_post) TextView postTimeTextView; - @BindView(R.id.title_text_view_best_item_post) TextView titleTextView; - @BindView(R.id.type_text_view_item_post) CustomTextView typeTextView; - @BindView(R.id.archived_image_view_item_post) ImageView archivedImageView; - @BindView(R.id.locked_image_view_item_post) ImageView lockedImageView; - @BindView(R.id.crosspost_image_view_item_post) ImageView crosspostImageView; - @BindView(R.id.nsfw_text_view_item_post) CustomTextView nsfwTextView; - @BindView(R.id.spoiler_custom_text_view_item_post) CustomTextView spoilerTextView; - @BindView(R.id.flair_custom_text_view_item_post) CustomTextView flairTextView; - @BindView(R.id.awards_text_view_item_post) CustomTextView awardsTextView; - @BindView(R.id.link_text_view_item_post) - TextView linkTextView; - @BindView(R.id.image_view_wrapper_item_post) - RelativeLayout relativeLayout; - @BindView(R.id.progress_bar_item_post) - ProgressBar progressBar; - @BindView(R.id.image_view_best_post_item) - AspectRatioGifImageView imageView; - @BindView(R.id.play_button_image_view_item_post) - ImageView playButtonImageView; - @BindView(R.id.load_image_error_relative_layout_item_post) - RelativeLayout errorRelativeLayout; - @BindView(R.id.image_view_no_preview_link_item_post) - ImageView noPreviewLinkImageView; - @BindView(R.id.content_text_view_item_post) - TextView contentTextView; - @BindView(R.id.bottom_constraint_layout_item_post) ConstraintLayout bottomConstraintLayout; - @BindView(R.id.plus_button_item_post) ImageView upvoteButton; - @BindView(R.id.score_text_view_item_post) TextView scoreTextView; - @BindView(R.id.minus_button_item_post) ImageView downvoteButton; - @BindView(R.id.comments_count_item_post) TextView commentsCountTextView; - @BindView(R.id.save_button_item_post) ImageView saveButton; - @BindView(R.id.share_button_item_post) ImageView shareButton; - PostViewHolder(View itemView) { + PostBaseViewHolder(@NonNull View itemView) { super(itemView); - ButterKnife.bind(this, itemView); + } + + void setBaseView(MaterialCardView cardView, + AspectRatioGifImageView iconGifImageView, + TextView subredditTextView, + TextView userTextView, + ImageView stickiedPostImageView, + TextView postTimeTextView, + TextView titleTextView, + CustomTextView typeTextView, + ImageView archivedImageView, + ImageView lockedImageView, + ImageView crosspostImageView, + CustomTextView nsfwTextView, + CustomTextView spoilerTextView, + CustomTextView flairTextView, + CustomTextView awardsTextView, + ConstraintLayout bottomConstraintLayout, + ImageView upvoteButton, + TextView scoreTextView, + ImageView downvoteButton, + TextView commentsCountTextView, + ImageView saveButton, + ImageView shareButton) { + this.cardView = cardView; + this.iconGifImageView = iconGifImageView; + this.subredditTextView = subredditTextView; + this.userTextView = userTextView; + this.stickiedPostImageView = stickiedPostImageView; + this.postTimeTextView = postTimeTextView; + this.titleTextView = titleTextView; + this.typeTextView = typeTextView; + this.archivedImageView = archivedImageView; + this.lockedImageView = lockedImageView; + this.crosspostImageView = crosspostImageView; + this.nsfwTextView = nsfwTextView; + this.spoilerTextView = spoilerTextView; + this.flairTextView = flairTextView; + this.awardsTextView = awardsTextView; + this.bottomConstraintLayout = bottomConstraintLayout; + this.upvoteButton = upvoteButton; + this.scoreTextView = scoreTextView; + this.downvoteButton = downvoteButton; + this.commentsCountTextView = commentsCountTextView; + this.saveButton = saveButton; + this.shareButton = shareButton; + scoreTextView.setOnClickListener(view -> { //Do nothing in order to prevent clicking this to start ViewPostDetailActivity }); @@ -1665,7 +1869,6 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0.85; + } + + @Override + public int getPlayerOrder() { + return getAdapterPosition(); + } + } + + class PostGifTypeViewHolder extends PostBaseViewHolder { + @BindView(R.id.card_view_item_post_gif_type) + MaterialCardView cardView; + @BindView(R.id.icon_gif_image_view_item_post_gif_type) + AspectRatioGifImageView iconGifImageView; + @BindView(R.id.subreddit_name_text_view_item_post_gif_type) + TextView subredditTextView; + @BindView(R.id.user_text_view_item_post_gif_type) + TextView userTextView; + @BindView(R.id.stickied_post_image_view_item_post_gif_type) + ImageView stickiedPostImageView; + @BindView(R.id.post_time_text_view_best_item_post_gif_type) + TextView postTimeTextView; + @BindView(R.id.title_text_view_best_item_post_gif_type) + TextView titleTextView; + @BindView(R.id.type_text_view_item_post_gif_type) + CustomTextView typeTextView; + @BindView(R.id.archived_image_view_item_post_gif_type) + ImageView archivedImageView; + @BindView(R.id.locked_image_view_item_post_gif_type) + ImageView lockedImageView; + @BindView(R.id.crosspost_image_view_item_post_gif_type) + ImageView crosspostImageView; + @BindView(R.id.nsfw_text_view_item_post_gif_type) + CustomTextView nsfwTextView; + @BindView(R.id.spoiler_custom_text_view_item_post_gif_type) + CustomTextView spoilerTextView; + @BindView(R.id.flair_custom_text_view_item_post_gif_type) + CustomTextView flairTextView; + @BindView(R.id.awards_text_view_item_post_gif_type) + CustomTextView awardsTextView; + @BindView(R.id.image_view_wrapper_item_post_gif_type) + RelativeLayout relativeLayout; + @BindView(R.id.progress_bar_item_post_gif_type) + ProgressBar progressBar; + @BindView(R.id.image_view_best_post_item) + AspectRatioGifImageView imageView; + @BindView(R.id.load_image_error_relative_layout_item_post_gif_type) + RelativeLayout errorRelativeLayout; + @BindView(R.id.bottom_constraint_layout_item_post_gif_type) + ConstraintLayout bottomConstraintLayout; + @BindView(R.id.plus_button_item_post_gif_type) + ImageView upvoteButton; + @BindView(R.id.score_text_view_item_post_gif_type) + TextView scoreTextView; + @BindView(R.id.minus_button_item_post_gif_type) + ImageView downvoteButton; + @BindView(R.id.comments_count_item_post_gif_type) + TextView commentsCountTextView; + @BindView(R.id.save_button_item_post_gif_type) + ImageView saveButton; + @BindView(R.id.share_button_item_post_gif_type) + ImageView shareButton; + + PostGifTypeViewHolder(View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + setBaseView(cardView, + iconGifImageView, + subredditTextView, + userTextView, + stickiedPostImageView, + postTimeTextView, + titleTextView, + typeTextView, + archivedImageView, + lockedImageView, + crosspostImageView, + nsfwTextView, + spoilerTextView, + flairTextView, + awardsTextView, + bottomConstraintLayout, + upvoteButton, + scoreTextView, + downvoteButton, + commentsCountTextView, + saveButton, + shareButton); + + progressBar.setIndeterminateTintList(ColorStateList.valueOf(mColorAccent)); + } + } + + class PostImageTypeViewHolder extends PostBaseViewHolder { + @BindView(R.id.card_view_item_post_image_type) + MaterialCardView cardView; + @BindView(R.id.icon_gif_image_view_item_post_image_type) + AspectRatioGifImageView iconGifImageView; + @BindView(R.id.subreddit_name_text_view_item_post_image_type) + TextView subredditTextView; + @BindView(R.id.user_text_view_item_post_image_type) + TextView userTextView; + @BindView(R.id.stickied_post_image_view_item_post_image_type) + ImageView stickiedPostImageView; + @BindView(R.id.post_time_text_view_best_item_post_image_type) + TextView postTimeTextView; + @BindView(R.id.title_text_view_best_item_post_image_type) + TextView titleTextView; + @BindView(R.id.type_text_view_item_post_image_type) + CustomTextView typeTextView; + @BindView(R.id.archived_image_view_item_post_image_type) + ImageView archivedImageView; + @BindView(R.id.locked_image_view_item_post_image_type) + ImageView lockedImageView; + @BindView(R.id.crosspost_image_view_item_post_image_type) + ImageView crosspostImageView; + @BindView(R.id.nsfw_text_view_item_post_image_type) + CustomTextView nsfwTextView; + @BindView(R.id.spoiler_custom_text_view_item_post_image_type) + CustomTextView spoilerTextView; + @BindView(R.id.flair_custom_text_view_item_post_image_type) + CustomTextView flairTextView; + @BindView(R.id.awards_text_view_item_post_image_type) + CustomTextView awardsTextView; + @BindView(R.id.image_view_wrapper_item_post_image_type) + RelativeLayout relativeLayout; + @BindView(R.id.progress_bar_item_post_image_type) + ProgressBar progressBar; + @BindView(R.id.image_view_best_post_item) + AspectRatioGifImageView imageView; + @BindView(R.id.load_image_error_relative_layout_item_post_image_type) + RelativeLayout errorRelativeLayout; + @BindView(R.id.bottom_constraint_layout_item_post_image_type) + ConstraintLayout bottomConstraintLayout; + @BindView(R.id.plus_button_item_post_image_type) + ImageView upvoteButton; + @BindView(R.id.score_text_view_item_post_image_type) + TextView scoreTextView; + @BindView(R.id.minus_button_item_post_image_type) + ImageView downvoteButton; + @BindView(R.id.comments_count_item_post_image_type) + TextView commentsCountTextView; + @BindView(R.id.save_button_item_post_image_type) + ImageView saveButton; + @BindView(R.id.share_button_item_post_image_type) + ImageView shareButton; + + PostImageTypeViewHolder(View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + setBaseView(cardView, + iconGifImageView, + subredditTextView, + userTextView, + stickiedPostImageView, + postTimeTextView, + titleTextView, + typeTextView, + archivedImageView, + lockedImageView, + crosspostImageView, + nsfwTextView, + spoilerTextView, + flairTextView, + awardsTextView, + bottomConstraintLayout, + upvoteButton, + scoreTextView, + downvoteButton, + commentsCountTextView, + saveButton, + shareButton); + + progressBar.setIndeterminateTintList(ColorStateList.valueOf(mColorAccent)); + } + } + + class PostLinkTypeViewHolder extends PostBaseViewHolder { + @BindView(R.id.card_view_item_post_link_type) + MaterialCardView cardView; + @BindView(R.id.icon_gif_image_view_item_post_link_type) + AspectRatioGifImageView iconGifImageView; + @BindView(R.id.subreddit_name_text_view_item_post_link_type) + TextView subredditTextView; + @BindView(R.id.user_text_view_item_post_link_type) + TextView userTextView; + @BindView(R.id.stickied_post_image_view_item_post_link_type) + ImageView stickiedPostImageView; + @BindView(R.id.post_time_text_view_best_item_post_link_type) + TextView postTimeTextView; + @BindView(R.id.title_text_view_best_item_post_link_type) + TextView titleTextView; + @BindView(R.id.type_text_view_item_post_link_type) + CustomTextView typeTextView; + @BindView(R.id.archived_image_view_item_post_link_type) + ImageView archivedImageView; + @BindView(R.id.locked_image_view_item_post_link_type) + ImageView lockedImageView; + @BindView(R.id.crosspost_image_view_item_post_link_type) + ImageView crosspostImageView; + @BindView(R.id.nsfw_text_view_item_post_link_type) + CustomTextView nsfwTextView; + @BindView(R.id.spoiler_custom_text_view_item_post_link_type) + CustomTextView spoilerTextView; + @BindView(R.id.flair_custom_text_view_item_post_link_type) + CustomTextView flairTextView; + @BindView(R.id.awards_text_view_item_post_link_type) + CustomTextView awardsTextView; + @BindView(R.id.link_text_view_item_post_link_type) + TextView linkTextView; + @BindView(R.id.image_view_wrapper_item_post_link_type) + RelativeLayout relativeLayout; + @BindView(R.id.progress_bar_item_post_link_type) + ProgressBar progressBar; + @BindView(R.id.image_view_best_post_item) + AspectRatioGifImageView imageView; + @BindView(R.id.load_image_error_relative_layout_item_post_link_type) + RelativeLayout errorRelativeLayout; + @BindView(R.id.bottom_constraint_layout_item_post_link_type) + ConstraintLayout bottomConstraintLayout; + @BindView(R.id.plus_button_item_post_link_type) + ImageView upvoteButton; + @BindView(R.id.score_text_view_item_post_link_type) + TextView scoreTextView; + @BindView(R.id.minus_button_item_post_link_type) + ImageView downvoteButton; + @BindView(R.id.comments_count_item_post_link_type) + TextView commentsCountTextView; + @BindView(R.id.save_button_item_post_link_type) + ImageView saveButton; + @BindView(R.id.share_button_item_post_link_type) + ImageView shareButton; + + PostLinkTypeViewHolder(View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + setBaseView(cardView, + iconGifImageView, + subredditTextView, + userTextView, + stickiedPostImageView, + postTimeTextView, + titleTextView, + typeTextView, + archivedImageView, + lockedImageView, + crosspostImageView, + nsfwTextView, + spoilerTextView, + flairTextView, + awardsTextView, + bottomConstraintLayout, + upvoteButton, + scoreTextView, + downvoteButton, + commentsCountTextView, + saveButton, + shareButton); + + linkTextView.setTextColor(mSecondaryTextColor); + progressBar.setIndeterminateTintList(ColorStateList.valueOf(mColorAccent)); + } + } + + class PostNoPreviewLinkTypeViewHolder extends PostBaseViewHolder { + @BindView(R.id.card_view_item_post_no_preview_link_type) + MaterialCardView cardView; + @BindView(R.id.icon_gif_image_view_item_post_no_preview_link_type) + AspectRatioGifImageView iconGifImageView; + @BindView(R.id.subreddit_name_text_view_item_post_no_preview_link_type) + TextView subredditTextView; + @BindView(R.id.user_text_view_item_post_no_preview_link_type) + TextView userTextView; + @BindView(R.id.stickied_post_image_view_item_post_no_preview_link_type) + ImageView stickiedPostImageView; + @BindView(R.id.post_time_text_view_best_item_post_no_preview_link_type) + TextView postTimeTextView; + @BindView(R.id.title_text_view_best_item_post_no_preview_link_type) + TextView titleTextView; + @BindView(R.id.type_text_view_item_post_no_preview_link_type) + CustomTextView typeTextView; + @BindView(R.id.archived_image_view_item_post_no_preview_link_type) + ImageView archivedImageView; + @BindView(R.id.locked_image_view_item_post_no_preview_link_type) + ImageView lockedImageView; + @BindView(R.id.crosspost_image_view_item_post_no_preview_link_type) + ImageView crosspostImageView; + @BindView(R.id.nsfw_text_view_item_post_no_preview_link_type) + CustomTextView nsfwTextView; + @BindView(R.id.spoiler_custom_text_view_item_post_no_preview_link_type) + CustomTextView spoilerTextView; + @BindView(R.id.flair_custom_text_view_item_post_no_preview_link_type) + CustomTextView flairTextView; + @BindView(R.id.awards_text_view_item_post_no_preview_link_type) + CustomTextView awardsTextView; + @BindView(R.id.link_text_view_item_post_no_preview_link_type) + TextView linkTextView; + @BindView(R.id.image_view_no_preview_link_item_post_no_preview_link_type) + ImageView noPreviewLinkImageView; + @BindView(R.id.bottom_constraint_layout_item_post_no_preview_link_type) + ConstraintLayout bottomConstraintLayout; + @BindView(R.id.plus_button_item_post_no_preview_link_type) + ImageView upvoteButton; + @BindView(R.id.score_text_view_item_post_no_preview_link_type) + TextView scoreTextView; + @BindView(R.id.minus_button_item_post_no_preview_link_type) + ImageView downvoteButton; + @BindView(R.id.comments_count_item_post_no_preview_link_type) + TextView commentsCountTextView; + @BindView(R.id.save_button_item_post_no_preview_link_type) + ImageView saveButton; + @BindView(R.id.share_button_item_post_no_preview_link_type) + ImageView shareButton; + + PostNoPreviewLinkTypeViewHolder(View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + setBaseView(cardView, + iconGifImageView, + subredditTextView, + userTextView, + stickiedPostImageView, + postTimeTextView, + titleTextView, + typeTextView, + archivedImageView, + lockedImageView, + crosspostImageView, + nsfwTextView, + spoilerTextView, + flairTextView, + awardsTextView, + bottomConstraintLayout, + upvoteButton, + scoreTextView, + downvoteButton, + commentsCountTextView, + saveButton, + shareButton); + + linkTextView.setTextColor(mSecondaryTextColor); + noPreviewLinkImageView.setBackgroundColor(mNoPreviewLinkBackgroundColor); + } + } + + class PostTextTypeViewHolder extends PostBaseViewHolder { + @BindView(R.id.card_view_item_post_text_type) + MaterialCardView cardView; + @BindView(R.id.icon_gif_image_view_item_post_text_type) + AspectRatioGifImageView iconGifImageView; + @BindView(R.id.subreddit_name_text_view_item_post_text_type) + TextView subredditTextView; + @BindView(R.id.user_text_view_item_post_text_type) + TextView userTextView; + @BindView(R.id.stickied_post_image_view_item_post_text_type) + ImageView stickiedPostImageView; + @BindView(R.id.post_time_text_view_best_item_post_text_type) + TextView postTimeTextView; + @BindView(R.id.title_text_view_best_item_post_text_type) + TextView titleTextView; + @BindView(R.id.type_text_view_item_post_text_type) + CustomTextView typeTextView; + @BindView(R.id.archived_image_view_item_post_text_type) + ImageView archivedImageView; + @BindView(R.id.locked_image_view_item_post_text_type) + ImageView lockedImageView; + @BindView(R.id.crosspost_image_view_item_post_text_type) + ImageView crosspostImageView; + @BindView(R.id.nsfw_text_view_item_post_text_type) + CustomTextView nsfwTextView; + @BindView(R.id.spoiler_custom_text_view_item_post_text_type) + CustomTextView spoilerTextView; + @BindView(R.id.flair_custom_text_view_item_post_text_type) + CustomTextView flairTextView; + @BindView(R.id.awards_text_view_item_post_text_type) + CustomTextView awardsTextView; + @BindView(R.id.content_text_view_item_post_text_type) + TextView contentTextView; + @BindView(R.id.bottom_constraint_layout_item_post_text_type) + ConstraintLayout bottomConstraintLayout; + @BindView(R.id.plus_button_item_post_text_type) + ImageView upvoteButton; + @BindView(R.id.score_text_view_item_post_text_type) + TextView scoreTextView; + @BindView(R.id.minus_button_item_post_text_type) + ImageView downvoteButton; + @BindView(R.id.comments_count_item_post_text_type) + TextView commentsCountTextView; + @BindView(R.id.save_button_item_post_text_type) + ImageView saveButton; + @BindView(R.id.share_button_item_post_text_type) + ImageView shareButton; + + PostTextTypeViewHolder (View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + setBaseView(cardView, + iconGifImageView, + subredditTextView, + userTextView, + stickiedPostImageView, + postTimeTextView, + titleTextView, + typeTextView, + archivedImageView, + lockedImageView, + crosspostImageView, + nsfwTextView, + spoilerTextView, + flairTextView, + awardsTextView, + bottomConstraintLayout, + upvoteButton, + scoreTextView, + downvoteButton, + commentsCountTextView, + saveButton, + shareButton); + + contentTextView.setTextColor(mPostContentColor); + } + } + class PostCompactViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.icon_gif_image_view_item_post_compact) AspectRatioGifImageView iconGifImageView; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java index fe483c3c..a15c01ab 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java @@ -6,6 +6,11 @@ import android.content.SharedPreferences; import androidx.preference.PreferenceManager; +import com.google.android.exoplayer2.database.ExoDatabaseProvider; +import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor; +import com.google.android.exoplayer2.upstream.cache.SimpleCache; + +import java.io.File; import java.util.concurrent.TimeUnit; import javax.inject.Named; @@ -13,6 +18,10 @@ import javax.inject.Singleton; import dagger.Module; import dagger.Provides; +import im.ene.toro.exoplayer.Config; +import im.ene.toro.exoplayer.ExoCreator; +import im.ene.toro.exoplayer.MediaSourceBuilder; +import im.ene.toro.exoplayer.ToroExo; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.Utils.CustomThemeSharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.RedditUtils; @@ -147,4 +156,14 @@ class AppModule { @Named("amoled_theme") SharedPreferences amoledThemeSharedPreferences) { return new CustomThemeWrapper(lightThemeSharedPreferences, darkThemeSharedPreferences, amoledThemeSharedPreferences); } + + @Provides + @Singleton + ExoCreator provideExoCreator() { + SimpleCache cache = new SimpleCache(new File(mApplication.getCacheDir(), "/toro_cache"), + new LeastRecentlyUsedCacheEvictor(200 * 1024 * 1024), new ExoDatabaseProvider(mApplication)); + Config config = new Config.Builder(mApplication).setMediaSourceBuilder(MediaSourceBuilder.LOOPING).setCache(cache) + .build(); + return ToroExo.with(mApplication).getCreator(config); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java index 07ef965a..50e622ba 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -46,6 +46,8 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; +import im.ene.toro.exoplayer.ExoCreator; +import im.ene.toro.widget.Container; import ml.docilealligator.infinityforreddit.Activity.BaseActivity; import ml.docilealligator.infinityforreddit.Activity.FilteredThingActivity; import ml.docilealligator.infinityforreddit.Activity.MainActivity; @@ -94,7 +96,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @BindView(R.id.swipe_refresh_layout_post_fragment) SwipeRefreshLayout mSwipeRefreshLayout; @BindView(R.id.recycler_view_post_fragment) - RecyclerView mPostRecyclerView; + Container mPostRecyclerView; @BindView(R.id.fetch_post_info_linear_layout_post_fragment) LinearLayout mFetchPostInfoLinearLayout; @BindView(R.id.fetch_post_info_image_view_post_fragment) @@ -121,6 +123,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { SharedPreferences mPostLayoutSharedPreferences; @Inject CustomThemeWrapper customThemeWrapper; + @Inject + ExoCreator exoCreator; private RequestManager mGlide; private AppCompatActivity activity; private LinearLayoutManager mLinearLayoutManager; @@ -323,12 +327,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { int filter = getArguments().getInt(EXTRA_FILTER); String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); boolean nsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false); - boolean needBlurNsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.BLUR_NSFW_KEY, true); - boolean needBlurSpoiler = mSharedPreferences.getBoolean(SharedPreferencesUtils.BLUR_SPOILER_KEY, false); - boolean voteButtonsOnTheRight = mSharedPreferences.getBoolean(SharedPreferencesUtils.VOTE_BUTTONS_ON_THE_RIGHT_KEY, false); - boolean showElapsedTime = mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_ELAPSED_TIME_KEY, false); - boolean showDividerInCompactLayout = mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_DIVIDER_IN_COMPACT_LAYOUT, true); - boolean showAbsoluteNumberOfVotes = mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_ABSOLUTE_NUMBER_OF_VOTES, true); int defaultPostLayout = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.DEFAULT_POST_LAYOUT_KEY, "0")); if (postType == PostDataSource.TYPE_SEARCH) { @@ -342,9 +340,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, customThemeWrapper, accessToken, postType, postLayout, true, - needBlurNsfw, needBlurSpoiler, voteButtonsOnTheRight, showElapsedTime, - showDividerInCompactLayout, showAbsoluteNumberOfVotes, - new PostRecyclerViewAdapter.Callback() { + mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -407,9 +403,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, customThemeWrapper, accessToken, postType, postLayout, displaySubredditName, - needBlurNsfw, needBlurSpoiler, voteButtonsOnTheRight, showElapsedTime, - showDividerInCompactLayout, showAbsoluteNumberOfVotes, - new PostRecyclerViewAdapter.Callback() { + mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -457,9 +451,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, customThemeWrapper, accessToken, postType, postLayout, true, - needBlurNsfw, needBlurSpoiler, voteButtonsOnTheRight, showElapsedTime, - showDividerInCompactLayout, showAbsoluteNumberOfVotes, - new PostRecyclerViewAdapter.Callback() { + mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -505,9 +497,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, customThemeWrapper, accessToken, postType, postLayout, true, - needBlurNsfw, needBlurSpoiler, voteButtonsOnTheRight, showElapsedTime, - showDividerInCompactLayout, showAbsoluteNumberOfVotes, - new PostRecyclerViewAdapter.Callback() { + mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -546,9 +536,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, customThemeWrapper, accessToken, postType, postLayout, true, - needBlurNsfw, needBlurSpoiler, voteButtonsOnTheRight, showElapsedTime, - showDividerInCompactLayout, showAbsoluteNumberOfVotes, - new PostRecyclerViewAdapter.Callback() { + mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -569,6 +557,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } mPostRecyclerView.setAdapter(mAdapter); + mPostRecyclerView.setCacheManager(mAdapter); mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts)); mPostViewModel.hasPost().observe(this, hasPost -> { diff --git a/app/src/main/res/drawable/exo_player_autoplay_button_background.xml b/app/src/main/res/drawable/exo_player_autoplay_button_background.xml new file mode 100644 index 00000000..0ad9af8e --- /dev/null +++ b/app/src/main/res/drawable/exo_player_autoplay_button_background.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/exo_autoplay_playback_control_view.xml b/app/src/main/res/layout/exo_autoplay_playback_control_view.xml new file mode 100644 index 00000000..99cab8e2 --- /dev/null +++ b/app/src/main/res/layout/exo_autoplay_playback_control_view.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_post.xml b/app/src/main/res/layout/fragment_post.xml index 7862ba25..dc3248b5 100644 --- a/app/src/main/res/layout/fragment_post.xml +++ b/app/src/main/res/layout/fragment_post.xml @@ -10,7 +10,13 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - --> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_post_image_type.xml b/app/src/main/res/layout/item_post_image_type.xml new file mode 100644 index 00000000..6ca140d3 --- /dev/null +++ b/app/src/main/res/layout/item_post_image_type.xml @@ -0,0 +1,314 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_post_link_type.xml b/app/src/main/res/layout/item_post_link_type.xml new file mode 100644 index 00000000..43f51b10 --- /dev/null +++ b/app/src/main/res/layout/item_post_link_type.xml @@ -0,0 +1,324 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_post_no_preview_link_type.xml b/app/src/main/res/layout/item_post_no_preview_link_type.xml new file mode 100644 index 00000000..a6ef9eeb --- /dev/null +++ b/app/src/main/res/layout/item_post_no_preview_link_type.xml @@ -0,0 +1,293 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_post_text_type.xml b/app/src/main/res/layout/item_post_text_type.xml new file mode 100644 index 00000000..cc7a3bb9 --- /dev/null +++ b/app/src/main/res/layout/item_post_text_type.xml @@ -0,0 +1,288 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_post_video_type_autoplay.xml b/app/src/main/res/layout/item_post_video_type_autoplay.xml new file mode 100644 index 00000000..2f1c078a --- /dev/null +++ b/app/src/main/res/layout/item_post_video_type_autoplay.xml @@ -0,0 +1,289 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 21615e4c..c71c119b 100644 --- a/build.gradle +++ b/build.gradle @@ -33,4 +33,5 @@ task clean(type: Delete) { ext { roomVersion = '2.2.5' archLifecycleVersion = '2.2.0' + toroVersion = '3.7.0.2010003' } \ No newline at end of file -- cgit v1.2.3