Skip to content

Commit

Permalink
add sorting as EndHeaderAction, revering settings-screen
Browse files Browse the repository at this point in the history
  • Loading branch information
ntruchsess committed Feb 19, 2024
1 parent 71ef2ca commit 8087d1e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
1 change: 0 additions & 1 deletion OsmAnd/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5618,7 +5618,6 @@
<string name="shared_string_keywords">Schlüsselwörter</string>
<string name="shared_string_additional">Zusätzlich</string>
<string name="shared_string_activity">Aktivität</string>
<string name="settings_favorites_sortorder">Nach Entfernung sortieren</string>
<string name="sort_subfolders">Unterordner sortieren</string>
<string name="sorted_sufolders_toast">Unterordner in „%1$s“ werden sortiert nach: „%2$s“</string>
<string name="special_routing_cpp">Spezieller Navigationstyp</string>
Expand Down
1 change: 0 additions & 1 deletion OsmAnd/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5524,5 +5524,4 @@ Download tile maps directly, or copy them as SQLite database files to OsmAnd\'s
<string name="routing_attr_freeride_policy_name">Off-piste</string>
<string name="routing_attr_freeride_policy_description">\'Freeride\' and \'Off-piste\' are unofficial routes and passages. Typically ungroomed, unmaintained and not checked in the evening. Enter at your own risk.</string>
<string name="voice_prompts_timetable">Voice prompts times</string>
<string name="settings_favorites_sortorder">Sort by distance</string>
</resources>
38 changes: 28 additions & 10 deletions OsmAnd/src/net/osmand/plus/auto/screens/FavoritesScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import androidx.car.app.model.CarIcon;
import androidx.car.app.model.CarLocation;
import androidx.car.app.model.DistanceSpan;
import androidx.car.app.model.Header;
import androidx.car.app.model.ItemList;
import androidx.car.app.model.Metadata;
import androidx.car.app.model.Place;
Expand Down Expand Up @@ -59,7 +60,7 @@ public final class FavoritesScreen extends BaseAndroidAutoScreen {
@Nullable
private FavoriteGroup selectedGroup;
private CompassMode initialCompassMode;
private boolean isSortableByDistance;
private boolean isLeastRecentyUsedGroup;

public FavoritesScreen(
@NonNull CarContext carContext,
Expand All @@ -68,7 +69,7 @@ public FavoritesScreen(
super(carContext);
this.settingsAction = settingsAction;
selectedGroup = group;
isSortableByDistance = group != null;
isLeastRecentyUsedGroup = selectedGroup == null;
getLifecycle().addObserver(new DefaultLifecycleObserver() {
@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
Expand Down Expand Up @@ -96,13 +97,26 @@ private FavouritesLayer getFavouritesLayer() {
@NonNull
@Override
public Template onGetTemplate() {
boolean sortFavByDistance = getApp().getSettings().SORT_FAV_BY_DISTANCE.get();
ItemList.Builder listBuilder = new ItemList.Builder();
setupFavorites(listBuilder);
setupFavorites(listBuilder, sortFavByDistance);
return new PlaceListNavigationTemplate.Builder()
.setItemList(listBuilder.build())
.setTitle(getApp().getString(R.string.shared_string_favorites))
.setHeader(new Header.Builder()
.setTitle(getApp().getString(R.string.shared_string_favorites))
.setStartHeaderAction(Action.BACK)
.addEndHeaderAction(new Action.Builder()
.setIcon(new CarIcon.Builder(
IconCompat.createWithResource(getCarContext(), sortFavByDistance ? R.drawable.ic_action_sort_short_to_long : R.drawable.ic_action_sort_date_31))
.build())
.setOnClickListener(() -> {
getApp().getSettings().SORT_FAV_BY_DISTANCE.set(!sortFavByDistance);
invalidate();
})
.build())
.build()
)
.setActionStrip(new ActionStrip.Builder().addAction(createSearchAction()).build())
.setHeaderAction(Action.BACK)
.build();
}

Expand All @@ -111,12 +125,12 @@ protected int getConstraintLimitType() {
return ConstraintManager.CONTENT_LIMIT_TYPE_PLACE_LIST;
}

private void setupFavorites(ItemList.Builder listBuilder) {
private void setupFavorites(ItemList.Builder listBuilder, boolean sortFavByDistance) {
OsmandSettings settings = getApp().getSettings();
List<FavouritePoint> favoritePoints = getFavorites();
int limitedSize = Math.min(favoritePoints.size(), getContentLimit() -1);
LatLon location = getApp().getMapViewTrackingUtilities().getDefaultLocation();
List<FavoritePointDistance> limitedFavoritePointDistances = toLimitedSortedPointDistanceList(favoritePoints, location, isSortableByDistance && settings.SORT_FAV_BY_DISTANCE.get(), limitedSize);
List<FavoritePointDistance> limitedFavoritePointDistances = toLimitedSortedPointDistanceList(favoritePoints, location, sortFavByDistance, limitedSize);
List<FavouritePoint> limitedFavoritePoints = new ArrayList<>(limitedSize);
QuadRect mapRect = new QuadRect();
if (!Algorithms.isEmpty(limitedFavoritePointDistances)) {
Expand Down Expand Up @@ -167,14 +181,18 @@ private static List<FavoritePointDistance> toPointDistanceList(List<FavouritePoi
return returnList;
};

private static List<FavoritePointDistance> toLimitedSortedPointDistanceList(List<FavouritePoint> points, LatLon location, boolean sortByDistance, int limitedSize) {
if (sortByDistance) {
private List<FavoritePointDistance> toLimitedSortedPointDistanceList(List<FavouritePoint> points, LatLon location, boolean sortByDistance, int limitedSize) {
if (sortByDistance && !isLeastRecentyUsedGroup) {
List<FavoritePointDistance> pointDistances = toPointDistanceList(points, location);
Collections.sort(pointDistances, Comparator.comparingDouble(pointDistance -> pointDistance.distance));
return pointDistances.subList(0, limitedSize);
} else {
Collections.sort(points, (left, right) -> Long.compare(right.getTimestamp(), left.getTimestamp()));
return toPointDistanceList(points.subList(0, limitedSize), location);
List<FavoritePointDistance> pointDistances = toPointDistanceList(points.subList(0, limitedSize), location);
if (sortByDistance) {
Collections.sort(pointDistances, Comparator.comparingDouble(pointDistance -> pointDistance.distance));
}
return pointDistances;
}
};

Expand Down
16 changes: 0 additions & 16 deletions OsmAnd/src/net/osmand/plus/auto/screens/SettingsScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,6 @@ public Template onGetTemplate() {
sectionABuilder.build(),
getCarContext().getString(R.string.voice_pref_title)));

ItemList.Builder sectionBBuilder = new ItemList.Builder();
sectionBBuilder.addItem(new Row.Builder()
.setTitle(getCarContext().getString(R.string.settings_favorites_sortorder))
.setToggle(
new Toggle.Builder(
(value) -> osmandSettings.SORT_FAV_BY_DISTANCE.set(value))
.setChecked(osmandSettings.SORT_FAV_BY_DISTANCE.get())
.build())
.build()
);

templateBuilder.addSectionedList(
SectionedItemList.create(
sectionBBuilder.build(),
getCarContext().getString(R.string.shared_string_favorites)));

/*
ItemList.Builder sectionBBuilder = new ItemList.Builder();
sectionBBuilder.addItem(
Expand Down

0 comments on commit 8087d1e

Please sign in to comment.