feat: improve audio track sorting, add prefer_descriptive_audio option

This commit is contained in:
ThetaDev 2023-03-18 14:50:19 +01:00
parent de7872d8f2
commit 208887d538
4 changed files with 39 additions and 25 deletions

View File

@ -233,33 +233,16 @@ public final class ListHelper {
// Fall back to English if the preferred language was not found
final String preferredLanguageOrEnglish =
hasPreferredLanguage ? preferredLanguage : Locale.ENGLISH.getISO3Language();
final SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(context);
final boolean preferDescriptiveAudio =
preferences.getBoolean(context.getString(R.string.prefer_descriptive_audio_key),
false);
final Comparator<AudioStream> trackCmp =
getAudioTrackComparator(preferredLanguageOrEnglish, preferDescriptiveAudio);
// Sort collected streams
return collectedStreams.values().stream()
.sorted((s1, s2) -> {
// Preferred language comes first
if (s1.getAudioLocale() != null
&& s1.getAudioLocale().getISO3Language()
.equals(preferredLanguageOrEnglish)) {
return -1;
}
if (s2.getAudioLocale() != null
&& s2.getAudioLocale().getISO3Language()
.equals(preferredLanguageOrEnglish)) {
return 1;
}
// Sort audio tracks alphabetically
if (s1.getAudioTrackName() != null) {
if (s2.getAudioTrackName() != null) {
return s1.getAudioTrackName().compareTo(s2.getAudioTrackName());
} else {
return -1;
}
}
return 1;
})
.collect(Collectors.toList());
return collectedStreams.values().stream().sorted(trackCmp).collect(Collectors.toList());
}
/*//////////////////////////////////////////////////////////////////////////
@ -680,4 +663,24 @@ public final class ListHelper {
return manager.isActiveNetworkMetered();
}
private static Comparator<AudioStream> getAudioTrackComparator(
final String preferredLanguage, final boolean preferDescriptiveAudio) {
return Comparator.comparing(AudioStream::getAudioLocale, (o1, o2) -> Boolean.compare(
o1 == null || !o1.getISO3Language().equals(preferredLanguage),
o2 == null || !o2.getISO3Language().equals(preferredLanguage))
).thenComparing(AudioStream::isDescriptive, (o1, o2) ->
Boolean.compare(o1 ^ preferDescriptiveAudio, o2 ^ preferDescriptiveAudio)
).thenComparing(AudioStream::getAudioTrackName, (o1, o2) -> {
if (o1 != null) {
if (o2 != null) {
return o1.compareTo(o2);
} else {
return -1;
}
} else {
return 1;
}
});
}
}

View File

@ -260,6 +260,7 @@
<string name="show_next_video_key">show_next_video</string>
<string name="show_description_key">show_description</string>
<string name="show_meta_info_key">show_meta_info</string>
<string name="prefer_descriptive_audio_key">prefer_descriptive_audio</string>
<string name="stream_info_selected_tab_key">stream_info_selected_tab</string>
<string name="show_hold_to_append_key">show_hold_to_append</string>
<string name="content_language_key">content_language</string>

View File

@ -94,6 +94,8 @@
<string name="show_description_summary">Turn off to hide video description and additional information</string>
<string name="show_meta_info_title">Show meta info</string>
<string name="show_meta_info_summary">Turn off to hide meta info boxes with additional information about the stream creator, stream content or a search request</string>
<string name="prefer_descriptive_audio_title">Prefer descriptive audio</string>
<string name="prefer_descriptive_audio_summary">Select an audio track with descriptions for visually impaired people if available</string>
<string name="thumbnail_cache_wipe_complete_notice">Image cache wiped</string>
<string name="metadata_cache_wipe_title">Wipe cached metadata</string>
<string name="metadata_cache_wipe_summary">Remove all cached webpage data</string>

View File

@ -114,6 +114,14 @@
app:singleLineTitle="false"
app:iconSpaceReserved="false" />
<SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/prefer_descriptive_audio_key"
android:summary="@string/prefer_descriptive_audio_summary"
android:title="@string/prefer_descriptive_audio_title"
app:singleLineTitle="false"
app:iconSpaceReserved="false"/>
<Preference
android:key="@string/import_data"
android:summary="@string/import_data_summary"