Skip to content

Commit 5bc5828

Browse files
authored
Nearby: Avoid reloading entire map upon cache clear (commons-app#6089)
1 parent a644496 commit 5bc5828

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java

+18-22
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
import fr.free.nrw.commons.nearby.NearbyFilterState;
9393
import fr.free.nrw.commons.nearby.Place;
9494
import fr.free.nrw.commons.nearby.PlacesRepository;
95+
import fr.free.nrw.commons.nearby.Sitelinks;
9596
import fr.free.nrw.commons.nearby.WikidataFeedback;
9697
import fr.free.nrw.commons.nearby.contract.NearbyParentFragmentContract;
9798
import fr.free.nrw.commons.nearby.fragments.AdvanceQueryFragment.Callback;
@@ -1173,27 +1174,7 @@ public void populatePlaces(final LatLng currentLatLng,
11731174
}
11741175

11751176
/**
1176-
* Reloads the Nearby map
1177-
* Clears all location markers, refreshes them, reinserts them into the map.
1178-
*
1179-
*/
1180-
private void reloadMap() {
1181-
clearAllMarkers(); // Clear the list of markers
1182-
binding.map.getController().setZoom(ZOOM_LEVEL); // Reset the zoom level
1183-
binding.map.getController().setCenter(lastMapFocus); // Recenter the focus
1184-
if (locationPermissionsHelper.checkLocationPermission(getActivity())) {
1185-
locationPermissionGranted(); // Reload map with user's location
1186-
} else {
1187-
startMapWithoutPermission(); // Reload map without user's location
1188-
}
1189-
binding.map.invalidate(); // Invalidate the map
1190-
presenter.updateMapAndList(LOCATION_SIGNIFICANTLY_CHANGED); // Restart the map
1191-
Timber.d("Reloaded Map Successfully");
1192-
}
1193-
1194-
1195-
/**
1196-
* Clears the Nearby local cache and then calls for the map to be reloaded
1177+
* Clears the Nearby local cache and then calls for pin details to be fetched afresh.
11971178
*
11981179
*/
11991180
private void emptyCache() {
@@ -1202,7 +1183,22 @@ private void emptyCache() {
12021183
placesRepository.clearCache()
12031184
.subscribeOn(Schedulers.io())
12041185
.observeOn(AndroidSchedulers.mainThread())
1205-
.andThen(Completable.fromAction(this::reloadMap))
1186+
.andThen(Completable.fromAction(() -> {
1187+
// reload only the pin details, by making all loaded pins gray:
1188+
ArrayList<MarkerPlaceGroup> newPlaceGroups = new ArrayList<>(
1189+
NearbyController.markerLabelList.size());
1190+
for (final MarkerPlaceGroup placeGroup : NearbyController.markerLabelList) {
1191+
final Place place = new Place("", "", placeGroup.getPlace().getLabel(), "",
1192+
placeGroup.getPlace().getLocation(), "",
1193+
placeGroup.getPlace().siteLinks, "", placeGroup.getPlace().exists,
1194+
placeGroup.getPlace().entityID);
1195+
place.setDistance(placeGroup.getPlace().distance);
1196+
place.setMonument(placeGroup.getPlace().isMonument());
1197+
newPlaceGroups.add(
1198+
new MarkerPlaceGroup(placeGroup.getIsBookmarked(), place));
1199+
}
1200+
presenter.loadPlacesDataAsync(newPlaceGroups, scope);
1201+
}))
12061202
.subscribe(
12071203
() -> {
12081204
Timber.d("Nearby Cache cleared successfully.");

app/src/main/java/fr/free/nrw/commons/nearby/presenter/NearbyParentFragmentPresenter.kt

+18-1
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,27 @@ class NearbyParentFragmentPresenter
300300
}
301301
?: return
302302

303-
loadPlacesDataAyncJob?.cancel()
304303
lockUnlockNearby(false) // So that new location updates wont come
305304
nearbyParentFragmentView.setProgressBarVisibility(false)
305+
loadPlacesDataAsync(nearbyPlaceGroups, scope)
306+
}
306307

308+
/**
309+
* Load the places' details from cache and Wikidata query, and update these details on the map
310+
* as and when they arrive.
311+
*
312+
* @param nearbyPlaceGroups The list of `MarkerPlaceGroup` objects to be rendered on the map.
313+
* Note that the supplied objects' `isBookmarked` property can be set false as the actual
314+
* value is retrieved from the bookmarks db eventually.
315+
* @param scope the lifecycle scope of `nearbyParentFragment`'s `viewLifecycleOwner`
316+
*
317+
* @see LoadPlacesAsyncOptions
318+
*/
319+
fun loadPlacesDataAsync(
320+
nearbyPlaceGroups: List<MarkerPlaceGroup>,
321+
scope: LifecycleCoroutineScope?
322+
) {
323+
loadPlacesDataAyncJob?.cancel()
307324
loadPlacesDataAyncJob = scope?.launch(Dispatchers.IO) {
308325
// clear past clicks and bookmarkChanged queues
309326
clickedPlaces.clear()

0 commit comments

Comments
 (0)