Skip to content

Commit

Permalink
fix: reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Theta-Dev committed Mar 18, 2023
1 parent dba53d2 commit 77649d3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 48 deletions.
91 changes: 49 additions & 42 deletions app/src/main/java/org/schabi/newpipe/player/ui/VideoPlayerUi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<VideoStream> 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<AudioStream> 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();
Expand All @@ -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<VideoStream> 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<AudioStream> 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.
*/
Expand Down
7 changes: 1 addition & 6 deletions app/src/main/java/org/schabi/newpipe/util/ListHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,7 @@ public static List<AudioStream> 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) {
Expand Down

0 comments on commit 77649d3

Please sign in to comment.