Skip to content
This repository has been archived by the owner on Oct 26, 2024. It is now read-only.

Commit

Permalink
refactor: Use preference key to indicate the sorting style for Integr…
Browse files Browse the repository at this point in the history
…ations to use
  • Loading branch information
LisoUseInAIKyrios committed Feb 18, 2024
1 parent 9d2595b commit 0f35dcb
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 26 deletions.
100 changes: 75 additions & 25 deletions app/src/main/java/app/revanced/integrations/shared/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -395,52 +402,95 @@ 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<String, Preference> 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);
}

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,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0f35dcb

Please sign in to comment.