Fix #7654 Add destination: Sort Favorites #7654

This commit is contained in:
Victor Shcherb 2020-02-12 19:18:08 +01:00
parent 84b6c31c83
commit b98d76fd25
3 changed files with 43 additions and 23 deletions

View file

@ -1965,6 +1965,7 @@
<string name="none_selected_gpx">Specify a GPX file by long-tapping first.</string> <string name="none_selected_gpx">Specify a GPX file by long-tapping first.</string>
<string name="local_index_select_gpx_file">Select a track</string> <string name="local_index_select_gpx_file">Select a track</string>
<string name="gpx_split_interval">Split interval</string> <string name="gpx_split_interval">Split interval</string>
<string name="sort_by_category">Sort by category</string>
<string name="sort_by_distance">Sort by distance</string> <string name="sort_by_distance">Sort by distance</string>
<string name="sort_by_name">Sort by name</string> <string name="sort_by_name">Sort by name</string>
<string name="show_zoom_buttons_navigation_descr">Show zoom buttons during navigation.</string> <string name="show_zoom_buttons_navigation_descr">Show zoom buttons during navigation.</string>

View file

@ -42,12 +42,8 @@ public class FavouritesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
if (holder instanceof FavouritesViewHolder) { if (holder instanceof FavouritesViewHolder) {
FavouritesViewHolder favouritesViewHolder = (FavouritesViewHolder) holder; FavouritesViewHolder favouritesViewHolder = (FavouritesViewHolder) holder;
FavouritePoint favouritePoint = getItem(position); FavouritePoint favouritePoint = getItem(position);
favouritesViewHolder.title.setText(favouritePoint.getName()); favouritesViewHolder.title.setText(favouritePoint.getDisplayName(app));
if (favouritePoint.getCategory().equals("")) { favouritesViewHolder.description.setText(favouritePoint.getCategoryDisplayName(app));
favouritesViewHolder.description.setText(R.string.shared_string_favorites);
} else {
favouritesViewHolder.description.setText(favouritePoint.getCategory());
}
favouritesViewHolder.favouriteImage.setImageDrawable( favouritesViewHolder.favouriteImage.setImageDrawable(
FavoriteImageDrawable.getOrCreate(app, favouritePoint.getColor(), false, favouritePoint)); FavoriteImageDrawable.getOrCreate(app, favouritePoint.getColor(), false, favouritePoint));
app.getUIUtilities().updateLocationView(cache, favouritesViewHolder.arrowImage, favouritesViewHolder.distance, app.getUIUtilities().updateLocationView(cache, favouritesViewHolder.arrowImage, favouritesViewHolder.distance,

View file

@ -43,11 +43,17 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag
public static final String TAG = "FavouritesBottomSheetMenuFragment"; public static final String TAG = "FavouritesBottomSheetMenuFragment";
private static final String IS_SORTED = "sorted"; private static final String IS_SORTED = "sorted";
private static final String SORTED_BY_TYPE = "sortedByType"; private static final String SORTED_BY_TYPE = "sortedByType";
private static final int SORT_TYPE_DIST = 1;
private static final int SORT_TYPE_NAME = 2;
private static final int SORT_TYPE_CATEGORY = 3;
private static int getNextType(int type) {
return type % SORT_TYPE_CATEGORY + 1;
}
private List<FavouritePoint> favouritePoints = new ArrayList<>(); private List<FavouritePoint> favouritePoints = new ArrayList<>();
private FavouritesAdapter adapter; private FavouritesAdapter adapter;
private RecyclerView recyclerView; private RecyclerView recyclerView;
private boolean sortByDist = true; private int sortByDist = SORT_TYPE_DIST;
private boolean isSorted = false; private boolean isSorted = false;
private boolean locationUpdateStarted; private boolean locationUpdateStarted;
private boolean compassUpdateAllowed = true; private boolean compassUpdateAllowed = true;
@ -64,7 +70,7 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag
pointType = PointType.valueOf(args.getString(POINT_TYPE_KEY)); pointType = PointType.valueOf(args.getString(POINT_TYPE_KEY));
} }
if (savedInstanceState != null && savedInstanceState.getBoolean(IS_SORTED)) { if (savedInstanceState != null && savedInstanceState.getBoolean(IS_SORTED)) {
sortByDist = savedInstanceState.getBoolean(SORTED_BY_TYPE); sortByDist = savedInstanceState.getInt(SORTED_BY_TYPE);
} }
adapter = new FavouritesAdapter(getMyApplication(), favouritePoints); adapter = new FavouritesAdapter(getMyApplication(), favouritePoints);
FavouritesDbHelper helper = getMyApplication().getFavorites(); FavouritesDbHelper helper = getMyApplication().getFavorites();
@ -91,18 +97,20 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag
sortFavourites(); sortFavourites();
final BottomSheetItemTitleWithDescrAndButton[] title = new BottomSheetItemTitleWithDescrAndButton[1]; final BottomSheetItemTitleWithDescrAndButton[] title = new BottomSheetItemTitleWithDescrAndButton[1];
title[0] = (BottomSheetItemTitleWithDescrAndButton) new BottomSheetItemTitleWithDescrAndButton.Builder() title[0] = (BottomSheetItemTitleWithDescrAndButton) new BottomSheetItemTitleWithDescrAndButton.Builder()
.setButtonIcons(null, getIconForButton()) .setButtonIcons(null, getIconForButton(getNextType(sortByDist)))
.setButtonTitle(getTextForButton(sortByDist)) .setButtonTitle(getTextForButton(getNextType(sortByDist)))
.setOnButtonClickListener(new View.OnClickListener() { .setOnButtonClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
sortByDist = getNextType(sortByDist);
sortFavourites(); sortFavourites();
title[0].setButtonIcons(null, getIconForButton()); int next = getNextType(sortByDist);
title[0].setButtonText(getTextForButton(sortByDist)); title[0].setButtonIcons(null, getIconForButton(next));
title[0].setDescription(getTextForButton(!sortByDist)); title[0].setButtonText(getTextForButton(next));
title[0].setDescription(getTextForButton(sortByDist));
} }
}) })
.setDescription(getTextForButton(!sortByDist)) .setDescription(getTextForButton(sortByDist))
.setTitle(getString(R.string.favourites)) .setTitle(getString(R.string.favourites))
.setLayoutId(R.layout.bottom_sheet_item_title_with_descr_and_button) .setLayoutId(R.layout.bottom_sheet_item_title_with_descr_and_button)
.create(); .create();
@ -139,13 +147,19 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag
} }
} }
private Drawable getIconForButton() { private Drawable getIconForButton(int type) {
return getIcon(sortByDist ? R.drawable.ic_action_list_sort : R.drawable.ic_action_sort_by_name, return getIcon(type == SORT_TYPE_DIST ? R.drawable.ic_action_list_sort : R.drawable.ic_action_sort_by_name,
nightMode ? R.color.multi_selection_menu_close_btn_dark : R.color.multi_selection_menu_close_btn_light); nightMode ? R.color.multi_selection_menu_close_btn_dark : R.color.multi_selection_menu_close_btn_light);
} }
private String getTextForButton(boolean sortByDist) { private String getTextForButton(int sortByDist) {
return getString(sortByDist ? R.string.sort_by_distance : R.string.sort_by_name); int r = R.string.sort_by_distance;
if(sortByDist == SORT_TYPE_CATEGORY) {
r = R.string.sort_by_category;
} else if(sortByDist == SORT_TYPE_NAME) {
r = R.string.sort_by_name;
}
return getString(r);
} }
private void selectFavorite(FavouritePoint point) { private void selectFavorite(FavouritePoint point) {
@ -280,7 +294,7 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putBoolean(IS_SORTED, isSorted); outState.putBoolean(IS_SORTED, isSorted);
outState.putBoolean(SORTED_BY_TYPE, !sortByDist); outState.putInt(SORTED_BY_TYPE, sortByDist);
} }
@Override @Override
@ -292,21 +306,30 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag
final Collator inst = Collator.getInstance(); final Collator inst = Collator.getInstance();
Location stale = getMyApplication().getLocationProvider().getLastStaleKnownLocation(); Location stale = getMyApplication().getLocationProvider().getLastStaleKnownLocation();
final LatLon latLon = stale != null ? new LatLon(stale.getLatitude(), stale.getLongitude()) : final LatLon latLon = stale != null ? new LatLon(stale.getLatitude(), stale.getLongitude()) :
getMyApplication().getMapViewTrackingUtilities().getMapLocation(); getMyApplication().getMapViewTrackingUtilities().getMapLocation();
Collections.sort(favouritePoints, new Comparator<FavouritePoint>() { Collections.sort(favouritePoints, new Comparator<FavouritePoint>() {
@Override @Override
public int compare(FavouritePoint lhs, FavouritePoint rhs) { public int compare(FavouritePoint lhs, FavouritePoint rhs) {
if (sortByDist && latLon != null) { if (sortByDist == SORT_TYPE_DIST && latLon != null) {
double ld = MapUtils.getDistance(latLon, lhs.getLatitude(), double ld = MapUtils.getDistance(latLon, lhs.getLatitude(),
lhs.getLongitude()); lhs.getLongitude());
double rd = MapUtils.getDistance(latLon, rhs.getLatitude(), double rd = MapUtils.getDistance(latLon, rhs.getLatitude(),
rhs.getLongitude()); rhs.getLongitude());
return Double.compare(ld, rd); return Double.compare(ld, rd);
} }
return inst.compare(lhs.getName(), rhs.getName());
if(sortByDist == SORT_TYPE_CATEGORY) {
int cat = inst.compare(lhs.getCategoryDisplayName(getMyApplication()), rhs.getCategoryDisplayName(getMyApplication()));
if(cat != 0) {
return cat;
}
}
int name = inst.compare(lhs.getDisplayName(getMyApplication()), rhs.getDisplayName(getMyApplication()));
return name;
} }
}); });
sortByDist = !sortByDist;
isSorted = true; isSorted = true;
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
recyclerView.getLayoutManager().scrollToPosition(0); recyclerView.getLayoutManager().scrollToPosition(0);