Skip to content

Instantly share code, notes, and snippets.

@talosdev
Last active May 16, 2018 14:41
Show Gist options
  • Select an option

  • Save talosdev/f8052ba03e3cd82a5503fbd5fa236190 to your computer and use it in GitHub Desktop.

Select an option

Save talosdev/f8052ba03e3cd82a5503fbd5fa236190 to your computer and use it in GitHub Desktop.
public class LocationPresenter implements LocationContract.Presenter {
private static final String TAG = "LOCATION";
private final WeakReference<LocationContract.View> viewWeakReference;
private final LocationInteractor interactor;
private final CompositeDisposable disposables = new CompositeDisposable();
@Inject
public LocationPresenter(LocationContract.View view, LocationInteractor interactor) {
this.viewWeakReference = new WeakReference<>(view);
this.interactor = interactor;
}
@Override
public void getLocation() {
disposables.add(
interactor.getLocation()
.subscribe(
location -> {
LocationContract.View view = viewWeakReference.get();
if (view != null) {
view.showLatitude(String.valueOf(location.latitude()));
view.showLongitude(String.valueOf(location.longitude()));
}
},
throwable -> {
LocationContract.View view = viewWeakReference.get();
if (view != null) {
Log.e(TAG, "Error while getting location", throwable);
if (throwable instanceof NoLocationAvailableException) {
view.showNoLocationAvailable();
} else {
view.showGenericError();
}
}
}
)
);
}
@Override
public void cleanup() {
disposables.clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment