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

View File

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

View File

@ -62,8 +62,8 @@ public interface MediaItemTag {
@NonNull
static Optional<MediaItemTag> from(@Nullable final MediaItem mediaItem) {
return Optional.ofNullable(mediaItem)
.map(item -> item.localConfiguration)
.map(localConfiguration -> localConfiguration.tag)
.flatMap(item -> Optional.ofNullable(item.localConfiguration))
.flatMap(localConfiguration -> Optional.ofNullable(localConfiguration.tag))
.filter(MediaItemTag.class::isInstance)
.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
.getNameById(videoStream.getFormatId()) + " " + videoStream.getResolution());
}
final VideoStream selectedVideoStream = player.getSelectedVideoStream();
if (selectedVideoStream != null) {
binding.qualityTextView.setText(selectedVideoStream.getResolution());
}
qualityPopupMenu.setOnMenuItemClickListener(this);
qualityPopupMenu.setOnDismissListener(this);
player.getSelectedVideoStream()
.ifPresent(s -> binding.qualityTextView.setText(s.getResolution()));
}
private void buildPlaybackSpeedMenu() {
@ -1171,12 +1170,9 @@ public abstract class VideoPlayerUi extends PlayerUi implements SeekBar.OnSeekBa
qualityPopupMenu.show();
isSomePopupMenuVisible = true;
final VideoStream videoStream = player.getSelectedVideoStream();
if (videoStream != null) {
//noinspection SetTextI18n
binding.qualityTextView.setText(MediaFormat.getNameById(videoStream.getFormatId())
+ " " + videoStream.getResolution());
}
player.getSelectedVideoStream()
.map(s -> MediaFormat.getNameById(s.getFormatId()) + " " + s.getResolution())
.ifPresent(binding.qualityTextView::setText);
}
/**
@ -1232,10 +1228,9 @@ public abstract class VideoPlayerUi extends PlayerUi implements SeekBar.OnSeekBa
Log.d(TAG, "onDismiss() called with: menu = [" + menu + "]");
}
isSomePopupMenuVisible = false; //TODO check if this works
final VideoStream selectedVideoStream = player.getSelectedVideoStream();
if (selectedVideoStream != null) {
binding.qualityTextView.setText(selectedVideoStream.getResolution());
}
player.getSelectedVideoStream()
.ifPresent(s -> binding.qualityTextView.setText(s.getResolution()));
if (player.isPlaying()) {
hideControls(DEFAULT_CONTROLS_DURATION, 0);
hideSystemUIIfNeeded();