Improved sorting

This commit is contained in:
Kanahia 2024-05-18 11:14:48 +05:30
parent 296eb5b1be
commit 49fd84b3cb
2 changed files with 7 additions and 10 deletions

View file

@ -6,6 +6,7 @@ import android.os.Parcelable;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import fr.free.nrw.commons.location.LatLng; import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.nearby.model.NearbyResultItem; import fr.free.nrw.commons.nearby.model.NearbyResultItem;
import fr.free.nrw.commons.utils.LocationUtils;
import fr.free.nrw.commons.utils.PlaceUtils; import fr.free.nrw.commons.utils.PlaceUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import timber.log.Timber; import timber.log.Timber;
@ -145,18 +146,14 @@ public class Place implements Parcelable {
} }
/** /**
* Gets the name of the place * Gets the distance between place and curLatLng
* *
* @param curLatLng
* @return name * @return name
*/ */
public Double getDistanceInDouble() { public Double getDistanceInDouble(LatLng curLatLng) {
double distanceValue = 0.0; return LocationUtils.calculateDistance(curLatLng.getLatitude(), curLatLng.getLongitude(),
if (distance.endsWith("km")) { getLocation().getLatitude(), getLocation().getLongitude());
distanceValue = Double.parseDouble(distance.replace("km", ""));
} else if (distance.endsWith("m")) {
distanceValue = Double.parseDouble(distance.replace("m", "")) / 1000;
}
return distanceValue;
} }
/** /**

View file

@ -1400,7 +1400,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
int batchSize = 3; int batchSize = 3;
final List<Place> updatedPlaceList = new ArrayList<>(placeList); final List<Place> updatedPlaceList = new ArrayList<>(placeList);
if (VERSION.SDK_INT >= VERSION_CODES.N) { if (VERSION.SDK_INT >= VERSION_CODES.N) {
Collections.sort(updatedPlaceList, Comparator.comparingDouble(Place::getDistanceInDouble)); Collections.sort(places, Comparator.comparingDouble(place -> place.getDistanceInDouble(curLatLng)));
} }
processBatchesSequentially(places, batchSize, updatedPlaceList, curLatLng, 0); processBatchesSequentially(places, batchSize, updatedPlaceList, curLatLng, 0);
} }