Skip to content

Commit d6ab30f

Browse files
committed
Simplify dispose logic.
1 parent d4f3fa7 commit d6ab30f

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/NearbyFragment.java

+6-15
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import fr.free.nrw.commons.wikidata.WikidataEditListener;
4343
import io.reactivex.Observable;
4444
import io.reactivex.android.schedulers.AndroidSchedulers;
45-
import io.reactivex.disposables.Disposable;
4645
import io.reactivex.schedulers.Schedulers;
4746
import timber.log.Timber;
4847

@@ -85,8 +84,6 @@ public class NearbyFragment extends CommonsDaggerSupportFragment
8584
private BottomSheetBehavior bottomSheetBehaviorForDetails; // Behavior for details bottom sheet
8685

8786
private LatLng curLatLng;
88-
private Disposable placesDisposable;
89-
private Disposable placesDisposableCustom;
9087
private boolean lockNearbyView; //Determines if the nearby places needs to be refreshed
9188
public View view;
9289
private Snackbar snackbar;
@@ -298,7 +295,7 @@ public void refreshView(LocationServiceManager.LocationChangeType locationChange
298295
bundle.clear();
299296
bundle.putString("CurLatLng", gsonCurLatLng);
300297

301-
placesDisposable = Observable.fromCallable(() -> nearbyController
298+
compositeDisposable.add(Observable.fromCallable(() -> nearbyController
302299
.loadAttractionsFromLocation(curLatLng, curLatLng, false, true))
303300
.subscribeOn(Schedulers.io())
304301
.observeOn(AndroidSchedulers.mainThread())
@@ -307,7 +304,7 @@ public void refreshView(LocationServiceManager.LocationChangeType locationChange
307304
Timber.d(throwable);
308305
showErrorMessage(getString(R.string.error_fetching_nearby_places));
309306
progressBar.setVisibility(View.GONE);
310-
});
307+
}));
311308

312309
} else if (locationChangeType
313310
.equals(LOCATION_SLIGHTLY_CHANGED) && nearbyMapFragment != null) {
@@ -335,15 +332,15 @@ public void refreshViewForCustomLocation(LatLng customLatLng, boolean refreshFor
335332

336333
populateForCurrentLocation = refreshForCurrentLocation;
337334
this.customLatLng = customLatLng;
338-
placesDisposableCustom = Observable.fromCallable(() -> nearbyController
335+
compositeDisposable.add(Observable.fromCallable(() -> nearbyController
339336
.loadAttractionsFromLocation(curLatLng, customLatLng, false, populateForCurrentLocation))
340337
.subscribeOn(Schedulers.io())
341338
.observeOn(AndroidSchedulers.mainThread())
342339
.subscribe(this::populatePlacesFromCustomLocation,
343340
throwable -> {
344341
Timber.d(throwable);
345342
showErrorMessage(getString(R.string.error_fetching_nearby_places));
346-
});
343+
}));
347344

348345
if (nearbyMapFragment != null) {
349346
nearbyMapFragment.searchThisAreaButton.setVisibility(View.GONE);
@@ -467,7 +464,7 @@ private void updateMapFragment(boolean updateViaButton, boolean isSlightUpdate,
467464
|| curLatLng.getLongitude() < nearbyMapFragment.boundaryCoordinates[2].getLongitude()
468465
|| curLatLng.getLongitude() > nearbyMapFragment.boundaryCoordinates[3].getLongitude())) {
469466
// populate places
470-
placesDisposable = Observable.fromCallable(() -> nearbyController
467+
compositeDisposable.add(Observable.fromCallable(() -> nearbyController
471468
.loadAttractionsFromLocation(curLatLng, curLatLng, false, updateViaButton))
472469
.subscribeOn(Schedulers.io())
473470
.observeOn(AndroidSchedulers.mainThread())
@@ -476,7 +473,7 @@ private void updateMapFragment(boolean updateViaButton, boolean isSlightUpdate,
476473
Timber.d(throwable);
477474
showErrorMessage(getString(R.string.error_fetching_nearby_places));
478475
progressBar.setVisibility(View.GONE);
479-
});
476+
}));
480477
nearbyMapFragment.setBundleForUpdates(bundle);
481478
nearbyMapFragment.updateMapSignificantlyForCurrentLocation();
482479
updateListFragment();
@@ -782,13 +779,7 @@ public void onAttach(Context context) {
782779
@Override
783780
public void onDestroy() {
784781
super.onDestroy();
785-
if (placesDisposable != null) {
786-
placesDisposable.dispose();
787-
}
788782
wikidataEditListener.setAuthenticationStateListener(null);
789-
if (placesDisposableCustom != null) {
790-
placesDisposableCustom.dispose();
791-
}
792783
}
793784

794785
@Override

0 commit comments

Comments
 (0)