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

Commit

Permalink
refactor: Rename and refactor
Browse files Browse the repository at this point in the history
SortStyle -> Sort (The enum values have also been adjusted
Minimal code changes have been applied to integrations as well
  • Loading branch information
oSumAtrIX committed Feb 22, 2024
1 parent 28bf153 commit a65ee82
Showing 1 changed file with 35 additions and 32 deletions.
67 changes: 35 additions & 32 deletions app/src/main/java/app/revanced/integrations/shared/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
import androidx.annotation.Nullable;

import java.text.Bidi;
import java.util.Locale;
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
Expand Down Expand Up @@ -404,39 +401,41 @@ public static void hideViewByLayoutParams(View view) {
/**
* {@link PreferenceScreen} and {@link PreferenceGroup} sorting styles.
*/
private enum SortStyle {
private enum Sort {
/**
* Sort alphabetically by the localized title.
* Sort by the localized preference title.
*/
TITLE("_sort_title"),
BY_TITLE("_sort_by_title"),

/**
* Sort by preference key.
* Sort by the preference keys.
*/
KEY("_sort_key"),
BY_KEY("_sort_by_key"),

/**
* Keep everything unsorted, and retain the original preference order created during patching.
* Unspecified sorting.
*/
UNSORTED("_sort_ignore");
UNSORTED("_sort_by_unsorted");

final String suffix;
final String keySuffix;

SortStyle(String suffix) {
this.suffix = suffix;
Sort(String keySuffix) {
this.keySuffix = keySuffix;
}

/**
* Defaults to {@link #TITLE} if key is null or has no sort suffix.
* Defaults to {@link #BY_TITLE} if key is null or has no sort suffix.
*/
@NonNull
static SortStyle sortForSuffix(@Nullable String key) {
static Sort fromKey(@Nullable String key) {
if (key != null) {
for (SortStyle sort : values()) {
if (key.endsWith(sort.suffix)) {
for (Sort sort : values()) {
if (key.endsWith(sort.keySuffix)) {
return sort;
}
}
}
return TITLE;
return BY_TITLE;
}
}

Expand All @@ -453,45 +452,49 @@ public static String removePunctuationConvertToLowercase(@Nullable CharSequence
/**
* Sort a PreferenceGroup and all it's sub groups by title or key.
*
* Sort order is determined by the preference key SortStyle suffix.
* Sort order is determined by the preferences key {@link Sort} suffix.
*
* If a preference has no key or no SortStyle suffix,
* If a preference has no key or no {@link Sort} suffix,
* then the preferences are sorted by the localized title.
*/
public static void sortPreferenceGroups(@NonNull 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) {
sortPreferenceGroups((PreferenceGroup) preference);
}

final String sortValue;
switch (sortToUse) {
case TITLE:
switch (Sort.fromKey(group.getKey())) {
case BY_TITLE:
sortValue = removePunctuationConvertToLowercase(preference.getTitle());
break;
case KEY:
case BY_KEY:
sortValue = preference.getKey();
break;
case UNSORTED:
continue; // Keep original sorting.
default:
throw new IllegalStateException();
}

preferences.put(sortValue, preference);
}

int prefIndex = 0;
int index = 0;
for (Preference pref : preferences.values()) {
int indexToSet = prefIndex++;
int order = index++;

// If the preference is a PreferenceScreen or is an intent preference, move to the top.
if (pref instanceof PreferenceScreen || pref.getIntent() != null) {
// Place sub menus first
// Use an offset to pull to the top.
indexToSet -= 1000;
// Arbitrary high number.
order -= 1000;
}
pref.setOrder(indexToSet);

pref.setOrder(order);
}
}
}

0 comments on commit a65ee82

Please sign in to comment.