From 76b3dc85ace335811e02161e47d81c685be538e6 Mon Sep 17 00:00:00 2001 From: Noah Vendrig Date: Wed, 23 Oct 2024 16:39:17 +1100 Subject: [PATCH] Tested changes as working, edited emptyCache to correctly clear cache and then reload map --- .../fr/free/nrw/commons/nearby/PlaceDao.java | 3 +- .../nrw/commons/nearby/PlacesRepository.java | 9 +++- .../fragments/NearbyParentFragment.java | 48 +++++++++++-------- 3 files changed, 37 insertions(+), 23 deletions(-) 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 544a8fe63..3c601d225 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 @@ -52,9 +52,8 @@ public abstract class PlaceDao { public abstract void deleteAllSynchronous(); /** - * Deletes all Place objects asynchronously from the database. + * Deletes all Place objects from the database. * - * @return A Completable that completes once the deletion operation is done. */ public Completable deleteAll() { return Completable 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 a50fed909..846e54fac 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 @@ -3,6 +3,7 @@ package fr.free.nrw.commons.nearby; import fr.free.nrw.commons.contributions.Contribution; import fr.free.nrw.commons.location.LatLng; import io.reactivex.Completable; +import io.reactivex.schedulers.Schedulers; import javax.inject.Inject; /** @@ -38,7 +39,13 @@ public class PlacesRepository { return localDataSource.fetchPlace(entityID); } + /** + * Clears the Nearby cache on an IO thread. + * + * @return A Completable that completes once the cache has been successfully cleared. + */ public Completable clearCache() { - return localDataSource.clearCache(); + return localDataSource.clearCache() + .subscribeOn(Schedulers.io()); // Ensure it runs on IO thread } } 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 c74d805cd..83597d82c 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 @@ -105,6 +105,7 @@ import fr.free.nrw.commons.utils.NetworkUtils; import fr.free.nrw.commons.utils.SystemThemeUtils; import fr.free.nrw.commons.utils.ViewUtil; import fr.free.nrw.commons.wikidata.WikidataEditListener; +import io.reactivex.Completable; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; @@ -319,9 +320,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment @Override public boolean onMenuItemClick(MenuItem item) { try { - // REFRESH BUTTON FUNCTIONALITY HERE emptyCache(); - reloadMap(); } catch (Exception e) { throw new RuntimeException(e); } @@ -1132,34 +1131,43 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment /** * Reloads the Nearby map * Clears all location markers, refreshes them, reinserts them into the map. - * @author Marcus Barta - marcusbarta@icloud.com + * */ private void reloadMap() { - // TODO: Marcus's section - clearAllMarkers(); // clear the list of markers - binding.map.getController().setZoom(ZOOM_LEVEL); // reset the zoom level - binding.map.getController().setCenter(lastMapFocus); // recentre the focus + clearAllMarkers(); // Clear the list of markers + binding.map.getController().setZoom(ZOOM_LEVEL); // Reset the zoom level + binding.map.getController().setCenter(lastMapFocus); // Recenter the focus if (locationPermissionsHelper.checkLocationPermission(getActivity())) { - locationPermissionGranted(); // reload map with user's location + locationPermissionGranted(); // Reload map with user's location } else { - startMapWithoutPermission(); // reload map without user's location + startMapWithoutPermission(); // Reload map without user's location } - binding.map.invalidate(); // invalidate the map - presenter.updateMapAndList(LOCATION_SIGNIFICANTLY_CHANGED); // restart the map + binding.map.invalidate(); // Invalidate the map + presenter.updateMapAndList(LOCATION_SIGNIFICANTLY_CHANGED); // Restart the map + Timber.d("Reloaded Map Successfully"); } /** - * Empties the Nearby local cache + * Clears the Nearby local cache and then calls for the map to be reloaded + * */ - private void emptyCache(){ - Timber.d("Reload: emptyCache"); - - placesRepository.clearCache(); - - // Optionally, clear any in-memory cache or state - updatedPlacesList.clear(); - updatedLatLng = null; + private void emptyCache() { + // reload the map once the cache is cleared + compositeDisposable.add( + placesRepository.clearCache() + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .andThen(Completable.fromAction(this::reloadMap)) + .subscribe( + () -> { + Timber.d("Nearby Cache cleared successfully."); + }, + throwable -> { + Timber.e(throwable, "Failed to clear the Nearby Cache"); + } + ) + ); } private void savePlacesAsKML() {