From a7a8a2651556a37f2f3a10a3c0885e13b8c4d6f9 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Thu, 26 Sep 2024 17:12:59 -0400 Subject: Continue optimizing class structures in PostRecyclerViewAdapter. Use VideoAutoplayImpl for concrete implementation of ToroPlayer. --- .../adapters/PostRecyclerViewAdapter.java | 647 +++++---------------- 1 file changed, 146 insertions(+), 501 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 ad395005..84faa827 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -1769,30 +1769,30 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { + if (helper != null) { + if (helper.getVolume() != 0) { + muteButton.setImageDrawable(ContextCompat.getDrawable(mActivity, R.drawable.ic_mute_24dp)); + helper.setVolume(0f); + volume = 0f; + mFragment.videoAutoplayChangeMutingOption(true); + } else { + muteButton.setImageDrawable(ContextCompat.getDrawable(mActivity, R.drawable.ic_unmute_24dp)); + helper.setVolume(1f); + volume = 1f; + mFragment.videoAutoplayChangeMutingOption(false); + } + } + }); + + fullscreenButton.setOnClickListener(view -> { + Post post = getPost(); + if (post != null) { + markPostRead(post, true); + + if (helper != null) { + openMedia(post, helper.getLatestPlaybackInfo().getResumePosition()); + } else { + openMedia(post, -1); + } + } + }); + + playPauseButton.setOnClickListener(view -> { + if (isPlaying()) { + pause(); + isManuallyPaused = true; + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + } else { + isManuallyPaused = false; + play(); + } + }); + + progressBar.addListener(new TimeBar.OnScrubListener() { + @Override + public void onScrubStart(TimeBar timeBar, long position) { + + } + + @Override + public void onScrubMove(TimeBar timeBar, long position) { + + } + + @Override + public void onScrubStop(TimeBar timeBar, long position, boolean canceled) { + if (!canceled) { + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + } + } + }); + + previewImageView.setOnClickListener(view -> fullscreenButton.performClick()); + + videoPlayer.setOnClickListener(view -> { + if (mEasierToWatchInFullScreen && videoPlayer.isControllerFullyVisible()) { + fullscreenButton.performClick(); + } + }); } void bindVideoUri(Uri videoUri) { @@ -3385,6 +3454,8 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter fetchRedgifsOrStreamableVideoCall; - private boolean isManuallyPaused; - private Drawable playDrawable; - private Drawable pauseDrawable;*/ VideoAutoplayImpl toroPlayer; @OptIn(markerClass = UnstableApi.class) @@ -3697,6 +3750,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { - if (toroPlayer.helper != null) { - if (toroPlayer.helper.getVolume() != 0) { - muteButton.setImageDrawable(ContextCompat.getDrawable(mActivity, R.drawable.ic_mute_24dp)); - toroPlayer.helper.setVolume(0f); - toroPlayer.volume = 0f; - mFragment.videoAutoplayChangeMutingOption(true); - } else { - muteButton.setImageDrawable(ContextCompat.getDrawable(mActivity, R.drawable.ic_unmute_24dp)); - toroPlayer.helper.setVolume(1f); - toroPlayer.volume = 1f; - mFragment.videoAutoplayChangeMutingOption(false); - } - } - }); - fullscreenButton.setOnClickListener(view -> { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null) { - markPostRead(post, true); - - if (toroPlayer.helper != null) { - openMedia(post, toroPlayer.helper.getLatestPlaybackInfo().getResumePosition()); - } else { - openMedia(post, -1); - } - } - }); - - playPauseButton.setOnClickListener(view -> { - if (isPlaying()) { - pause(); - toroPlayer.isManuallyPaused = true; - toroPlayer.savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); - } else { - toroPlayer.isManuallyPaused = false; - play(); - } - }); - - progressBar.addListener(new TimeBar.OnScrubListener() { @Override - public void onScrubStart(TimeBar timeBar, long position) { - - } - - @Override - public void onScrubMove(TimeBar timeBar, long position) { - - } - - @Override - public void onScrubStop(TimeBar timeBar, long position, boolean canceled) { - if (!canceled) { - toroPlayer.savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); - } - } - }); - - previewImageView.setOnClickListener(view -> fullscreenButton.performClick()); - - videoPlayer.setOnClickListener(view -> { - if (mEasierToWatchInFullScreen && videoPlayer.isControllerFullyVisible()) { - fullscreenButton.performClick(); + void markPostRead(Post post, boolean changePostItemColor) { + PostBaseVideoAutoplayViewHolder.this.markPostRead(post, changePostItemColor); } - }); - } - - @NonNull - @Override - public View getPlayerView() { - return toroPlayer.getPlayerView(); - } - - @NonNull - @Override - public PlaybackInfo getCurrentPlaybackInfo() { - return toroPlayer.getCurrentPlaybackInfo(); - } - - @Override - public void initialize(@NonNull Container container, @NonNull PlaybackInfo playbackInfo) { - toroPlayer.initialize(container, playbackInfo); - } - - @Override - public void play() { - toroPlayer.play(); - } - - @Override - public void pause() { - toroPlayer.pause(); - } - - @Override - public boolean isPlaying() { - return toroPlayer.isPlaying(); - } - - @Override - public void release() { - toroPlayer.release(); - } - - @Override - public boolean wantsToPlay() { - return toroPlayer.wantsToPlay(); - } - - @Override - public int getPlayerOrder() { - return toroPlayer.getPlayerOrder(); - } - - /*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() { - 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) { - if (post.getVideoFallBackDirectUrl() == null || post.getVideoFallBackDirectUrl().equals(mediaUri.toString())) { - errorLoadingRedgifsImageView.setVisibility(View.VISIBLE); - } else { - loadFallbackDirectVideo(); - } - } - }); - } - helper.initialize(container, playbackInfo); + public View getPlayerView() { + return toroPlayer.getPlayerView(); + } + + @NonNull + @Override + public PlaybackInfo getCurrentPlaybackInfo() { + return toroPlayer.getCurrentPlaybackInfo(); + } + + @Override + public void initialize(@NonNull Container container, @NonNull PlaybackInfo playbackInfo) { + toroPlayer.initialize(container, playbackInfo); } @Override public void play() { - if (helper != null && mediaUri != null) { - if (!isPlaying() && isManuallyPaused) { - helper.play(); - pause(); - helper.setVolume(volume); - } else { - helper.play(); - } - } + toroPlayer.play(); } @Override public void pause() { - if (helper != null) helper.pause(); + toroPlayer.pause(); } @Override public boolean isPlaying() { - return helper != null && helper.isPlaying(); + return toroPlayer.isPlaying(); } @Override public void release() { - if (helper != null) { - helper.release(); - helper = null; - } - isManuallyPaused = false; - container = null; + toroPlayer.release(); } @Override public boolean wantsToPlay() { - return canPlayVideo && mediaUri != null && ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= mStartAutoplayVisibleAreaOffset; + return toroPlayer.wantsToPlay(); } @Override public int getPlayerOrder() { - return getBindingAdapterPosition(); - }*/ + return toroPlayer.getPlayerOrder(); + } } @UnstableApi @@ -5390,24 +5216,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter fetchRedgifsOrStreamableVideoCall; - private boolean isManuallyPaused; - private Drawable playDrawable; - private Drawable pauseDrawable; + VideoAutoplayImpl toroPlayer; PostMaterial3CardBaseVideoAutoplayViewHolder(View rootView, AspectRatioGifImageView iconGifImageView, @@ -5447,237 +5256,73 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { - if (helper != null) { - if (helper.getVolume() != 0) { - muteButton.setImageDrawable(ContextCompat.getDrawable(mActivity, R.drawable.ic_mute_24dp)); - helper.setVolume(0f); - volume = 0f; - mFragment.videoAutoplayChangeMutingOption(true); - } else { - muteButton.setImageDrawable(ContextCompat.getDrawable(mActivity, R.drawable.ic_unmute_24dp)); - helper.setVolume(1f); - volume = 1f; - mFragment.videoAutoplayChangeMutingOption(false); - } - } - }); - - fullscreenButton.setOnClickListener(view -> { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null) { - markPostRead(post, true); - - if (helper != null) { - openMedia(post, helper.getLatestPlaybackInfo().getResumePosition()); - } else { - openMedia(post, -1); - } - } - }); - - playPauseButton.setOnClickListener(view -> { - if (isPlaying()) { - pause(); - isManuallyPaused = true; - savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); - } else { - isManuallyPaused = false; - play(); - } - }); - - progressBar.addListener(new TimeBar.OnScrubListener() { + toroPlayer = new VideoAutoplayImpl(rootView, aspectRatioFrameLayout, previewImageView, + errorLoadingRedgifsImageView, videoPlayer, muteButton, fullscreenButton, playPauseButton, + progressBar, + AppCompatResources.getDrawable(mActivity, R.drawable.ic_play_arrow_24dp), + AppCompatResources.getDrawable(mActivity, R.drawable.ic_pause_24dp)) { @Override - public void onScrubStart(TimeBar timeBar, long position) { - + public int getPlayerOrder() { + return getBindingAdapterPosition(); } @Override - public void onScrubMove(TimeBar timeBar, long position) { - + Post getPost() { + return post; } @Override - public void onScrubStop(TimeBar timeBar, long position, boolean canceled) { - if (!canceled) { - savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); - } + void markPostRead(Post post, boolean changePostItemColor) { + PostMaterial3CardBaseVideoAutoplayViewHolder.this.markPostRead(post, changePostItemColor); } - }); - - previewImageView.setOnClickListener(view -> fullscreenButton.performClick()); - - videoPlayer.setOnClickListener(view -> { - if (mEasierToWatchInFullScreen && videoPlayer.isControllerFullyVisible()) { - fullscreenButton.performClick(); - } - }); - } - - 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() { - 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; + return toroPlayer.getPlayerView(); } @NonNull @Override public PlaybackInfo getCurrentPlaybackInfo() { - return helper != null && mediaUri != null ? helper.getLatestPlaybackInfo() : new PlaybackInfo(); + return toroPlayer.getCurrentPlaybackInfo(); } @Override public void initialize(@NonNull Container container, @NonNull PlaybackInfo playbackInfo) { - if (mediaUri == null) { - return; - } - if (this.container == null) { - this.container = container; - } - 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) { - if (post.getVideoFallBackDirectUrl() == null || post.getVideoFallBackDirectUrl().equals(mediaUri.toString())) { - errorLoadingRedgifsImageView.setVisibility(View.VISIBLE); - } else { - loadFallbackDirectVideo(); - } - } - }); - } - helper.initialize(container, playbackInfo); + toroPlayer.initialize(container, playbackInfo); } @Override public void play() { - if (helper != null && mediaUri != null) { - if (!isPlaying() && isManuallyPaused) { - helper.play(); - pause(); - helper.setVolume(volume); - } else { - helper.play(); - } - } + toroPlayer.play(); } @Override public void pause() { - if (helper != null) helper.pause(); + toroPlayer.pause(); } @Override public boolean isPlaying() { - return helper != null && helper.isPlaying(); + return toroPlayer.isPlaying(); } @Override public void release() { - if (helper != null) { - helper.release(); - helper = null; - } - isManuallyPaused = false; - container = null; + toroPlayer.release(); } @Override public boolean wantsToPlay() { - return canPlayVideo && mediaUri != null && ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= mStartAutoplayVisibleAreaOffset; + return toroPlayer.wantsToPlay(); } @Override public int getPlayerOrder() { - return getBindingAdapterPosition(); + return toroPlayer.getPlayerOrder(); } } -- cgit v1.2.3