Added sort folders on unread option

pull/205/head
M66B 4 years ago
parent 28b30a874b
commit 137e6859df

@ -72,6 +72,7 @@ import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@ -86,6 +87,7 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
private boolean show_hidden;
private boolean show_flagged;
private boolean subscribed_only;
private boolean sort_unread_atop;
private IFolderSelectedListener listener;
private Context context;
@ -1128,6 +1130,7 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
this.subscriptions = prefs.getBoolean("subscriptions", false);
this.subscribed_only = prefs.getBoolean("subscribed_only", false) && subscriptions;
this.sort_unread_atop = prefs.getBoolean("sort_unread_atop", false);
this.dp12 = Helper.dp2pixels(context, 12);
this.textSize = Helper.getTextSize(context, zoom);
@ -1174,6 +1177,13 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
}
}
void setSortUnreadAtop(boolean sort_unread_atop) {
if (this.sort_unread_atop != sort_unread_atop) {
this.sort_unread_atop = sort_unread_atop;
set(all);
}
}
void setDisabled(List<Long> ids) {
disabledIds = ids;
}
@ -1187,6 +1197,14 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
if (folders.size() > 0)
Collections.sort(folders, folders.get(0).getComparator(context));
hierarchical = folders;
if (sort_unread_atop)
Collections.sort(hierarchical, new Comparator<TupleFolderEx>() {
@Override
public int compare(TupleFolderEx f1, TupleFolderEx f2) {
return -Boolean.compare(f1.unseen > 0, f2.unseen > 0);
}
});
} else {
List<TupleFolderEx> parents = new ArrayList<>();
Map<Long, TupleFolderEx> idFolder = new HashMap<>();
@ -1235,7 +1253,7 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
}
}
hierarchical = getHierarchical(parents, anyChild ? 0 : 1);
hierarchical = getHierarchical(parents, anyChild ? 0 : 1, sort_unread_atop);
}
List<TupleFolderEx> items;
@ -1325,19 +1343,27 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
void onNotFound();
}
private List<TupleFolderEx> getHierarchical(List<TupleFolderEx> parents, int indentation) {
private List<TupleFolderEx> getHierarchical(List<TupleFolderEx> parents, int indentation, boolean sort_unread_atop) {
List<TupleFolderEx> result = new ArrayList<>();
if (parents.size() > 0)
Collections.sort(parents, parents.get(0).getComparator(context));
if (sort_unread_atop)
Collections.sort(parents, new Comparator<TupleFolderEx>() {
@Override
public int compare(TupleFolderEx f1, TupleFolderEx f2) {
return -Boolean.compare(f1.unseen > 0, f2.unseen > 0);
}
});
for (TupleFolderEx parent : parents) {
if (parent.hide && !show_hidden)
continue;
List<TupleFolderEx> childs = null;
if (parent.child_refs != null) {
childs = getHierarchical(parent.child_refs, indentation + 1);
childs = getHierarchical(parent.child_refs, indentation + 1, sort_unread_atop);
for (TupleFolderEx child : childs) {
parent.childs_unseen += child.unseen;
if (child.collapsed)

@ -527,6 +527,7 @@ public class FragmentFolders extends FragmentBase {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean subscriptions = prefs.getBoolean("subscriptions", false);
boolean subscribed_only = prefs.getBoolean("subscribed_only", false);
boolean sort_unread_atop = prefs.getBoolean("sort_unread_atop", false);
menu.findItem(R.id.menu_unified).setVisible(account < 0 || primary);
menu.findItem(R.id.menu_theme).setVisible(account < 0 || primary);
@ -535,6 +536,7 @@ public class FragmentFolders extends FragmentBase {
menu.findItem(R.id.menu_show_flagged).setChecked(show_flagged);
menu.findItem(R.id.menu_subscribed_only).setChecked(subscribed_only);
menu.findItem(R.id.menu_subscribed_only).setVisible(subscriptions);
menu.findItem(R.id.menu_sort_unread_atop).setChecked(sort_unread_atop);
menu.findItem(R.id.menu_apply_all).setVisible(account >= 0 && imap);
super.onPrepareOptionsMenu(menu);
@ -564,6 +566,9 @@ public class FragmentFolders extends FragmentBase {
} else if (itemId == R.id.menu_subscribed_only) {
onMenuSubscribedOnly();
return true;
} else if (itemId == R.id.menu_sort_unread_atop) {
onMenuSortUnreadAtop();
return true;
} else if (itemId == R.id.menu_search_folder) {
onMenuSearchFolder(item);
return true;
@ -657,6 +662,14 @@ public class FragmentFolders extends FragmentBase {
adapter.setSubscribedOnly(subscribed_only);
}
private void onMenuSortUnreadAtop() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean sort_unread_atop = !prefs.getBoolean("sort_unread_atop", false);
prefs.edit().putBoolean("sort_unread_atop", sort_unread_atop).apply();
getActivity().invalidateOptionsMenu();
adapter.setSortUnreadAtop(sort_unread_atop);
}
private void onMenuSearchFolder(MenuItem item) {
if (item.isActionViewExpanded())
item.collapseActionView();

@ -47,6 +47,12 @@
android:checkable="true"
android:title="@string/title_subscribed_only"
app:showAsAction="never" />
<item
android:id="@+id/menu_sort_unread_atop"
android:checkable="true"
android:title="@string/title_sort_unread_atop"
app:showAsAction="never" />
</group>
<group android:id="@+id/group_operations">

@ -913,6 +913,7 @@
<string name="title_show_folders">Show hidden folders</string>
<string name="title_show_flagged">Show starred message count</string>
<string name="title_subscribed_only">Subscribed only</string>
<string name="title_sort_unread_atop">Sort unread on top</string>
<string name="title_search_folder">Search for folder</string>
<string name="title_apply_to_all">Apply to all</string>
<string name="title_hide_folder">Hide folder</string>

Loading…
Cancel
Save