From 685550622ea8a66fc30d5b3cf28d954a83ac3666 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Sat, 18 Mar 2023 16:28:09 +0100 Subject: [PATCH] fix: reduce complexity --- .../newpipe/player/ui/VideoPlayerUi.java | 93 ++++++++++--------- .../org/schabi/newpipe/util/ListHelper.java | 7 +- 2 files changed, 51 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/ui/VideoPlayerUi.java b/app/src/main/java/org/schabi/newpipe/player/ui/VideoPlayerUi.java index 72d2f0b260a..aed7acb4888 100644 --- a/app/src/main/java/org/schabi/newpipe/player/ui/VideoPlayerUi.java +++ b/app/src/main/java/org/schabi/newpipe/player/ui/VideoPlayerUi.java @@ -1236,60 +1236,67 @@ public boolean onMenuItemClick(@NonNull final MenuItem menuItem) { } if (menuItem.getGroupId() == POPUP_MENU_ID_QUALITY) { - final int menuItemIndex = menuItem.getItemId(); - @Nullable final MediaItemTag currentMetadata = player.getCurrentMetadata(); - if (currentMetadata == null || currentMetadata.getMaybeQuality().isEmpty()) { - return true; - } + return onQualityItemClick(menuItem); + } else if (menuItem.getGroupId() == POPUP_MENU_ID_LANGUAGE) { + return onLanguageItemClick(menuItem); + } else if (menuItem.getGroupId() == POPUP_MENU_ID_PLAYBACK_SPEED) { + final int speedIndex = menuItem.getItemId(); + final float speed = PLAYBACK_SPEEDS[speedIndex]; - final MediaItemTag.Quality quality = currentMetadata.getMaybeQuality().get(); - final List availableStreams = quality.getSortedVideoStreams(); - final int selectedStreamIndex = quality.getSelectedVideoStreamIndex(); - if (selectedStreamIndex == menuItemIndex || availableStreams.size() <= menuItemIndex) { - return true; - } + player.setPlaybackSpeed(speed); + binding.playbackSpeed.setText(formatSpeed(speed)); + } - player.saveStreamProgressState(); //TODO added, check if good - final String newResolution = availableStreams.get(menuItemIndex).getResolution(); - player.setRecovery(); - player.setPlaybackQuality(newResolution); - player.reloadPlayQueueManager(); + return false; + } - binding.qualityTextView.setText(menuItem.getTitle()); + private boolean onQualityItemClick(@NonNull final MenuItem menuItem) { + final int menuItemIndex = menuItem.getItemId(); + @Nullable final MediaItemTag currentMetadata = player.getCurrentMetadata(); + if (currentMetadata == null || currentMetadata.getMaybeQuality().isEmpty()) { return true; - } else if (menuItem.getGroupId() == POPUP_MENU_ID_LANGUAGE) { - final int menuItemIndex = menuItem.getItemId(); - @Nullable final MediaItemTag currentMetadata = player.getCurrentMetadata(); - //noinspection SimplifyOptionalCallChains - if (currentMetadata == null || !currentMetadata.getMaybeAudioLanguage().isPresent()) { - return true; - } + } - final MediaItemTag.AudioLanguage language = - currentMetadata.getMaybeAudioLanguage().get(); - final List availableStreams = language.getAudioStreams(); - final int selectedStreamIndex = language.getSelectedAudioStreamIndex(); - if (selectedStreamIndex == menuItemIndex || availableStreams.size() <= menuItemIndex) { - return true; - } + final MediaItemTag.Quality quality = currentMetadata.getMaybeQuality().get(); + final List availableStreams = quality.getSortedVideoStreams(); + final int selectedStreamIndex = quality.getSelectedVideoStreamIndex(); + if (selectedStreamIndex == menuItemIndex || availableStreams.size() <= menuItemIndex) { + return true; + } - player.saveStreamProgressState(); - final String newLanguage = availableStreams.get(menuItemIndex).getAudioTrackId(); - player.setRecovery(); - player.setAudioLanguage(newLanguage); - player.reloadPlayQueueManager(); + player.saveStreamProgressState(); + final String newResolution = availableStreams.get(menuItemIndex).getResolution(); + player.setRecovery(); + player.setPlaybackQuality(newResolution); + player.reloadPlayQueueManager(); + + binding.qualityTextView.setText(menuItem.getTitle()); + return true; + } - binding.languageTextView.setText(menuItem.getTitle()); + private boolean onLanguageItemClick(@NonNull final MenuItem menuItem) { + final int menuItemIndex = menuItem.getItemId(); + @Nullable final MediaItemTag currentMetadata = player.getCurrentMetadata(); + if (currentMetadata == null || currentMetadata.getMaybeAudioLanguage().isEmpty()) { return true; - } else if (menuItem.getGroupId() == POPUP_MENU_ID_PLAYBACK_SPEED) { - final int speedIndex = menuItem.getItemId(); - final float speed = PLAYBACK_SPEEDS[speedIndex]; + } - player.setPlaybackSpeed(speed); - binding.playbackSpeed.setText(formatSpeed(speed)); + final MediaItemTag.AudioLanguage language = + currentMetadata.getMaybeAudioLanguage().get(); + final List availableStreams = language.getAudioStreams(); + final int selectedStreamIndex = language.getSelectedAudioStreamIndex(); + if (selectedStreamIndex == menuItemIndex || availableStreams.size() <= menuItemIndex) { + return true; } - return false; + player.saveStreamProgressState(); + final String newLanguage = availableStreams.get(menuItemIndex).getAudioTrackId(); + player.setRecovery(); + player.setAudioLanguage(newLanguage); + player.reloadPlayQueueManager(); + + binding.languageTextView.setText(menuItem.getTitle()); + return true; } /** diff --git a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java index 2714337c2fd..ef40692d7e2 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java @@ -212,12 +212,7 @@ public static List getFilteredAudioStreams( continue; } - final String trackId; - if (stream.getAudioTrackId() != null) { - trackId = stream.getAudioTrackId(); - } else { - trackId = ""; - } + final String trackId = Objects.toString(stream.getAudioTrackId(), ""); final AudioStream presentStream = collectedStreams.get(trackId); if (presentStream == null || cmp.compare(stream, presentStream) > 0) {