1
0
mirror of https://github.com/TeamNewPipe/NewPipe.git synced 2024-11-22 11:02:35 +01:00

Use Optional.map correctly and other improvements

This commit is contained in:
Stypox 2023-01-11 14:47:53 +01:00
parent e8216b2e80
commit 9c7ed80662
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
4 changed files with 19 additions and 23 deletions

View File

@ -255,7 +255,9 @@ public final class VideoDetailFragment
playerUi.ifPresent(MainPlayerUi::toggleFullscreen); playerUi.ifPresent(MainPlayerUi::toggleFullscreen);
} }
if (playAfterConnect || (currentInfo != null && isAutoplayEnabled() if (playAfterConnect
|| (currentInfo != null
&& isAutoplayEnabled()
&& playerUi.isEmpty())) { && playerUi.isEmpty())) {
autoPlayEnabled = true; // forcefully start playing autoPlayEnabled = true; // forcefully start playing
openVideoPlayerAutoFullscreen(); openVideoPlayerAutoFullscreen();
@ -1883,8 +1885,9 @@ public final class VideoDetailFragment
@Override @Override
public void onFullscreenStateChanged(final boolean fullscreen) { public void onFullscreenStateChanged(final boolean fullscreen) {
setupBrightness(); setupBrightness();
if (!isPlayerAndPlayerServiceAvailable() || player.UIs().get(MainPlayerUi.class).isEmpty() if (!isPlayerAndPlayerServiceAvailable()
|| getRoot().map(View::getParent).isEmpty()) { || player.UIs().get(MainPlayerUi.class).isEmpty()
|| getRoot().flatMap(v -> Optional.ofNullable(v.getParent())).isEmpty()) {
return; return;
} }

View File

@ -1875,8 +1875,7 @@ public final class Player implements PlaybackListener, Listener {
loadController.disablePreloadingOfCurrentTrack(); loadController.disablePreloadingOfCurrentTrack();
} }
@Nullable public Optional<VideoStream> getSelectedVideoStream() {
public VideoStream getSelectedVideoStream() {
return Optional.ofNullable(currentMetadata) return Optional.ofNullable(currentMetadata)
.flatMap(MediaItemTag::getMaybeQuality) .flatMap(MediaItemTag::getMaybeQuality)
.filter(quality -> { .filter(quality -> {
@ -1885,8 +1884,7 @@ public final class Player implements PlaybackListener, Listener {
&& selectedStreamIndex < quality.getSortedVideoStreams().size(); && selectedStreamIndex < quality.getSortedVideoStreams().size();
}) })
.map(quality -> quality.getSortedVideoStreams() .map(quality -> quality.getSortedVideoStreams()
.get(quality.getSelectedVideoStreamIndex())) .get(quality.getSelectedVideoStreamIndex()));
.orElse(null);
} }
//endregion //endregion

View File

@ -62,8 +62,8 @@ public interface MediaItemTag {
@NonNull @NonNull
static Optional<MediaItemTag> from(@Nullable final MediaItem mediaItem) { static Optional<MediaItemTag> from(@Nullable final MediaItem mediaItem) {
return Optional.ofNullable(mediaItem) return Optional.ofNullable(mediaItem)
.map(item -> item.localConfiguration) .flatMap(item -> Optional.ofNullable(item.localConfiguration))
.map(localConfiguration -> localConfiguration.tag) .flatMap(localConfiguration -> Optional.ofNullable(localConfiguration.tag))
.filter(MediaItemTag.class::isInstance) .filter(MediaItemTag.class::isInstance)
.map(MediaItemTag.class::cast); .map(MediaItemTag.class::cast);
} }

View File

@ -1060,12 +1060,11 @@ public abstract class VideoPlayerUi extends PlayerUi implements SeekBar.OnSeekBa
qualityPopupMenu.getMenu().add(POPUP_MENU_ID_QUALITY, i, Menu.NONE, MediaFormat qualityPopupMenu.getMenu().add(POPUP_MENU_ID_QUALITY, i, Menu.NONE, MediaFormat
.getNameById(videoStream.getFormatId()) + " " + videoStream.getResolution()); .getNameById(videoStream.getFormatId()) + " " + videoStream.getResolution());
} }
final VideoStream selectedVideoStream = player.getSelectedVideoStream();
if (selectedVideoStream != null) {
binding.qualityTextView.setText(selectedVideoStream.getResolution());
}
qualityPopupMenu.setOnMenuItemClickListener(this); qualityPopupMenu.setOnMenuItemClickListener(this);
qualityPopupMenu.setOnDismissListener(this); qualityPopupMenu.setOnDismissListener(this);
player.getSelectedVideoStream()
.ifPresent(s -> binding.qualityTextView.setText(s.getResolution()));
} }
private void buildPlaybackSpeedMenu() { private void buildPlaybackSpeedMenu() {
@ -1171,12 +1170,9 @@ public abstract class VideoPlayerUi extends PlayerUi implements SeekBar.OnSeekBa
qualityPopupMenu.show(); qualityPopupMenu.show();
isSomePopupMenuVisible = true; isSomePopupMenuVisible = true;
final VideoStream videoStream = player.getSelectedVideoStream(); player.getSelectedVideoStream()
if (videoStream != null) { .map(s -> MediaFormat.getNameById(s.getFormatId()) + " " + s.getResolution())
//noinspection SetTextI18n .ifPresent(binding.qualityTextView::setText);
binding.qualityTextView.setText(MediaFormat.getNameById(videoStream.getFormatId())
+ " " + videoStream.getResolution());
}
} }
/** /**
@ -1232,10 +1228,9 @@ public abstract class VideoPlayerUi extends PlayerUi implements SeekBar.OnSeekBa
Log.d(TAG, "onDismiss() called with: menu = [" + menu + "]"); Log.d(TAG, "onDismiss() called with: menu = [" + menu + "]");
} }
isSomePopupMenuVisible = false; //TODO check if this works isSomePopupMenuVisible = false; //TODO check if this works
final VideoStream selectedVideoStream = player.getSelectedVideoStream(); player.getSelectedVideoStream()
if (selectedVideoStream != null) { .ifPresent(s -> binding.qualityTextView.setText(s.getResolution()));
binding.qualityTextView.setText(selectedVideoStream.getResolution());
}
if (player.isPlaying()) { if (player.isPlaying()) {
hideControls(DEFAULT_CONTROLS_DURATION, 0); hideControls(DEFAULT_CONTROLS_DURATION, 0);
hideSystemUIIfNeeded(); hideSystemUIIfNeeded();