From 0f35dcb0081084515fb2e45445f597879650fe11 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sun, 18 Feb 2024 18:36:08 +0400 Subject: [PATCH] refactor: Use preference key to indicate the sorting style for Integrations to use --- .../revanced/integrations/shared/Utils.java | 100 +++++++++++++----- .../AbstractPreferenceFragment.java | 2 +- 2 files changed, 76 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/shared/Utils.java b/app/src/main/java/app/revanced/integrations/shared/Utils.java index 96318a9fec..6ec808faa8 100644 --- a/app/src/main/java/app/revanced/integrations/shared/Utils.java +++ b/app/src/main/java/app/revanced/integrations/shared/Utils.java @@ -12,6 +12,7 @@ import android.os.Looper; import android.preference.Preference; import android.preference.PreferenceGroup; +import android.preference.PreferenceScreen; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; @@ -347,6 +348,12 @@ public static void verifyOffMainThread() throws IllegalStateException { } } + public enum NetworkType { + NONE, + MOBILE, + OTHER, + } + public static boolean isNetworkConnected() { NetworkType networkType = getNetworkType(); return networkType == NetworkType.MOBILE @@ -395,29 +402,82 @@ public static void hideViewByLayoutParams(View view) { } } + /** + * PreferenceScreen and Group sorting styles. + */ + private enum SortStyle { + /** + * Sort alphabetically by the localized title. + */ + TITLE("_sort_title"), + /** + * Sort by preference key. + */ + KEY("_sort_key"), + /** + * Leaves everything unsorted, and retain the order created during patching. + */ + UNSORTED("_sort_ignore"); + + final String suffix; + + SortStyle(String suffix) { + this.suffix = suffix; + } + + /** + * Defaults to {@link #TITLE} if the key does not match. + */ + @NonNull + static SortStyle sortForSuffix(@Nullable String key) { + if (key != null) { + for (SortStyle sort : values()) { + if (key.endsWith(sort.suffix)) { + return sort; + } + } + } + return TITLE; + } + } + private static final Regex punctuationRegex = new Regex("\\p{P}+"); /** - * Sort the preferences by preference key and/or title and ignore text casing. + * Strips all punctionation and converts to lower case. A null parameter returns an empty string. + */ + public static String removePunctuationConvertToLowercase(@Nullable CharSequence original) { + if (original == null) return ""; + return punctuationRegex.replace(original, "").toLowerCase(); + } + + /** + * Sort the a PreferenceGroup based by either title, by key, or to retain the original order, + * based on the suffix of the key. * - * @param menuDepthToSortByKey Menu depth to sort using the preference key. Index 1 is the first level. - * Menus deeper than this value are sorted by localized title. - * @param menuDepthToSort Maximum menu depth to sort by localized title. - * Menus deeper than this value will show preferences in the order created during patching. + * If no suffix key is present, then the preferences are sorted by title. */ - public static void sortPreferenceGroupByTitle(PreferenceGroup group, int menuDepthToSortByKey, int menuDepthToSort) { - if (menuDepthToSort == 0) return; + public static void sortPreferenceGroups(PreferenceGroup group) { + SortStyle sortToUse = SortStyle.sortForSuffix(group.getKey()); SortedMap preferences = new TreeMap<>(); for (int i = 0, prefCount = group.getPreferenceCount(); i < prefCount; i++) { Preference preference = group.getPreference(i); if (preference instanceof PreferenceGroup) { - sortPreferenceGroupByTitle((PreferenceGroup) preference, - menuDepthToSortByKey - 1, menuDepthToSort - 1); + sortPreferenceGroups((PreferenceGroup) preference); } - String sortValue = preference.getKey(); - if (sortValue == null || menuDepthToSortByKey <= 0) { - sortValue = removePunctuationConvertToLowercase(preference.getTitle()); + final String sortValue; + switch (sortToUse) { + case TITLE: + sortValue = removePunctuationConvertToLowercase(preference.getTitle()); + break; + case KEY: + sortValue = preference.getKey(); + break; + case UNSORTED: + continue; // Keep original sorting. + default: + throw new IllegalStateException(); } preferences.put(sortValue, preference); } @@ -425,22 +485,12 @@ public static void sortPreferenceGroupByTitle(PreferenceGroup group, int menuDep int prefIndex = 0; for (Preference pref : preferences.values()) { int indexToSet = prefIndex++; - if (pref instanceof PreferenceGroup || pref.getIntent() != null) { - // Place preference groups first. - // Use an offset to push the group to the start. + if (pref instanceof PreferenceScreen || pref.getIntent() != null) { + // Place sub menus first + // Use an offset to pull to the top. indexToSet -= 1000; } pref.setOrder(indexToSet); } } - - public static String removePunctuationConvertToLowercase(CharSequence original) { - return punctuationRegex.replace(original, "").toLowerCase(); - } - - public enum NetworkType { - NONE, - MOBILE, - OTHER, - } } diff --git a/app/src/main/java/app/revanced/integrations/shared/settings/preference/AbstractPreferenceFragment.java b/app/src/main/java/app/revanced/integrations/shared/settings/preference/AbstractPreferenceFragment.java index 6e078f9133..dc1fcbd934 100644 --- a/app/src/main/java/app/revanced/integrations/shared/settings/preference/AbstractPreferenceFragment.java +++ b/app/src/main/java/app/revanced/integrations/shared/settings/preference/AbstractPreferenceFragment.java @@ -85,7 +85,7 @@ protected void initialize() { if (identifier == 0) return; addPreferencesFromResource(identifier); - Utils.sortPreferenceGroupByTitle(getPreferenceScreen(), 1, 2); + Utils.sortPreferenceGroups(getPreferenceScreen()); } private void showSettingUserDialogConfirmation(SwitchPreference switchPref, BooleanSetting setting) {