mirror of
https://github.com/M66B/FairEmail.git
synced 2026-03-28 03:46:00 +01:00
Added answer sorting options
This commit is contained in:
@@ -48,10 +48,14 @@ import androidx.recyclerview.widget.DiffUtil;
|
||||
import androidx.recyclerview.widget.ListUpdateCallback;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import java.text.Collator;
|
||||
import java.text.DateFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder> {
|
||||
private Fragment parentFragment;
|
||||
@@ -63,6 +67,7 @@ public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder
|
||||
private DateFormat DF;
|
||||
private NumberFormat NF = NumberFormat.getNumberInstance();
|
||||
|
||||
private String sort = null;
|
||||
private String search = null;
|
||||
private List<EntityAnswer> all = new ArrayList<>();
|
||||
private List<EntityAnswer> selected = new ArrayList<>();
|
||||
@@ -313,8 +318,39 @@ public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder
|
||||
}.execute(context, owner, new Bundle(), "answer:composable");
|
||||
}
|
||||
|
||||
public void set(@NonNull List<EntityAnswer> answers) {
|
||||
Log.i("Set answers=" + answers.size() + " search=" + search);
|
||||
public void set(String sort, @NonNull List<EntityAnswer> answers) {
|
||||
this.sort = sort;
|
||||
Log.i("Set answers=" + answers.size() + " sort=" + sort + " search=" + search);
|
||||
|
||||
final Collator collator = Collator.getInstance(Locale.getDefault());
|
||||
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
|
||||
|
||||
Collections.sort(answers, new Comparator<EntityAnswer>() {
|
||||
@Override
|
||||
public int compare(EntityAnswer a1, EntityAnswer a2) {
|
||||
int order;
|
||||
if ("last_applied".equals(sort))
|
||||
order = -Long.compare(
|
||||
a1.last_applied == null ? 0 : a1.last_applied,
|
||||
a2.last_applied == null ? 0 : a2.last_applied);
|
||||
else if ("applied".equals(sort)) {
|
||||
order = -Integer.compare(
|
||||
a1.applied == null ? 0 : a1.applied,
|
||||
a2.applied == null ? 0 : a2.applied);
|
||||
} else {
|
||||
order = collator.compare(a1.group == null ? "" : a1.group, a2.group == null ? "" : a2.group);
|
||||
if (order == 0)
|
||||
order = -Boolean.compare(a1.favorite, a2.favorite);
|
||||
}
|
||||
|
||||
if (order == 0)
|
||||
return collator.compare(
|
||||
a1.name == null ? "" : a1.name,
|
||||
a2.name == null ? "" : a2.name);
|
||||
else
|
||||
return order;
|
||||
}
|
||||
});
|
||||
|
||||
all = answers;
|
||||
|
||||
@@ -365,10 +401,16 @@ public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder
|
||||
}
|
||||
}
|
||||
|
||||
public void setSort(String sort) {
|
||||
this.sort = sort;
|
||||
set(sort, all);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public void search(String query) {
|
||||
Log.i("Answers query=" + query);
|
||||
search = query;
|
||||
set(all);
|
||||
set(sort, all);
|
||||
}
|
||||
|
||||
private static class DiffCallback extends DiffUtil.Callback {
|
||||
|
||||
Reference in New Issue
Block a user