Merge pull request #9836 from osmandapp/sort_tracks

Add tracks sorting
This commit is contained in:
vshcherb 2020-09-17 16:52:01 +02:00 committed by GitHub
commit 1c799387b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 10 deletions

View file

@ -37,6 +37,7 @@ import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
@ -102,7 +103,11 @@ import java.util.regex.Pattern;
import static net.osmand.plus.GpxSelectionHelper.CURRENT_TRACK; import static net.osmand.plus.GpxSelectionHelper.CURRENT_TRACK;
import static net.osmand.plus.myplaces.FavoritesActivity.GPX_TAB; import static net.osmand.plus.myplaces.FavoritesActivity.GPX_TAB;
import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID; import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID;
import static net.osmand.util.Algorithms.*; import static net.osmand.util.Algorithms.capitalizeFirstLetter;
import static net.osmand.util.Algorithms.collectDirs;
import static net.osmand.util.Algorithms.formatDuration;
import static net.osmand.util.Algorithms.objectEquals;
import static net.osmand.util.Algorithms.removeAllFiles;
public class AvailableGPXFragment extends OsmandExpandableListFragment implements public class AvailableGPXFragment extends OsmandExpandableListFragment implements
FavoritesFragmentStateHolder { FavoritesFragmentStateHolder {
@ -129,6 +134,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
private boolean importing = false; private boolean importing = false;
private View emptyView; private View emptyView;
private GpxSelectionHelper.SelectGpxTaskListener gpxTaskListener; private GpxSelectionHelper.SelectGpxTaskListener gpxTaskListener;
private boolean sortByName;
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
@ -157,6 +163,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
public void onAttach(Context activity) { public void onAttach(Context activity) {
super.onAttach(activity); super.onAttach(activity);
this.app = (OsmandApplication) getActivity().getApplication(); this.app = (OsmandApplication) getActivity().getApplication();
sortByName = app.getSettings().SORT_TRACKS_BY_NAME.get();
final Collator collator = Collator.getInstance(); final Collator collator = Collator.getInstance();
collator.setStrength(Collator.SECONDARY); collator.setStrength(Collator.SECONDARY);
currentRecording = new GpxInfo(getMyApplication().getSavingTrackHelper().getCurrentGpx(), getString(R.string.shared_string_currently_recording_track)); currentRecording = new GpxInfo(getMyApplication().getSavingTrackHelper().getCurrentGpx(), getString(R.string.shared_string_currently_recording_track));
@ -495,6 +502,8 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
addTrack(); addTrack();
}else if (itemId == R.string.coordinate_input) { }else if (itemId == R.string.coordinate_input) {
openCoordinatesInput(); openCoordinatesInput();
} else if (itemId == R.string.shared_string_sort) {
updateTracksSort();
} }
return true; return true;
} }
@ -512,6 +521,8 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
.setIcon(R.drawable.ic_action_delete_dark).setListener(listener).createItem()); .setIcon(R.drawable.ic_action_delete_dark).setListener(listener).createItem());
optionsMenuAdapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.shared_string_refresh, getActivity()) optionsMenuAdapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.shared_string_refresh, getActivity())
.setIcon(R.drawable.ic_action_refresh_dark).setListener(listener).createItem()); .setIcon(R.drawable.ic_action_refresh_dark).setListener(listener).createItem());
optionsMenuAdapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.shared_string_sort, getActivity())
.setIcon(getSortIconId(!sortByName)).setListener(listener).createItem());
OsmandPlugin.onOptionsMenuActivity(getActivity(), this, optionsMenuAdapter); OsmandPlugin.onOptionsMenuActivity(getActivity(), this, optionsMenuAdapter);
for (int j = 0; j < optionsMenuAdapter.length(); j++) { for (int j = 0; j < optionsMenuAdapter.length(); j++) {
final MenuItem item; final MenuItem item;
@ -536,6 +547,11 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
} }
} }
@DrawableRes
private int getSortIconId(boolean sortByName) {
return sortByName ? R.drawable.ic_action_sort_by_name : R.drawable.ic_action_list_sort;
}
public void doAction(int actionResId) { public void doAction(int actionResId) {
if (actionResId == R.string.shared_string_delete) { if (actionResId == R.string.shared_string_delete) {
operationTask = new DeleteGpxTask(); operationTask = new DeleteGpxTask();
@ -555,6 +571,9 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
ContextMenuItem contextMenuItem = optionsMenuAdapter.getItem(i); ContextMenuItem contextMenuItem = optionsMenuAdapter.getItem(i);
if (itemId == contextMenuItem.getTitleId()) { if (itemId == contextMenuItem.getTitleId()) {
contextMenuItem.getItemClickListener().onContextMenuClick(null, itemId, i, false, null); contextMenuItem.getItemClickListener().onContextMenuClick(null, itemId, i, false, null);
if (itemId == R.string.shared_string_sort) {
item.setIcon(getSortIconId(!sortByName));
}
return true; return true;
} }
} }
@ -565,6 +584,12 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
((FavoritesActivity) getActivity()).addTrack(); ((FavoritesActivity) getActivity()).addTrack();
} }
private void updateTracksSort() {
sortByName = !sortByName;
app.getSettings().SORT_TRACKS_BY_NAME.set(sortByName);
reloadTracks();
}
private void openCoordinatesInput() { private void openCoordinatesInput() {
CoordinateInputDialogFragment fragment = new CoordinateInputDialogFragment(); CoordinateInputDialogFragment fragment = new CoordinateInputDialogFragment();
fragment.setRetainInstance(true); fragment.setRetainInstance(true);
@ -948,12 +973,16 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
Arrays.sort(listFiles, new Comparator<File>() { Arrays.sort(listFiles, new Comparator<File>() {
@Override @Override
public int compare(File f1, File f2) { public int compare(File f1, File f2) {
if (sortByName) {
return -f1.getName().compareTo(f2.getName());
} else {
// here we could guess date from file name '2017-08-30 ...' - first part date // here we could guess date from file name '2017-08-30 ...' - first part date
if (f1.lastModified() == f2.lastModified()) { if (f1.lastModified() == f2.lastModified()) {
return -f1.getName().compareTo(f2.getName()); return -f1.getName().compareTo(f2.getName());
} }
return -Long.compare(f1.lastModified(), f2.lastModified()); return -Long.compare(f1.lastModified(), f2.lastModified());
} }
}
}); });
return listFiles; return listFiles;
} }
@ -970,7 +999,8 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
private void loadGPXFolder(File mapPath, List<GpxInfo> result, LoadGpxTask loadTask, List<GpxInfo> progress, private void loadGPXFolder(File mapPath, List<GpxInfo> result, LoadGpxTask loadTask, List<GpxInfo> progress,
String gpxSubfolder) { String gpxSubfolder) {
for (File gpxFile : listFilesSorted(mapPath)) { File[] listFiles = listFilesSorted(mapPath);
for (File gpxFile : listFiles) {
if (gpxFile.isDirectory()) { if (gpxFile.isDirectory()) {
String sub = gpxSubfolder.length() == 0 ? gpxFile.getName() : gpxSubfolder + "/" String sub = gpxSubfolder.length() == 0 ? gpxFile.getName() : gpxSubfolder + "/"
+ gpxFile.getName(); + gpxFile.getName();
@ -985,7 +1015,6 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
loadTask.loadFile(progress.toArray(new GpxInfo[progress.size()])); loadTask.loadFile(progress.toArray(new GpxInfo[progress.size()]));
progress.clear(); progress.clear();
} }
} }
} }
} }
@ -993,7 +1022,6 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
public List<GpxInfo> getResult() { public List<GpxInfo> getResult() {
return result; return result;
} }
} }
protected class GpxIndexesAdapter extends OsmandBaseExpandableListAdapter implements Filterable { protected class GpxIndexesAdapter extends OsmandBaseExpandableListAdapter implements Filterable {
@ -1050,7 +1078,16 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
Collections.sort(selected, new Comparator<GpxInfo>() { Collections.sort(selected, new Comparator<GpxInfo>() {
@Override @Override
public int compare(GpxInfo i1, GpxInfo i2) { public int compare(GpxInfo i1, GpxInfo i2) {
if (sortByName) {
return i1.getName().toLowerCase().compareTo(i2.getName().toLowerCase()); return i1.getName().toLowerCase().compareTo(i2.getName().toLowerCase());
} else {
long time1 = i1.file.lastModified();
long time2 = i2.file.lastModified();
if (time1 == time2) {
return i1.getName().toLowerCase().compareTo(i2.getName().toLowerCase());
}
return -Long.compare(time1, time2);
}
} }
}); });
} }
@ -1112,7 +1149,6 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
data.get(category.get(found)).add(info); data.get(category.get(found)).add(info);
} }
// disable sort
public void sort() { public void sort() {
Collections.sort(category, new Comparator<String>() { Collections.sort(category, new Comparator<String>() {
@Override @Override

View file

@ -3936,6 +3936,9 @@ public class OsmandSettings {
public final CommonPreference<Integer> FAVORITES_TAB = public final CommonPreference<Integer> FAVORITES_TAB =
new IntPreference("FAVORITES_TAB", 0).makeGlobal().cache(); new IntPreference("FAVORITES_TAB", 0).makeGlobal().cache();
public final CommonPreference<Boolean> SORT_TRACKS_BY_NAME
= new BooleanPreference("sort_tracks_by_name", true).makeGlobal().cache();
public final CommonPreference<Integer> OSMAND_THEME = public final CommonPreference<Integer> OSMAND_THEME =
new IntPreference("osmand_theme", OSMAND_LIGHT_THEME) { new IntPreference("osmand_theme", OSMAND_LIGHT_THEME) {
@Override @Override