From 890050d91380587a2b2757a4a9744506539ab7f6 Mon Sep 17 00:00:00 2001 From: Paul Hawke Date: Sat, 5 Jul 2025 23:25:55 -0500 Subject: [PATCH] Convert MapController to kotlin along with fixing nullability in a few places --- .../fr/free/nrw/commons/MapController.java | 30 ------------ .../java/fr/free/nrw/commons/MapController.kt | 46 +++++++++++++++++++ .../commons/explore/map/ExploreMapCalls.java | 3 ++ .../explore/map/ExploreMapPresenter.java | 9 ++-- .../free/nrw/commons/nearby/NearbyPlaces.java | 26 +++++++---- 5 files changed, 69 insertions(+), 45 deletions(-) delete mode 100644 app/src/main/java/fr/free/nrw/commons/MapController.java create mode 100644 app/src/main/java/fr/free/nrw/commons/MapController.kt diff --git a/app/src/main/java/fr/free/nrw/commons/MapController.java b/app/src/main/java/fr/free/nrw/commons/MapController.java deleted file mode 100644 index 72005fe83..000000000 --- a/app/src/main/java/fr/free/nrw/commons/MapController.java +++ /dev/null @@ -1,30 +0,0 @@ -package fr.free.nrw.commons; - -import fr.free.nrw.commons.location.LatLng; -import fr.free.nrw.commons.nearby.Place; -import java.util.List; - -public abstract class MapController { - - /** - * We pass this variable as a group of placeList and boundaryCoordinates - */ - public class NearbyPlacesInfo { - public List placeList; // List of nearby places - public LatLng[] boundaryCoordinates; // Corners of nearby area - public LatLng currentLatLng; // Current location when this places are populated - public LatLng searchLatLng; // Search location for finding this places - public List mediaList; // Search location for finding this places - } - - /** - * We pass this variable as a group of placeList and boundaryCoordinates - */ - public class ExplorePlacesInfo { - public List explorePlaceList; // List of nearby places - public LatLng[] boundaryCoordinates; // Corners of nearby area - public LatLng currentLatLng; // Current location when this places are populated - public LatLng searchLatLng; // Search location for finding this places - public List mediaList; // Search location for finding this places - } -} diff --git a/app/src/main/java/fr/free/nrw/commons/MapController.kt b/app/src/main/java/fr/free/nrw/commons/MapController.kt new file mode 100644 index 000000000..5888b3f5f --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/MapController.kt @@ -0,0 +1,46 @@ +package fr.free.nrw.commons + +import fr.free.nrw.commons.location.LatLng +import fr.free.nrw.commons.nearby.Place + +abstract class MapController { + /** + * We pass this variable as a group of placeList and boundaryCoordinates + */ + inner class NearbyPlacesInfo { + @JvmField + var placeList: List = emptyList() // List of nearby places + + @JvmField + var boundaryCoordinates: Array = emptyArray() // Corners of nearby area + + @JvmField + var currentLatLng: LatLng? = null // Current location when this places are populated + + @JvmField + var searchLatLng: LatLng? = null // Search location for finding this places + + @JvmField + var mediaList: List? = null // Search location for finding this places + } + + /** + * We pass this variable as a group of placeList and boundaryCoordinates + */ + inner class ExplorePlacesInfo { + @JvmField + var explorePlaceList: List = emptyList() // List of nearby places + + @JvmField + var boundaryCoordinates: Array = emptyArray() // Corners of nearby area + + @JvmField + var currentLatLng: LatLng? = null // Current location when this places are populated + + @JvmField + var searchLatLng: LatLng? = null // Search location for finding this places + + @JvmField + var mediaList: List = emptyList() // Search location for finding this places + } +} diff --git a/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapCalls.java b/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapCalls.java index ec426942c..5e674dceb 100644 --- a/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapCalls.java +++ b/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapCalls.java @@ -1,8 +1,10 @@ package fr.free.nrw.commons.explore.map; +import androidx.annotation.NonNull; import fr.free.nrw.commons.Media; import fr.free.nrw.commons.location.LatLng; import fr.free.nrw.commons.media.MediaClient; +import java.util.Collections; import java.util.List; import javax.inject.Inject; import javax.inject.Singleton; @@ -23,6 +25,7 @@ public class ExploreMapCalls { * @param currentLatLng coordinates of search location * @return list of places obtained */ + @NonNull List callCommonsQuery(final LatLng currentLatLng) { String coordinates = currentLatLng.getLatitude() + "|" + currentLatLng.getLongitude(); return mediaClient.getMediaListFromGeoSearch(coordinates).blockingGet(); diff --git a/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapPresenter.java b/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapPresenter.java index 45a11a422..70f785b40 100644 --- a/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapPresenter.java +++ b/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapPresenter.java @@ -14,6 +14,7 @@ import fr.free.nrw.commons.explore.map.ExploreMapController.NearbyBaseMarkerThum import fr.free.nrw.commons.kvstore.JsonKvStore; import fr.free.nrw.commons.location.LatLng; import fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType; +import fr.free.nrw.commons.nearby.Place; import io.reactivex.Observable; import java.lang.reflect.Proxy; import java.util.List; @@ -182,7 +183,7 @@ public class ExploreMapPresenter exploreMapController .loadAttractionsFromLocationToBaseMarkerOptions(explorePlacesInfo.currentLatLng, // Curlatlang will be used to calculate distances - explorePlacesInfo.explorePlaceList, + (List) explorePlacesInfo.explorePlaceList, exploreMapFragmentView.getContext(), this, explorePlacesInfo); @@ -230,11 +231,7 @@ public class ExploreMapPresenter mylocation.setLongitude(exploreMapFragmentView.getLastMapFocus().getLongitude()); Float distance = mylocation.distanceTo(dest_location); - if (distance > 2000.0 * 3 / 4) { - return false; - } else { - return true; - } + return !(distance > 2000.0 * 3 / 4); } } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java index caae8ee45..3bb2f549f 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java @@ -1,10 +1,14 @@ package fr.free.nrw.commons.nearby; +import static java.util.Collections.emptyList; + import android.location.Location; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import fr.free.nrw.commons.nearby.model.NearbyQueryParams; +import fr.free.nrw.commons.nearby.model.NearbyQueryParams.Radial; +import fr.free.nrw.commons.nearby.model.NearbyQueryParams.Rectangular; import java.io.IOException; -import java.util.Collections; import java.util.List; import javax.inject.Inject; @@ -46,13 +50,14 @@ public class NearbyPlaces { * @param customQuery * @return list of places obtained */ + @NonNull List radiusExpander(final LatLng currentLatLng, final String lang, final boolean returnClosestResult, @Nullable final String customQuery) throws Exception { final int minResults; final double maxRadius; - List places = Collections.emptyList(); + List places = emptyList(); // If returnClosestResult is true, then this means that we are trying to get closest point // to use in cardView in Contributions fragment @@ -113,6 +118,7 @@ public class NearbyPlaces { * @return A list of places obtained from the Wikidata query. * @throws Exception If an error occurs during the retrieval process. */ + @NonNull public List getFromWikidataQuery( final fr.free.nrw.commons.location.LatLng centerPoint, final fr.free.nrw.commons.location.LatLng screenTopRight, @@ -120,11 +126,11 @@ public class NearbyPlaces { final boolean shouldQueryForMonuments, @Nullable final String customQuery) throws Exception { if (customQuery != null) { - return okHttpJsonApiClient - .getNearbyPlaces( - new NearbyQueryParams.Rectangular(screenTopRight, screenBottomLeft), lang, + final List nearbyPlaces = okHttpJsonApiClient.getNearbyPlaces( + new Rectangular(screenTopRight, screenBottomLeft), lang, shouldQueryForMonuments, customQuery); + return nearbyPlaces != null ? nearbyPlaces : emptyList(); } final int lowerLimit = 1000, upperLimit = 1500; @@ -141,9 +147,10 @@ public class NearbyPlaces { final int itemCount = okHttpJsonApiClient.getNearbyItemCount( new NearbyQueryParams.Rectangular(screenTopRight, screenBottomLeft)); if (itemCount < upperLimit) { - return okHttpJsonApiClient.getNearbyPlaces( - new NearbyQueryParams.Rectangular(screenTopRight, screenBottomLeft), lang, + final List nearbyPlaces = okHttpJsonApiClient.getNearbyPlaces( + new Rectangular(screenTopRight, screenBottomLeft), lang, shouldQueryForMonuments, null); + return nearbyPlaces != null ? nearbyPlaces : emptyList(); } } @@ -175,9 +182,10 @@ public class NearbyPlaces { maxRadius = targetRadius - 1; } } - return okHttpJsonApiClient.getNearbyPlaces( - new NearbyQueryParams.Radial(centerPoint, targetRadius / 100f), lang, shouldQueryForMonuments, + final List nearbyPlaces = okHttpJsonApiClient.getNearbyPlaces( + new Radial(centerPoint, targetRadius / 100f), lang, shouldQueryForMonuments, null); + return nearbyPlaces != null ? nearbyPlaces : emptyList(); } /**