Local feeds: add sort option for file name (#5629)

Co-authored-by: widlok <widlok@users.noreply.github.com>
This commit is contained in:
Shom Bandopadhaya 2022-01-04 10:26:59 +00:00 committed by GitHub
parent 4f031c4e45
commit 8568226468
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 86 additions and 22 deletions

View File

@ -16,35 +16,52 @@ public abstract class IntraFeedSortDialog {
@NonNull
protected Context context;
public IntraFeedSortDialog(@NonNull Context context, @Nullable SortOrder sortOrder) {
private final String[] sortItems;
private final SortOrder[] sortValues;
public IntraFeedSortDialog(@NonNull Context context, @Nullable SortOrder sortOrder, @NonNull boolean isLocalFeed) {
this.context = context;
this.currentSortOrder = sortOrder;
if (isLocalFeed) {
sortItems = context.getResources().getStringArray(R.array.local_feed_episodes_sort_options);
final String[] localSortStringValues =
context.getResources().getStringArray(R.array.local_feed_episodes_sort_values);
sortValues = SortOrder.valuesOf(localSortStringValues);
} else {
sortItems = context.getResources().getStringArray(R.array.feed_episodes_sort_options);
final String[] commonSortStringValues =
context.getResources().getStringArray(R.array.feed_episodes_sort_values);
sortValues = SortOrder.valuesOf(commonSortStringValues);
}
}
public void openDialog() {
final String[] items = context.getResources().getStringArray(R.array.feed_episodes_sort_options);
final String[] valueStrs = context.getResources().getStringArray(R.array.feed_episodes_sort_values);
final SortOrder[] values = new SortOrder[valueStrs.length];
for (int i = 0; i < valueStrs.length; i++) {
values[i] = SortOrder.valueOf(valueStrs[i]);
}
int idxCurrentSort = getCurrentSortOrderIndex();
int idxCurrentSort = 0;
for (int i = 0; i < values.length; i++) {
if (currentSortOrder == values[i]) {
idxCurrentSort = i;
break;
AlertDialog.Builder builder =
new AlertDialog.Builder(context)
.setTitle(R.string.sort)
.setSingleChoiceItems(sortItems, idxCurrentSort, (dialog, idxNewSort) -> {
updateSort(sortValues[idxNewSort]);
dialog.dismiss();
})
.setNegativeButton(R.string.cancel_label, null);
builder.create().show();
}
/**
* Retrieves index of currentSortOrder index in values array.
* @return if currentSortOrder is found in array - returns index of that element,
* otherwise returns 0, the default sort option;
*/
private int getCurrentSortOrderIndex() {
for (int i = 0; i < sortValues.length; i++) {
if (currentSortOrder == sortValues[i]) {
return i;
}
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.sort);
builder.setSingleChoiceItems(items, idxCurrentSort, (dialog, idxNewSort) -> {
updateSort(values[idxNewSort]);
dialog.dismiss();
});
builder.setNegativeButton(R.string.cancel_label, null);
builder.create().show();
return 0;
}
protected abstract void updateSort(@NonNull SortOrder sortOrder);

View File

@ -108,7 +108,7 @@ public class FeedMenuHandler {
private static void showSortDialog(Context context, Feed selectedFeed) {
IntraFeedSortDialog sortDialog = new IntraFeedSortDialog(context, selectedFeed.getSortOrder()) {
IntraFeedSortDialog sortDialog = new IntraFeedSortDialog(context, selectedFeed.getSortOrder(), selectedFeed.isLocalFeed()) {
@Override
protected void updateSort(@NonNull SortOrder sortOrder) {
selectedFeed.setSortOrder(sortOrder);

View File

@ -50,6 +50,12 @@ public class FeedItemPermutors {
case DURATION_LONG_SHORT:
comparator = (f1, f2) -> Integer.compare(duration(f2), duration(f1));
break;
case EPISODE_FILENAME_A_Z:
comparator = (f1, f2) -> itemLink(f1).compareTo(itemLink(f2));
break;
case EPISODE_FILENAME_Z_A:
comparator = (f1, f2) -> itemLink(f2).compareTo(itemLink(f1));
break;
case FEED_TITLE_A_Z:
comparator = (f1, f2) -> feedTitle(f1).compareTo(feedTitle(f2));
break;
@ -90,6 +96,12 @@ public class FeedItemPermutors {
return (item != null && item.getMedia() != null) ? item.getMedia().getDuration() : 0;
}
@NonNull
private static String itemLink(@Nullable FeedItem item) {
return (item != null && item.getLink() != null)
? item.getLink().toLowerCase(Locale.getDefault()) : "";
}
@NonNull
private static String feedTitle(@Nullable FeedItem item) {
return (item != null && item.getFeed() != null && item.getFeed().getTitle() != null)

View File

@ -209,6 +209,18 @@
<item>@string/sort_duration_long_short</item>
</string-array>
<!-- sort for local feed screen -->
<string-array name="local_feed_episodes_sort_options">
<item>@string/sort_date_new_old</item>
<item>@string/sort_date_old_new</item>
<item>@string/sort_title_a_z</item>
<item>@string/sort_title_z_a</item>
<item>@string/sort_duration_short_long</item>
<item>@string/sort_duration_long_short</item>
<item>@string/sort_filename_a_z</item>
<item>@string/sort_filename_z_a</item>
</string-array>
<string-array name="feed_episodes_sort_values">
<item>DATE_NEW_OLD</item>
<item>DATE_OLD_NEW</item>
@ -218,6 +230,17 @@
<item>DURATION_LONG_SHORT</item>
</string-array>
<string-array name="local_feed_episodes_sort_values">
<item>DATE_NEW_OLD</item>
<item>DATE_OLD_NEW</item>
<item>EPISODE_TITLE_A_Z</item>
<item>EPISODE_TITLE_Z_A</item>
<item>DURATION_SHORT_LONG</item>
<item>DURATION_LONG_SHORT</item>
<item>EPISODE_FILENAME_A_Z</item>
<item>EPISODE_FILENAME_Z_A</item>
</string-array>
<string-array name="compact_notification_buttons_options">
<item>@string/rewind_label</item>
<item>@string/fast_forward_label</item>

View File

@ -761,6 +761,8 @@
<string name="sort_date_old_new">Date (Old \u2192 New)</string>
<string name="sort_duration_short_long">Duration (Short \u2192 Long)</string>
<string name="sort_duration_long_short">Duration (Long \u2192 Short)</string>
<string name="sort_filename_a_z">File Name (A \u2192 Z)</string>
<string name="sort_filename_z_a">File Name (Z \u2192 A)</string>
<string name="sort_a_z">A \u2192 Z</string>
<string name="sort_z_a">Z \u2192 A</string>

View File

@ -18,6 +18,8 @@ public enum SortOrder {
EPISODE_TITLE_Z_A(4, INTRA_FEED),
DURATION_SHORT_LONG(5, INTRA_FEED),
DURATION_LONG_SHORT(6, INTRA_FEED),
EPISODE_FILENAME_A_Z(7, INTRA_FEED),
EPISODE_FILENAME_Z_A(8, INTRA_FEED),
FEED_TITLE_A_Z(101, INTER_FEED),
FEED_TITLE_Z_A(102, INTER_FEED),
RANDOM(103, INTER_FEED),
@ -68,4 +70,12 @@ public enum SortOrder {
public static String toCodeString(@Nullable SortOrder sortOrder) {
return sortOrder != null ? Integer.toString(sortOrder.code) : null;
}
public static SortOrder[] valuesOf(String[] stringValues) {
SortOrder[] values = new SortOrder[stringValues.length];
for (int i = 0; i < stringValues.length; i++) {
values[i] = SortOrder.valueOf(stringValues[i]);
}
return values;
}
}