From 3f93ed03edcafceb94f17d89c54b032e0b112e24 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Thu, 26 Sep 2024 17:00:20 -0400 Subject: Continue optimizing class structures in PostRecyclerViewAdapter. --- .../adapters/PostRecyclerViewAdapter.java | 566 ++++++++++++++++----- 1 file changed, 452 insertions(+), 114 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index 7af77dd3..ad395005 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -808,30 +808,30 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { + int position = getBindingAdapterPosition(); + if (position < 0) { + return; + } + Post post = getItem(position); + if (post != null) { + if (canStartActivity) { + canStartActivity = false; + Intent intent = new Intent(mActivity, ViewSubredditDetailActivity.class); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, + post.getSubredditName()); + mActivity.startActivity(intent); + } + } + }); + + iconGifImageView.setOnClickListener(view -> subredditTextView.performClick()); + } else { + subredditTextView.setOnClickListener(view -> { + int position = getBindingAdapterPosition(); + if (position < 0) { + return; + } + Post post = getItem(position); + if (post != null) { + if (canStartActivity) { + canStartActivity = false; + Intent intent = new Intent(mActivity, ViewSubredditDetailActivity.class); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, + post.getSubredditName()); + mActivity.startActivity(intent); + } + } + }); + + iconGifImageView.setOnClickListener(view -> userTextView.performClick()); + } + + userTextView.setOnClickListener(view -> { + if (!canStartActivity) { + return; + } + int position = getBindingAdapterPosition(); + if (position < 0) { + return; + } + Post post = getItem(position); + if (post == null || post.isAuthorDeleted()) { + return; + } + canStartActivity = false; + Intent intent = new Intent(mActivity, ViewUserDetailActivity.class); + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, post.getAuthor()); + mActivity.startActivity(intent); + }); + + setOnClickListeners( + typeTextView, + nsfwTextView, + flairTextView, + upvoteButton, + scoreTextView, + downvoteButton, + commentsCountButton, + saveButton, + shareButton); + } + void setBaseView(AspectRatioGifImageView iconGifImageView, TextView nameTextView, + CustomTextView typeTextView, + CustomTextView nsfwTextView, + CustomTextView flairTextView, MaterialButton upvoteButton, TextView scoreTextView, MaterialButton downvoteButton, @@ -2730,7 +2824,10 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter nameTextView.performClick()); - setOnClickListeners(upvoteButton, + setOnClickListeners(typeTextView, + nsfwTextView, + flairTextView, + upvoteButton, scoreTextView, downvoteButton, commentsCountButton, @@ -3049,9 +3146,247 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { + int position = getBindingAdapterPosition(); + if (position < 0) { + return; + } + Post post = getItem(position); + if (post != null) { + mCallback.nsfwChipClicked(); + } + }); + typeTextView.setOnClickListener(view -> { + int position = getBindingAdapterPosition(); + if (position < 0) { + return; + } + Post post = getItem(position); + if (post != null) { + mCallback.typeChipClicked(post.getPostType()); + } + }); + + flairTextView.setOnClickListener(view -> { + int position = getBindingAdapterPosition(); + if (position < 0) { + return; + } + Post post = getItem(position); + if (post != null) { + mCallback.flairChipClicked(post.getFlair()); + } + }); + } + } + abstract void markPostRead(Post post, boolean changePostItemColor); } + @UnstableApi + public abstract class VideoAutoplayImpl implements ToroPlayer { + View itemView; + AspectRatioFrameLayout aspectRatioFrameLayout; + GifImageView previewImageView; + ImageView errorLoadingRedgifsImageView; + PlayerView videoPlayer; + ImageView muteButton; + ImageView fullscreenButton; + ImageView playPauseButton; + @Nullable + Container container; + @Nullable + ExoPlayerViewHelper helper; + private Uri mediaUri; + private float volume; + public Call fetchRedgifsOrStreamableVideoCall; + private boolean isManuallyPaused; + private Drawable playDrawable; + private Drawable pauseDrawable; + + public VideoAutoplayImpl(View itemView, AspectRatioFrameLayout aspectRatioFrameLayout, + GifImageView previewImageView, ImageView errorLoadingRedgifsImageView, + PlayerView videoPlayer, ImageView muteButton, ImageView fullscreenButton, + ImageView playPauseButton, + Drawable playDrawable, Drawable pauseDrawable) { + this.itemView = itemView; + this.aspectRatioFrameLayout = aspectRatioFrameLayout; + this.previewImageView = previewImageView; + this.errorLoadingRedgifsImageView = errorLoadingRedgifsImageView; + this.videoPlayer = videoPlayer; + this.muteButton = muteButton; + this.fullscreenButton = fullscreenButton; + this.playPauseButton = playPauseButton; + this.playDrawable = playDrawable; + this.pauseDrawable = pauseDrawable; + } + + void bindVideoUri(Uri videoUri) { + mediaUri = videoUri; + } + + void setVolume(float volume) { + this.volume = volume; + } + + void resetVolume() { + volume = 0f; + } + + private void savePlaybackInfo(int order, @Nullable PlaybackInfo playbackInfo) { + if (container != null) container.savePlaybackInfo(order, playbackInfo); + } + + void loadFallbackDirectVideo() { + Post post = getPost(); + if (post.getVideoFallBackDirectUrl() != null) { + mediaUri = Uri.parse(post.getVideoFallBackDirectUrl()); + post.setVideoDownloadUrl(post.getVideoFallBackDirectUrl()); + post.setVideoUrl(post.getVideoFallBackDirectUrl()); + post.setLoadRedgifsOrStreamableVideoSuccess(true); + if (container != null) { + container.onScrollStateChanged(RecyclerView.SCROLL_STATE_IDLE); + } + } + } + + @NonNull + @Override + public View getPlayerView() { + return videoPlayer; + } + + @NonNull + @Override + public PlaybackInfo getCurrentPlaybackInfo() { + return helper != null && mediaUri != null ? helper.getLatestPlaybackInfo() : new PlaybackInfo(); + } + + @OptIn(markerClass = UnstableApi.class) + @Override + public void initialize(@NonNull Container container, @NonNull PlaybackInfo playbackInfo) { + if (this.container == null) { + this.container = container; + } + if (mediaUri == null) { + return; + } + if (helper == null) { + helper = new ExoPlayerViewHelper(this, mediaUri, null, mExoCreator); + helper.addEventListener(new Playable.DefaultEventListener() { + @Override + public void onEvents(@NonNull Player player, @NonNull Player.Events events) { + if (events.containsAny( + Player.EVENT_PLAY_WHEN_READY_CHANGED, + Player.EVENT_PLAYBACK_STATE_CHANGED, + Player.EVENT_PLAYBACK_SUPPRESSION_REASON_CHANGED)) { + playPauseButton.setImageDrawable(Util.shouldShowPlayButton(player) ? playDrawable : pauseDrawable); + } + } + + @Override + public void onTracksChanged(@NonNull Tracks tracks) { + ImmutableList trackGroups = tracks.getGroups(); + if (!trackGroups.isEmpty()) { + for (int i = 0; i < trackGroups.size(); i++) { + String mimeType = trackGroups.get(i).getTrackFormat(0).sampleMimeType; + if (mimeType != null && mimeType.contains("audio")) { + if (mFragment.getMasterMutingOption() != null) { + volume = mFragment.getMasterMutingOption() ? 0f : 1f; + } + helper.setVolume(volume); + muteButton.setVisibility(View.VISIBLE); + if (volume != 0f) { + muteButton.setImageDrawable(ContextCompat.getDrawable(mActivity, R.drawable.ic_unmute_24dp)); + } else { + muteButton.setImageDrawable(ContextCompat.getDrawable(mActivity, R.drawable.ic_mute_24dp)); + } + break; + } + } + } else { + muteButton.setVisibility(View.GONE); + } + } + + @Override + public void onRenderedFirstFrame() { + mGlide.clear(previewImageView); + previewImageView.setVisibility(View.GONE); + } + + @Override + public void onPlayerError(@NonNull PlaybackException error) { + Post post = getPost(); + if (post.getVideoFallBackDirectUrl() == null || post.getVideoFallBackDirectUrl().equals(mediaUri.toString())) { + errorLoadingRedgifsImageView.setVisibility(View.VISIBLE); + } else { + loadFallbackDirectVideo(); + } + } + }); + } + helper.initialize(container, playbackInfo); + } + + @Override + public void play() { + if (helper != null && mediaUri != null) { + if (!isPlaying() && isManuallyPaused) { + helper.play(); + pause(); + helper.setVolume(volume); + } else { + helper.play(); + } + } + } + + @Override + public void pause() { + if (helper != null) helper.pause(); + } + + @Override + public boolean isPlaying() { + return helper != null && helper.isPlaying(); + } + + @Override + public void release() { + if (helper != null) { + helper.release(); + helper = null; + } + isManuallyPaused = false; + container = null; + } + + @Override + public boolean wantsToPlay() { + return canPlayVideo && mediaUri != null && ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= mStartAutoplayVisibleAreaOffset; + } + + abstract Post getPost(); + } + public class PostBaseViewHolder extends PostViewHolder { TextView subredditTextView; TextView userTextView; @@ -3099,6 +3434,9 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null) { - mCallback.nsfwChipClicked(); - } - }); - typeTextView.setOnClickListener(view -> { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null) { - mCallback.typeChipClicked(post.getPostType()); - } - }); - - flairTextView.setOnClickListener(view -> { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null) { - mCallback.flairChipClicked(post.getFlair()); - } - }); - } } void setBaseView(AspectRatioGifImageView iconGifImageView, @@ -3318,7 +3622,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter fetchRedgifsOrStreamableVideoCall; private boolean isManuallyPaused; private Drawable playDrawable; - private Drawable pauseDrawable; + private Drawable pauseDrawable;*/ + VideoAutoplayImpl toroPlayer; @OptIn(markerClass = UnstableApi.class) PostBaseVideoAutoplayViewHolder(View rootView, @@ -3390,7 +3695,22 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { - if (helper != null) { - if (helper.getVolume() != 0) { + if (toroPlayer.helper != null) { + if (toroPlayer.helper.getVolume() != 0) { muteButton.setImageDrawable(ContextCompat.getDrawable(mActivity, R.drawable.ic_mute_24dp)); - helper.setVolume(0f); - volume = 0f; + toroPlayer.helper.setVolume(0f); + toroPlayer.volume = 0f; mFragment.videoAutoplayChangeMutingOption(true); } else { muteButton.setImageDrawable(ContextCompat.getDrawable(mActivity, R.drawable.ic_unmute_24dp)); - helper.setVolume(1f); - volume = 1f; + toroPlayer.helper.setVolume(1f); + toroPlayer.volume = 1f; mFragment.videoAutoplayChangeMutingOption(false); } } @@ -3428,8 +3748,8 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { if (isPlaying()) { pause(); - isManuallyPaused = true; - savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + toroPlayer.isManuallyPaused = true; + toroPlayer.savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); } else { - isManuallyPaused = false; + toroPlayer.isManuallyPaused = false; play(); } }); @@ -3461,7 +3781,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null && !(mActivity instanceof FilteredPostsActivity)) { - mCallback.nsfwChipClicked(); - } - }); - - typeTextView.setOnClickListener(view -> { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null && !(mActivity instanceof FilteredPostsActivity)) { - mCallback.typeChipClicked(post.getPostType()); - } - }); - - flairTextView.setOnClickListener(view -> { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null && !(mActivity instanceof FilteredPostsActivity)) { - mCallback.flairChipClicked(post.getFlair()); - } - }); - imageView.setOnClickListener(view -> { int position = getBindingAdapterPosition(); if (position < 0) { @@ -4300,6 +4637,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter