mirror of
https://github.com/revanced/revanced-patches.git
synced 2025-06-12 05:07:45 +02:00
feat(YouTube - Settings): Add ability to search in settings (#4881)
Co-authored-by: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com>
This commit is contained in:
@ -371,7 +371,7 @@ public class Utils {
|
||||
if (language != AppLanguage.DEFAULT) {
|
||||
// Create a new context with the desired language.
|
||||
Logger.printDebug(() -> "Using app language: " + language);
|
||||
Configuration config = appContext.getResources().getConfiguration();
|
||||
Configuration config = new Configuration(appContext.getResources().getConfiguration());
|
||||
config.setLocale(language.getLocale());
|
||||
context = appContext.createConfigurationContext(config);
|
||||
}
|
||||
@ -391,16 +391,47 @@ public class Utils {
|
||||
private static Boolean isRightToLeftTextLayout;
|
||||
|
||||
/**
|
||||
* If the device language uses right to left text layout (hebrew, arabic, etc)
|
||||
* @return If the device language uses right to left text layout (Hebrew, Arabic, etc).
|
||||
* If this should match any ReVanced language override then instead use
|
||||
* {@link #isRightToLeftLocale(Locale)} with {@link BaseSettings#REVANCED_LANGUAGE}.
|
||||
* This is the default locale of the device, which may differ if
|
||||
* {@link BaseSettings#REVANCED_LANGUAGE} is set to a different language.
|
||||
*/
|
||||
public static boolean isRightToLeftTextLayout() {
|
||||
public static boolean isRightToLeftLocale() {
|
||||
if (isRightToLeftTextLayout == null) {
|
||||
String displayLanguage = Locale.getDefault().getDisplayLanguage();
|
||||
isRightToLeftTextLayout = new Bidi(displayLanguage, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT).isRightToLeft();
|
||||
isRightToLeftTextLayout = isRightToLeftLocale(Locale.getDefault());
|
||||
}
|
||||
return isRightToLeftTextLayout;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return If the locale uses right to left text layout (Hebrew, Arabic, etc).
|
||||
*/
|
||||
public static boolean isRightToLeftLocale(Locale locale) {
|
||||
String displayLanguage = locale.getDisplayLanguage();
|
||||
return new Bidi(displayLanguage, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT).isRightToLeft();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A UTF8 string containing a left-to-right or right-to-left
|
||||
* character of the device locale. If this should match any ReVanced language
|
||||
* override then instead use {@link #getTextDirectionString(Locale)} with
|
||||
* {@link BaseSettings#REVANCED_LANGUAGE}.
|
||||
*/
|
||||
public static String getTextDirectionString() {
|
||||
return getTextDirectionString(isRightToLeftLocale());
|
||||
}
|
||||
|
||||
public static String getTextDirectionString(Locale locale) {
|
||||
return getTextDirectionString(isRightToLeftLocale(locale));
|
||||
}
|
||||
|
||||
private static String getTextDirectionString(boolean isRightToLeft) {
|
||||
return isRightToLeft
|
||||
? "\u200F" // u200F = right to left character.
|
||||
: "\u200E"; // u200E = left to right character.
|
||||
}
|
||||
|
||||
/**
|
||||
* @return if the text contains at least 1 number character,
|
||||
* including any unicode numbers such as Arabic.
|
||||
@ -692,9 +723,10 @@ public class Utils {
|
||||
/**
|
||||
* Strips all punctuation and converts to lower case. A null parameter returns an empty string.
|
||||
*/
|
||||
public static String removePunctuationConvertToLowercase(@Nullable CharSequence original) {
|
||||
public static String removePunctuationToLowercase(@Nullable CharSequence original) {
|
||||
if (original == null) return "";
|
||||
return punctuationPattern.matcher(original).replaceAll("").toLowerCase();
|
||||
return punctuationPattern.matcher(original).replaceAll("")
|
||||
.toLowerCase(BaseSettings.REVANCED_LANGUAGE.get().getLocale());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -726,7 +758,7 @@ public class Utils {
|
||||
final String sortValue;
|
||||
switch (preferenceSort) {
|
||||
case BY_TITLE:
|
||||
sortValue = removePunctuationConvertToLowercase(preference.getTitle());
|
||||
sortValue = removePunctuationToLowercase(preference.getTitle());
|
||||
break;
|
||||
case BY_KEY:
|
||||
sortValue = preference.getKey();
|
||||
@ -810,4 +842,12 @@ public class Utils {
|
||||
}
|
||||
return getResourceColor(colorString);
|
||||
}
|
||||
|
||||
public static int clamp(int value, int lower, int upper) {
|
||||
return Math.max(lower, Math.min(value, upper));
|
||||
}
|
||||
|
||||
public static float clamp(float value, float lower, float upper) {
|
||||
return Math.max(lower, Math.min(value, upper));
|
||||
}
|
||||
}
|
||||
|
@ -89,9 +89,11 @@ public enum AppLanguage {
|
||||
ZU;
|
||||
|
||||
private final String language;
|
||||
private final Locale locale;
|
||||
|
||||
AppLanguage() {
|
||||
language = name().toLowerCase(Locale.US);
|
||||
locale = Locale.forLanguageTag(language);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,6 +114,6 @@ public enum AppLanguage {
|
||||
return Locale.getDefault();
|
||||
}
|
||||
|
||||
return Locale.forLanguageTag(language);
|
||||
return locale;
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,10 @@ public class NoTitlePreferenceCategory extends PreferenceCategory {
|
||||
super(context, attrs, defStyleAttr);
|
||||
}
|
||||
|
||||
public NoTitlePreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
}
|
||||
|
||||
public NoTitlePreferenceCategory(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
@ -0,0 +1,107 @@
|
||||
package app.revanced.extension.shared.settings.preference;
|
||||
|
||||
import android.content.Context;
|
||||
import android.preference.ListPreference;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Pair;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import app.revanced.extension.shared.Utils;
|
||||
|
||||
/**
|
||||
* PreferenceList that sorts itself.
|
||||
* By default the first entry is preserved in its original position,
|
||||
* and all other entries are sorted alphabetically.
|
||||
*
|
||||
* Ideally the 'keep first entries to preserve' is an xml parameter,
|
||||
* but currently that's not so simple since Extensions code cannot use
|
||||
* generated code from the Patches repo (which is required for custom xml parameters).
|
||||
*
|
||||
* If any class wants to use a different getFirstEntriesToPreserve value,
|
||||
* it needs to subclass this preference and override {@link #getFirstEntriesToPreserve}.
|
||||
*/
|
||||
@SuppressWarnings({"unused", "deprecation"})
|
||||
public class SortedListPreference extends ListPreference {
|
||||
|
||||
/**
|
||||
* Sorts the current list entries.
|
||||
*
|
||||
* @param firstEntriesToPreserve The number of entries to preserve in their original position.
|
||||
*/
|
||||
public void sortEntryAndValues(int firstEntriesToPreserve) {
|
||||
CharSequence[] entries = getEntries();
|
||||
CharSequence[] entryValues = getEntryValues();
|
||||
if (entries == null || entryValues == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final int entrySize = entries.length;
|
||||
if (entrySize != entryValues.length) {
|
||||
// Xml array declaration has a missing/extra entry.
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
List<Pair<CharSequence, CharSequence>> firstEntries = new ArrayList<>(firstEntriesToPreserve);
|
||||
SortedMap<String, Pair<CharSequence, CharSequence>> lastEntries = new TreeMap<>();
|
||||
|
||||
for (int i = 0; i < entrySize; i++) {
|
||||
Pair<CharSequence, CharSequence> pair = new Pair<>(entries[i], entryValues[i]);
|
||||
if (i < firstEntriesToPreserve) {
|
||||
firstEntries.add(pair);
|
||||
} else {
|
||||
lastEntries.put(Utils.removePunctuationToLowercase(pair.first), pair);
|
||||
}
|
||||
}
|
||||
|
||||
CharSequence[] sortedEntries = new CharSequence[entrySize];
|
||||
CharSequence[] sortedEntryValues = new CharSequence[entrySize];
|
||||
|
||||
int i = 0;
|
||||
for (Pair<CharSequence, CharSequence> pair : firstEntries) {
|
||||
sortedEntries[i] = pair.first;
|
||||
sortedEntryValues[i] = pair.second;
|
||||
i++;
|
||||
}
|
||||
|
||||
for (Pair<CharSequence, CharSequence> pair : lastEntries.values()) {
|
||||
sortedEntries[i] = pair.first;
|
||||
sortedEntryValues[i] = pair.second;
|
||||
i++;
|
||||
}
|
||||
|
||||
super.setEntries(sortedEntries);
|
||||
super.setEntryValues(sortedEntryValues);
|
||||
}
|
||||
|
||||
protected int getFirstEntriesToPreserve() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
public SortedListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
|
||||
sortEntryAndValues(getFirstEntriesToPreserve());
|
||||
}
|
||||
|
||||
public SortedListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
|
||||
sortEntryAndValues(getFirstEntriesToPreserve());
|
||||
}
|
||||
|
||||
public SortedListPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
|
||||
sortEntryAndValues(getFirstEntriesToPreserve());
|
||||
}
|
||||
|
||||
public SortedListPreference(Context context) {
|
||||
super(context);
|
||||
|
||||
sortEntryAndValues(getFirstEntriesToPreserve());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user