fix: reduce complexity

This commit is contained in:
ThetaDev 2023-03-18 16:28:09 +01:00
parent dba53d23aa
commit 77649d388c
2 changed files with 50 additions and 48 deletions

View File

@ -1236,50 +1236,10 @@ public abstract class VideoPlayerUi extends PlayerUi implements SeekBar.OnSeekBa
}
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();
@ -1292,6 +1252,53 @@ public abstract class VideoPlayerUi extends PlayerUi implements SeekBar.OnSeekBa
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.
*/

View File

@ -212,12 +212,7 @@ public final class ListHelper {
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) {