|
108 | 108 | import fr.free.nrw.commons.utils.SystemThemeUtils;
|
109 | 109 | import fr.free.nrw.commons.utils.ViewUtil;
|
110 | 110 | import fr.free.nrw.commons.wikidata.WikidataEditListener;
|
| 111 | +import io.reactivex.Completable; |
111 | 112 | import io.reactivex.Observable;
|
112 | 113 | import io.reactivex.android.schedulers.AndroidSchedulers;
|
113 | 114 | import io.reactivex.disposables.Disposable;
|
@@ -342,9 +343,21 @@ public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup
|
342 | 343 | public void onCreateOptionsMenu(@NonNull final Menu menu,
|
343 | 344 | @NonNull final MenuInflater inflater) {
|
344 | 345 | inflater.inflate(R.menu.nearby_fragment_menu, menu);
|
| 346 | + MenuItem refreshButton = menu.findItem(R.id.item_refresh); |
345 | 347 | MenuItem listMenu = menu.findItem(R.id.list_sheet);
|
346 | 348 | MenuItem saveAsGPXButton = menu.findItem(R.id.list_item_gpx);
|
347 | 349 | MenuItem saveAsKMLButton = menu.findItem(R.id.list_item_kml);
|
| 350 | + refreshButton.setOnMenuItemClickListener(new OnMenuItemClickListener() { |
| 351 | + @Override |
| 352 | + public boolean onMenuItemClick(MenuItem item) { |
| 353 | + try { |
| 354 | + emptyCache(); |
| 355 | + } catch (Exception e) { |
| 356 | + throw new RuntimeException(e); |
| 357 | + } |
| 358 | + return false; |
| 359 | + } |
| 360 | + }); |
348 | 361 | listMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
|
349 | 362 | @Override
|
350 | 363 | public boolean onMenuItemClick(MenuItem item) {
|
@@ -1158,6 +1171,48 @@ public void populatePlaces(final LatLng currentLatLng,
|
1158 | 1171 | }
|
1159 | 1172 | }
|
1160 | 1173 |
|
| 1174 | + /** |
| 1175 | + * Reloads the Nearby map |
| 1176 | + * Clears all location markers, refreshes them, reinserts them into the map. |
| 1177 | + * |
| 1178 | + */ |
| 1179 | + private void reloadMap() { |
| 1180 | + clearAllMarkers(); // Clear the list of markers |
| 1181 | + binding.map.getController().setZoom(ZOOM_LEVEL); // Reset the zoom level |
| 1182 | + binding.map.getController().setCenter(lastMapFocus); // Recenter the focus |
| 1183 | + if (locationPermissionsHelper.checkLocationPermission(getActivity())) { |
| 1184 | + locationPermissionGranted(); // Reload map with user's location |
| 1185 | + } else { |
| 1186 | + startMapWithoutPermission(); // Reload map without user's location |
| 1187 | + } |
| 1188 | + binding.map.invalidate(); // Invalidate the map |
| 1189 | + presenter.updateMapAndList(LOCATION_SIGNIFICANTLY_CHANGED); // Restart the map |
| 1190 | + Timber.d("Reloaded Map Successfully"); |
| 1191 | + } |
| 1192 | + |
| 1193 | + |
| 1194 | + /** |
| 1195 | + * Clears the Nearby local cache and then calls for the map to be reloaded |
| 1196 | + * |
| 1197 | + */ |
| 1198 | + private void emptyCache() { |
| 1199 | + // reload the map once the cache is cleared |
| 1200 | + compositeDisposable.add( |
| 1201 | + placesRepository.clearCache() |
| 1202 | + .subscribeOn(Schedulers.io()) |
| 1203 | + .observeOn(AndroidSchedulers.mainThread()) |
| 1204 | + .andThen(Completable.fromAction(this::reloadMap)) |
| 1205 | + .subscribe( |
| 1206 | + () -> { |
| 1207 | + Timber.d("Nearby Cache cleared successfully."); |
| 1208 | + }, |
| 1209 | + throwable -> { |
| 1210 | + Timber.e(throwable, "Failed to clear the Nearby Cache"); |
| 1211 | + } |
| 1212 | + ) |
| 1213 | + ); |
| 1214 | + } |
| 1215 | + |
1161 | 1216 | private void savePlacesAsKML() {
|
1162 | 1217 | final Observable<String> savePlacesObservable = Observable
|
1163 | 1218 | .fromCallable(() -> nearbyController
|
|
0 commit comments