Add sorting by type

This commit is contained in:
Alexander Sytnyk 2017-11-16 17:56:06 +02:00
parent bce0fd7862
commit a6245c4a8a
2 changed files with 57 additions and 13 deletions

View file

@ -107,7 +107,7 @@ public class NotesFragment extends OsmAndListFragment {
@Override
public void onResume() {
super.onResume();
List<Object> items = createItemsList(sortItemsDescending(new LinkedList<>(plugin.getAllRecordings())));
List<Object> items = createItemsList();
ListView listView = getListView();
listView.setDivider(null);
if (items.size() > 0 && footerView == null) {
@ -176,12 +176,38 @@ public class NotesFragment extends OsmAndListFragment {
return null;
}
private List<Object> createItemsList(List<Recording> recs) {
private List<Object> createItemsList() {
List<Recording> recs = new LinkedList<>(plugin.getAllRecordings());
List<Object> res = new LinkedList<>();
OsmandSettings settings = getMyApplication().getSettings();
if (settings.NOTES_SORT_BY_MODE.get().isByDate()) {
res.add(NotesAdapter.TYPE_HEADER);
res.addAll(recs);
res.add(NotesAdapter.TYPE_DATE_HEADER);
res.addAll(sortItemsByDateDescending(recs));
} else if (settings.NOTES_SORT_BY_MODE.get().isByType()) {
List<Recording> audios = new LinkedList<>();
List<Recording> photos = new LinkedList<>();
List<Recording> videos = new LinkedList<>();
for (Recording rec : recs) {
if (rec.isAudio()) {
audios.add(rec);
} else if (rec.isPhoto()) {
photos.add(rec);
} else {
videos.add(rec);
}
}
if (!audios.isEmpty()) {
res.add(NotesAdapter.TYPE_AUDIO_HEADER);
res.addAll(audios);
}
if (!photos.isEmpty()) {
res.add(NotesAdapter.TYPE_PHOTO_HEADER);
res.addAll(photos);
}
if (!videos.isEmpty()) {
res.add(NotesAdapter.TYPE_VIDEO_HEADER);
res.addAll(videos);
}
}
return res;
}
@ -250,7 +276,7 @@ public class NotesFragment extends OsmAndListFragment {
listAdapter.notifyDataSetInvalidated();
}
private List<Recording> sortItemsDescending(List<Recording> recs) {
private List<Recording> sortItemsByDateDescending(List<Recording> recs) {
Collections.sort(recs, new Comparator<Recording>() {
@Override
public int compare(Recording first, Recording second) {
@ -272,7 +298,9 @@ public class NotesFragment extends OsmAndListFragment {
return new SortFragmentListener() {
@Override
public void onSortModeChanged() {
Toast.makeText(getContext(), "Sort by mode changed", Toast.LENGTH_SHORT).show();
listAdapter.clear();
listAdapter.addAll(createItemsList());
listAdapter.notifyDataSetChanged();
}
};
}

View file

@ -21,9 +21,12 @@ import java.util.Set;
public class NotesAdapter extends ArrayAdapter<Object> {
public static final int TYPE_COUNT = 2;
public static final int TYPE_HEADER = 0;
public static final int TYPE_ITEM = 1;
public static final int TYPE_COUNT = 5;
public static final int TYPE_DATE_HEADER = 0;
public static final int TYPE_AUDIO_HEADER = 1;
public static final int TYPE_PHOTO_HEADER = 2;
public static final int TYPE_VIDEO_HEADER = 3;
public static final int TYPE_ITEM = 4;
private OsmandApplication app;
private NotesAdapterListener listener;
@ -52,9 +55,13 @@ public class NotesAdapter extends ArrayAdapter<Object> {
@Override
public View getView(final int position, View row, @NonNull ViewGroup parent) {
final int type = getItemViewType(position);
boolean header = type == TYPE_DATE_HEADER
|| type == TYPE_AUDIO_HEADER
|| type == TYPE_PHOTO_HEADER
|| type == TYPE_VIDEO_HEADER;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (type == TYPE_HEADER) {
if (header) {
row = inflater.inflate(R.layout.list_item_header, parent, false);
HeaderViewHolder hHolder = new HeaderViewHolder(row);
row.setTag(hHolder);
@ -65,10 +72,9 @@ public class NotesAdapter extends ArrayAdapter<Object> {
}
}
if (type == TYPE_HEADER) {
if (header) {
final HeaderViewHolder holder = (HeaderViewHolder) row.getTag();
holder.checkBox.setVisibility(selectionMode ? View.VISIBLE : View.GONE);
holder.view.setEnabled(selectionMode);
holder.headerRow.setEnabled(selectionMode);
if (selectionMode) {
holder.checkBox.setChecked(isSelectAllChecked());
@ -89,7 +95,17 @@ public class NotesAdapter extends ArrayAdapter<Object> {
} else {
holder.view.setOnClickListener(null);
}
holder.title.setText(R.string.notes_by_date);
int titleId;
if (type == TYPE_DATE_HEADER) {
titleId = R.string.notes_by_date;
} else if (type == TYPE_AUDIO_HEADER) {
titleId = R.string.shared_string_audio;
} else if (type == TYPE_PHOTO_HEADER) {
titleId = R.string.shared_string_photo;
} else {
titleId = R.string.shared_string_video;
}
holder.title.setText(titleId);
} else {
final Object item = getItem(position);
if (item instanceof Recording) {