Tested changes as working, edited emptyCache to correctly clear cache and then reload map

This commit is contained in:
Noah Vendrig 2024-10-23 16:39:17 +11:00
parent 979f3a3fbc
commit 76b3dc85ac
3 changed files with 37 additions and 23 deletions

View file

@ -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

View file

@ -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
}
}

View file

@ -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() {