Fixes #5806 Implemented "Refresh" button to clear the cache and reload the Nearby map (#5891)

* Changed files required to get the app to run correctly. Removed suspend from affected DAO files and funcs, and changed to (Kotlin v1.9.22) and (Kotlin compiler v1.5.8)

* Created refresh button icon, and added it to the nearby_fragment_menu.xml (header of the nearby page). Created function refresh() in NearbyParentFragment.java to handle refresh functionality.

* Replaced refresh() func with emptyCache() and reloadMap()

* Attempt at reloadMap(), no testing done yet.

* added changes for a possibly working emptyCache implementation (needs testing).

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

---------

Co-authored-by: MarcusBarta <marcusbarta@icloud.com>
This commit is contained in:
Noah Vendrig 2024-10-25 16:19:07 +11:00 committed by GitHub
parent becc07d26b
commit 3e020ed973
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 140 additions and 35 deletions

View file

@ -41,4 +41,23 @@ public abstract class PlaceDao {
saveSynchronous(place);
});
}
/**
* Deletes all Place objects from the database.
*
* @return A Completable that completes once the deletion operation is done.
*/
@Query("DELETE FROM place")
public abstract void deleteAllSynchronous();
/**
* Deletes all Place objects from the database.
*
*/
public Completable deleteAll() {
return Completable
.fromAction(() -> {
deleteAllSynchronous();
});
}
}

View file

@ -35,4 +35,8 @@ public class PlacesLocalDataSource {
public Completable savePlace(Place place) {
return placeDao.save(place);
}
public Completable clearCache() {
return placeDao.deleteAll();
}
}

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,4 +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()
.subscribeOn(Schedulers.io()); // Ensure it runs on IO thread
}
}

View file

@ -108,6 +108,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;
@ -342,9 +343,21 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
public void onCreateOptionsMenu(@NonNull final Menu menu,
@NonNull final MenuInflater inflater) {
inflater.inflate(R.menu.nearby_fragment_menu, menu);
MenuItem refreshButton = menu.findItem(R.id.item_refresh);
MenuItem listMenu = menu.findItem(R.id.list_sheet);
MenuItem saveAsGPXButton = menu.findItem(R.id.list_item_gpx);
MenuItem saveAsKMLButton = menu.findItem(R.id.list_item_kml);
refreshButton.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
try {
emptyCache();
} catch (Exception e) {
throw new RuntimeException(e);
}
return false;
}
});
listMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
@ -1158,6 +1171,48 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
}
}
/**
* Reloads the Nearby map
* Clears all location markers, refreshes them, reinserts them into the map.
*
*/
private void reloadMap() {
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
} else {
startMapWithoutPermission(); // Reload map without user's location
}
binding.map.invalidate(); // Invalidate the map
presenter.updateMapAndList(LOCATION_SIGNIFICANTLY_CHANGED); // Restart the map
Timber.d("Reloaded Map Successfully");
}
/**
* Clears the Nearby local cache and then calls for the map to be reloaded
*
*/
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() {
final Observable<String> savePlacesObservable = Observable
.fromCallable(() -> nearbyController