From 984a8fabd1374ff00e097ede592191c35149b1f8 Mon Sep 17 00:00:00 2001 From: savsch Date: Thu, 26 Dec 2024 12:56:15 +0530 Subject: [PATCH] PlacesRepository: Add methods for fetching places in geo bounds --- .../java/fr/free/nrw/commons/nearby/PlaceDao.java | 6 ++++++ .../nrw/commons/nearby/PlacesLocalDataSource.java | 6 ++++++ .../fr/free/nrw/commons/nearby/PlacesRepository.java | 6 ++++++ .../nearby/presenter/NearbyParentFragmentPresenter.kt | 11 ++++++++++- 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/PlaceDao.java b/app/src/main/java/fr/free/nrw/commons/nearby/PlaceDao.java index 9e4292114..a5a54ec4a 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/PlaceDao.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/PlaceDao.java @@ -5,6 +5,7 @@ import androidx.room.Insert; import androidx.room.OnConflictStrategy; import androidx.room.Query; import io.reactivex.Completable; +import java.util.List; /** * Data Access Object (DAO) for accessing the Place entity in the database. @@ -32,6 +33,11 @@ public abstract class PlaceDao { @Query("SELECT * from place WHERE entityID=:entity") public abstract Place getPlace(String entity); + @Query("SELECT * from place WHERE latitude>=:latBegin AND longitude>=:lngBegin " + + "AND latitude<:latEnd AND longitude<:lngEnd") + public abstract List fetchPlaces(double latBegin, double lngBegin, + double latEnd, double lngEnd); + /** * Saves a Place object asynchronously into the database. */ diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/PlacesLocalDataSource.java b/app/src/main/java/fr/free/nrw/commons/nearby/PlacesLocalDataSource.java index 86a57eadc..e48001e8e 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/PlacesLocalDataSource.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/PlacesLocalDataSource.java @@ -1,6 +1,7 @@ package fr.free.nrw.commons.nearby; import io.reactivex.Completable; +import java.util.List; import javax.inject.Inject; /** @@ -26,6 +27,11 @@ public class PlacesLocalDataSource { return placeDao.getPlace(entityID); } + public List fetchPlaces(final double latBegin, final double lngBegin, + final double latEnd, final double lngEnd) { + return placeDao.fetchPlaces(latBegin, lngBegin, latEnd, lngEnd); + } + /** * Saves a Place object asynchronously into the database. * diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/PlacesRepository.java b/app/src/main/java/fr/free/nrw/commons/nearby/PlacesRepository.java index 846e54fac..e942b68e3 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/PlacesRepository.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/PlacesRepository.java @@ -4,6 +4,7 @@ import fr.free.nrw.commons.contributions.Contribution; import fr.free.nrw.commons.location.LatLng; import io.reactivex.Completable; import io.reactivex.schedulers.Schedulers; +import java.util.List; import javax.inject.Inject; /** @@ -39,6 +40,11 @@ public class PlacesRepository { return localDataSource.fetchPlace(entityID); } + public List fetchPlaces(final double latBegin, final double lngBegin, + final double latEnd, final double lngEnd) { + return localDataSource.fetchPlaces(latBegin, lngBegin, latEnd, lngEnd); + } + /** * Clears the Nearby cache on an IO thread. * diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/presenter/NearbyParentFragmentPresenter.kt b/app/src/main/java/fr/free/nrw/commons/nearby/presenter/NearbyParentFragmentPresenter.kt index 41d5a448b..a5be715eb 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/presenter/NearbyParentFragmentPresenter.kt +++ b/app/src/main/java/fr/free/nrw/commons/nearby/presenter/NearbyParentFragmentPresenter.kt @@ -54,6 +54,7 @@ class NearbyParentFragmentPresenter private var placeSearchJob: Job? = null private var isSearchInProgress = false + private var localPlaceSearchJob: Job? = null private val clickedPlaces = CopyOnWriteArrayList() @@ -495,8 +496,9 @@ class NearbyParentFragmentPresenter @Override override fun handleMapScrolled(scope: LifecycleCoroutineScope?) { scope ?: return + placeSearchJob?.cancel() - placeSearchJob = scope.launch { + placeSearchJob = scope.launch(Dispatchers.Main) { delay(SCROLL_DELAY) if (!isSearchInProgress) { isSearchInProgress = true; // search executing flag @@ -508,6 +510,12 @@ class NearbyParentFragmentPresenter } } } + + localPlaceSearchJob?.cancel() + localPlaceSearchJob = scope.launch { + delay(LOCAL_SCROLL_DELAY) + + } } /** @@ -583,6 +591,7 @@ class NearbyParentFragmentPresenter companion object { private const val SCROLL_DELAY = 800L; // Delay for debounce of onscroll, in milliseconds. + private const val LOCAL_SCROLL_DELAY = 200L; // SCROLL_DELAY but for local db place search private val DUMMY = Proxy.newProxyInstance( NearbyParentFragmentContract.View::class.java.getClassLoader(), arrayOf>(NearbyParentFragmentContract.View::class.java),