From 77649d388c01c0fbca3a1fad2399eb79aa5a8cd2 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 | 91 ++++++++++--------- .../org/schabi/newpipe/util/ListHelper.java | 7 +- 2 files changed, 50 insertions(+), 48 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..13cbc9fd3f8 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,50 +1236,10 @@ 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; - } - - 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(); //TODO added, check if good - final String newResolution = availableStreams.get(menuItemIndex).getResolution(); - player.setRecovery(); - player.setPlaybackQuality(newResolution); - player.reloadPlayQueueManager(); - - binding.qualityTextView.setText(menuItem.getTitle()); + onQualityItemClick(menuItem); 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; - } - - player.saveStreamProgressState(); - final String newLanguage = availableStreams.get(menuItemIndex).getAudioTrackId(); - player.setRecovery(); - player.setAudioLanguage(newLanguage); - player.reloadPlayQueueManager(); - - binding.languageTextView.setText(menuItem.getTitle()); + onLanguageItemClick(menuItem); return true; } else if (menuItem.getGroupId() == POPUP_MENU_ID_PLAYBACK_SPEED) { final int speedIndex = menuItem.getItemId(); @@ -1292,6 +1252,53 @@ public boolean onMenuItemClick(@NonNull final MenuItem menuItem) { return false; } + private void onQualityItemClick(@NonNull final MenuItem menuItem) { + final int menuItemIndex = menuItem.getItemId(); + @Nullable final MediaItemTag currentMetadata = player.getCurrentMetadata(); + if (currentMetadata == null || currentMetadata.getMaybeQuality().isEmpty()) { + return; + } + + final MediaItemTag.Quality quality = currentMetadata.getMaybeQuality().get(); + final List availableStreams = quality.getSortedVideoStreams(); + final int selectedStreamIndex = quality.getSelectedVideoStreamIndex(); + if (selectedStreamIndex == menuItemIndex || availableStreams.size() <= menuItemIndex) { + return; + } + + player.saveStreamProgressState(); + final String newResolution = availableStreams.get(menuItemIndex).getResolution(); + player.setRecovery(); + player.setPlaybackQuality(newResolution); + player.reloadPlayQueueManager(); + + binding.qualityTextView.setText(menuItem.getTitle()); + } + + private void onLanguageItemClick(@NonNull final MenuItem menuItem) { + final int menuItemIndex = menuItem.getItemId(); + @Nullable final MediaItemTag currentMetadata = player.getCurrentMetadata(); + if (currentMetadata == null || currentMetadata.getMaybeAudioLanguage().isEmpty()) { + return; + } + + final MediaItemTag.AudioLanguage language = + currentMetadata.getMaybeAudioLanguage().get(); + final List availableStreams = language.getAudioStreams(); + final int selectedStreamIndex = language.getSelectedAudioStreamIndex(); + if (selectedStreamIndex == menuItemIndex || availableStreams.size() <= menuItemIndex) { + return; + } + + player.saveStreamProgressState(); + final String newLanguage = availableStreams.get(menuItemIndex).getAudioTrackId(); + player.setRecovery(); + player.setAudioLanguage(newLanguage); + player.reloadPlayQueueManager(); + + binding.languageTextView.setText(menuItem.getTitle()); + } + /** * Called when some popup menu is dismissed. */ 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) {