fix(YouTube): Better handle incorrect duplicate translations

This commit is contained in:
LisoUseInAIKyrios 2025-05-22 21:20:01 +02:00
parent d519a8c81f
commit 20abac5212
2 changed files with 29 additions and 13 deletions

View File

@ -1,7 +1,11 @@
package app.revanced.extension.shared; package app.revanced.extension.shared;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.*; import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
@ -18,6 +22,7 @@ import android.os.Looper;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceGroup; import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.util.Pair;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewParent; import android.view.ViewParent;
@ -738,9 +743,9 @@ public class Utils {
* then the preferences are left unsorted. * then the preferences are left unsorted.
*/ */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public static void sortPreferenceGroups(@NonNull PreferenceGroup group) { public static void sortPreferenceGroups(PreferenceGroup group) {
Sort groupSort = Sort.fromKey(group.getKey(), Sort.UNSORTED); Sort groupSort = Sort.fromKey(group.getKey(), Sort.UNSORTED);
SortedMap<String, Preference> preferences = new TreeMap<>(); List<Pair<String, Preference>> preferences = new ArrayList<>();
for (int i = 0, prefCount = group.getPreferenceCount(); i < prefCount; i++) { for (int i = 0, prefCount = group.getPreferenceCount(); i < prefCount; i++) {
Preference preference = group.getPreference(i); Preference preference = group.getPreference(i);
@ -769,17 +774,21 @@ public class Utils {
throw new IllegalStateException(); throw new IllegalStateException();
} }
preferences.put(sortValue, preference); preferences.add(new Pair<>(sortValue, preference));
} }
Collections.sort(preferences, (pair1, pair2)
-> pair1.first.compareToIgnoreCase(pair2.first));
int index = 0; int index = 0;
for (Preference pref : preferences.values()) { for (Pair<String, Preference> pair : preferences) {
int order = index++; int order = index++;
Preference pref = pair.second;
// Move any screens, intents, and the one off About preference to the top. // Move any screens, intents, and the one off About preference to the top.
if (pref instanceof PreferenceScreen || pref instanceof ReVancedAboutPreference if (pref instanceof PreferenceScreen || pref instanceof ReVancedAboutPreference
|| pref.getIntent() != null) { || pref.getIntent() != null) {
// Arbitrary high number. // Any arbitrary large number.
order -= 1000; order -= 1000;
} }

View File

@ -6,9 +6,8 @@ import android.util.AttributeSet;
import android.util.Pair; import android.util.Pair;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import app.revanced.extension.shared.Utils; import app.revanced.extension.shared.Utils;
@ -46,17 +45,24 @@ public class SortedListPreference extends ListPreference {
} }
List<Pair<CharSequence, CharSequence>> firstEntries = new ArrayList<>(firstEntriesToPreserve); List<Pair<CharSequence, CharSequence>> firstEntries = new ArrayList<>(firstEntriesToPreserve);
SortedMap<String, Pair<CharSequence, CharSequence>> lastEntries = new TreeMap<>();
// Android does not have a triple class like Kotlin, So instead use a nested pair.
// Cannot easily use a SortedMap, because if two entries incorrectly have
// identical names then the duplicates entries are not preserved.
List<Pair<String, Pair<CharSequence, CharSequence>>> lastEntries = new ArrayList<>();
for (int i = 0; i < entrySize; i++) { for (int i = 0; i < entrySize; i++) {
Pair<CharSequence, CharSequence> pair = new Pair<>(entries[i], entryValues[i]); Pair<CharSequence, CharSequence> pair = new Pair<>(entries[i], entryValues[i]);
if (i < firstEntriesToPreserve) { if (i < firstEntriesToPreserve) {
firstEntries.add(pair); firstEntries.add(pair);
} else { } else {
lastEntries.put(Utils.removePunctuationToLowercase(pair.first), pair); lastEntries.add(new Pair<>(Utils.removePunctuationToLowercase(pair.first), pair));
} }
} }
Collections.sort(lastEntries, (pair1, pair2)
-> pair1.first.compareToIgnoreCase(pair2.first));
CharSequence[] sortedEntries = new CharSequence[entrySize]; CharSequence[] sortedEntries = new CharSequence[entrySize];
CharSequence[] sortedEntryValues = new CharSequence[entrySize]; CharSequence[] sortedEntryValues = new CharSequence[entrySize];
@ -67,9 +73,10 @@ public class SortedListPreference extends ListPreference {
i++; i++;
} }
for (Pair<CharSequence, CharSequence> pair : lastEntries.values()) { for (Pair<String, Pair<CharSequence, CharSequence>> outer : lastEntries) {
sortedEntries[i] = pair.first; Pair<CharSequence, CharSequence> inner = outer.second;
sortedEntryValues[i] = pair.second; sortedEntries[i] = inner.first;
sortedEntryValues[i] = inner.second;
i++; i++;
} }