4444
4545import java .util .ArrayList ;
4646import java .util .Collections ;
47+ import java .util .Comparator ;
4748import java .util .List ;
4849
4950/**
@@ -58,6 +59,7 @@ public final class FavoritesScreen extends BaseAndroidAutoScreen {
5859 @ Nullable
5960 private FavoriteGroup selectedGroup ;
6061 private CompassMode initialCompassMode ;
62+ private boolean isSortableByDistance ;
6163
6264 public FavoritesScreen (
6365 @ NonNull CarContext carContext ,
@@ -66,6 +68,7 @@ public FavoritesScreen(
6668 super (carContext );
6769 this .settingsAction = settingsAction ;
6870 selectedGroup = group ;
71+ isSortableByDistance = group != null ;
6972 getLifecycle ().addObserver (new DefaultLifecycleObserver () {
7073 @ Override
7174 public void onDestroy (@ NonNull LifecycleOwner owner ) {
@@ -109,18 +112,19 @@ protected int getConstraintLimitType() {
109112 }
110113
111114 private void setupFavorites (ItemList .Builder listBuilder ) {
115+ OsmandSettings settings = getApp ().getSettings ();
116+ List <FavouritePoint > favoritePoints = getFavorites ();
117+ int limitedSize = Math .min (favoritePoints .size (), getContentLimit () -1 );
112118 LatLon location = getApp ().getMapViewTrackingUtilities ().getDefaultLocation ();
113- List <FavouritePoint > favoritesPoints = getFavorites ();
114- int favoritesPointsSize = favoritesPoints .size ();
115- List <FavouritePoint > limitedFavoritesPoints = favoritesPoints .subList (0 , Math .min (favoritesPointsSize , getContentLimit () - 1 ));
116- getApp ().getOsmandMap ().getMapLayers ().getFavouritesLayer ().setCustomMapObjects (limitedFavoritesPoints );
119+ List <FavoritePointDistance > limitedFavoritePointDistances = toLimitedSortedPointDistanceList (favoritePoints , location , isSortableByDistance && settings .SORT_FAV_BY_DISTANCE .get (), limitedSize );
120+ List <FavouritePoint > limitedFavoritePoints = new ArrayList <>(limitedSize );
117121 QuadRect mapRect = new QuadRect ();
118- if (!Algorithms .isEmpty (limitedFavoritesPoints )) {
119- OsmandSettings settings = getApp ().getSettings ();
122+ if (!Algorithms .isEmpty (limitedFavoritePointDistances )) {
120123 initialCompassMode = settings .getCompassMode ();
121124 getApp ().getMapViewTrackingUtilities ().switchCompassModeTo (CompassMode .NORTH_IS_UP );
122125 }
123- for (FavouritePoint point : limitedFavoritesPoints ) {
126+ for (FavoritePointDistance favoritePointDistance : limitedFavoritePointDistances ) {
127+ FavouritePoint point = favoritePointDistance .favorite ;
124128 double longitude = point .getLongitude ();
125129 double latitude = point .getLatitude ();
126130 Algorithms .extendRectToContainPoint (mapRect , longitude , latitude );
@@ -129,10 +133,8 @@ private void setupFavorites(ItemList.Builder listBuilder) {
129133 CarIcon icon = new CarIcon .Builder (IconCompat .createWithBitmap (
130134 AndroidUtils .drawableToBitmap (PointImageDrawable .getFromFavorite (getApp (), color , false , point )))).build ();
131135 String description = point .getSpecialPointType () != null ? point .getDescription () : point .getCategory ();
132- double dist = MapUtils .getDistance (point .getLatitude (), point .getLongitude (),
133- location .getLatitude (), location .getLongitude ());
134136 SpannableString address = new SpannableString (Algorithms .isEmpty (description ) ? " " : " • " + description );
135- DistanceSpan distanceSpan = DistanceSpan .create (TripHelper .getDistance (getApp (), dist ));
137+ DistanceSpan distanceSpan = DistanceSpan .create (TripHelper .getDistance (getApp (), favoritePointDistance . distance ));
136138 address .setSpan (distanceSpan , 0 , 1 , SPAN_INCLUSIVE_INCLUSIVE );
137139 listBuilder .addItem (new Row .Builder ()
138140 .setTitle (title )
@@ -142,10 +144,40 @@ private void setupFavorites(ItemList.Builder listBuilder) {
142144 .setMetadata (new Metadata .Builder ().setPlace (new Place .Builder (
143145 CarLocation .create (point .getLatitude (), point .getLongitude ())).build ()).build ())
144146 .build ());
147+ limitedFavoritePoints .add (point );
145148 }
149+ getApp ().getOsmandMap ().getMapLayers ().getFavouritesLayer ().setCustomMapObjects (limitedFavoritePoints );
146150 adjustMapToRect (location , mapRect );
147151 }
148152
153+ private static class FavoritePointDistance {
154+ private final FavouritePoint favorite ;
155+ private final double distance ;
156+ FavoritePointDistance (FavouritePoint favorite , double dist ) {
157+ this .favorite = favorite ;
158+ this .distance = dist ;
159+ }
160+ }
161+
162+ private static List <FavoritePointDistance > toPointDistanceList (List <FavouritePoint > points , LatLon location ) {
163+ List <FavoritePointDistance > returnList = new ArrayList <>(points .size ());
164+ for (FavouritePoint point : points ) {
165+ returnList .add (new FavoritePointDistance (point , MapUtils .getDistance (point .getLatitude (), point .getLongitude (), location .getLatitude (), location .getLongitude ())));
166+ }
167+ return returnList ;
168+ };
169+
170+ private static List <FavoritePointDistance > toLimitedSortedPointDistanceList (List <FavouritePoint > points , LatLon location , boolean sortByDistance , int limitedSize ) {
171+ if (sortByDistance ) {
172+ List <FavoritePointDistance > pointDistances = toPointDistanceList (points , location );
173+ Collections .sort (pointDistances , Comparator .comparingDouble (pointDistance -> pointDistance .distance ));
174+ return pointDistances .subList (0 , limitedSize );
175+ } else {
176+ Collections .sort (points , (left , right ) -> Long .compare (right .getTimestamp (), left .getTimestamp ()));
177+ return toPointDistanceList (points .subList (0 , limitedSize ), location );
178+ }
179+ };
180+
149181 private void onClickFavorite (@ NonNull FavouritePoint point ) {
150182 SearchResult result = new SearchResult ();
151183 result .location = new LatLon (point .getLatitude (), point .getLongitude ());
@@ -163,7 +195,6 @@ private List<FavouritePoint> getFavorites() {
163195 } else {
164196 filteredFavorites .addAll (selectedGroup .getPoints ());
165197 }
166- Collections .sort (filteredFavorites , (left , right ) -> Long .compare (right .getTimestamp (), left .getTimestamp ()));
167198 return filteredFavorites ;
168199 }
169200
0 commit comments