diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/contract/NearbyParentFragmentContract.java b/app/src/main/java/fr/free/nrw/commons/nearby/contract/NearbyParentFragmentContract.java index 98e84858a..ef3d82d47 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/contract/NearbyParentFragmentContract.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/contract/NearbyParentFragmentContract.java @@ -91,6 +91,10 @@ public interface NearbyParentFragmentContract { LatLng getMapFocus(); + LatLng getScreenTopRight(); + + LatLng getScreenBottomLeft(); + boolean isAdvancedQueryFragmentVisible(); void showHideAdvancedQueryFragment(boolean shouldShow); @@ -130,6 +134,6 @@ public interface NearbyParentFragmentContract { void toggleBookmarkedStatus(Place place); - void handleMapScrolled(LifecycleCoroutineScope scope); + void handleMapScrolled(LifecycleCoroutineScope scope, boolean isNetworkAvailable); } } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java index 094a14a9e..5be19ea40 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java @@ -472,7 +472,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment binding.map.addMapListener(new MapListener() { @Override public boolean onScroll(ScrollEvent event) { - presenter.handleMapScrolled(scope); + presenter.handleMapScrolled(scope, !isNetworkErrorOccurred); return true; } @@ -1055,15 +1055,27 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment } @Override - public void populatePlaces(final LatLng currentLatLng) { - IGeoPoint screenTopRight = binding.map.getProjection() + public LatLng getScreenTopRight() { + final IGeoPoint screenTopRight = binding.map.getProjection() .fromPixels(binding.map.getWidth(), 0); - IGeoPoint screenBottomLeft = binding.map.getProjection() - .fromPixels(0, binding.map.getHeight()); - LatLng screenTopRightLatLng = new LatLng( - screenBottomLeft.getLatitude(), screenBottomLeft.getLongitude(), 0); - LatLng screenBottomLeftLatLng = new LatLng( + return new LatLng( screenTopRight.getLatitude(), screenTopRight.getLongitude(), 0); + } + + @Override + public LatLng getScreenBottomLeft() { + final IGeoPoint screenBottomLeft = binding.map.getProjection() + .fromPixels(0, binding.map.getHeight()); + return new LatLng( + screenBottomLeft.getLatitude(), screenBottomLeft.getLongitude(), 0); + } + + @Override + public void populatePlaces(final LatLng currentLatLng) { + // these two variables have historically been assigned values the opposite of what their + // names imply, and quite some existing code depends on this fact + LatLng screenTopRightLatLng = getScreenBottomLeft(); + LatLng screenBottomLeftLatLng = getScreenTopRight(); // When the nearby fragment is opened immediately upon app launch, the {screenTopRightLatLng} // and {screenBottomLeftLatLng} variables return {LatLng(0.0,0.0)} as output. @@ -1116,14 +1128,10 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment populatePlaces(currentLatLng); return; } - IGeoPoint screenTopRight = binding.map.getProjection() - .fromPixels(binding.map.getWidth(), 0); - IGeoPoint screenBottomLeft = binding.map.getProjection() - .fromPixels(0, binding.map.getHeight()); - LatLng screenTopRightLatLng = new LatLng( - screenBottomLeft.getLatitude(), screenBottomLeft.getLongitude(), 0); - LatLng screenBottomLeftLatLng = new LatLng( - screenTopRight.getLatitude(), screenTopRight.getLongitude(), 0); + // these two variables have historically been assigned values the opposite of what their + // names imply, and quite some existing code depends on this fact + final LatLng screenTopRightLatLng = getScreenBottomLeft(); + final LatLng screenBottomLeftLatLng = getScreenTopRight(); if (currentLatLng.equals(lastFocusLocation) || lastFocusLocation == null || recenterToUserLocation) { // Means we are checking around current location 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 a5be715eb..1d3d5f30d 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 @@ -494,27 +494,29 @@ class NearbyParentFragmentPresenter } @Override - override fun handleMapScrolled(scope: LifecycleCoroutineScope?) { + override fun handleMapScrolled(scope: LifecycleCoroutineScope?, isNetworkAvailable: Boolean) { scope ?: return placeSearchJob?.cancel() - placeSearchJob = scope.launch(Dispatchers.Main) { - delay(SCROLL_DELAY) - if (!isSearchInProgress) { - isSearchInProgress = true; // search executing flag - // Start Search - try { - searchInTheArea(); - } finally { - isSearchInProgress = false; + localPlaceSearchJob?.cancel() + if (isNetworkAvailable) { + placeSearchJob = scope.launch(Dispatchers.Main) { + delay(SCROLL_DELAY) + if (!isSearchInProgress) { + isSearchInProgress = true; // search executing flag + // Start Search + try { + searchInTheArea(); + } finally { + isSearchInProgress = false; + } } } - } - - localPlaceSearchJob?.cancel() - localPlaceSearchJob = scope.launch { - delay(LOCAL_SCROLL_DELAY) + } else { + localPlaceSearchJob = scope.launch { + delay(LOCAL_SCROLL_DELAY) + } } }