package com.schibstedspain.leku.geocoder;

import android.location.Address;
import android.location.Geocoder;
import com.google.android.gms.maps.model.LatLng;
import io.reactivex.Observable;
import java.io.IOException;
import java.util.List;

public class AndroidGeocoderDataSource implements GeocoderInteractorDataSource {

  private final Geocoder geocoder;
  private final static int MAX_RESULTS = 5;

  public AndroidGeocoderDataSource(Geocoder geocoder) {
    this.geocoder = geocoder;
  }

  @Override
  public Observable<List<Address>> getFromLocationName(String query) {
    return Observable.create(subscriber -> {
      try {
        subscriber.onNext(geocoder.getFromLocationName(query, MAX_RESULTS));
        subscriber.onComplete();
      } catch (IOException e) {
        subscriber.onError(e);
      }
    });
  }

  @Override
  public Observable<List<Address>> getFromLocationName(String query, LatLng lowerLeft,
      LatLng upperRight) {
    return Observable.create(subscriber -> {
      try {
        subscriber.onNext(geocoder.getFromLocationName(query, MAX_RESULTS, lowerLeft.latitude,
            lowerLeft.longitude, upperRight.latitude, upperRight.longitude));
        subscriber.onComplete();
      } catch (IOException e) {
        subscriber.onError(e);
      }
    });
  }

  @Override
  public Observable<List<Address>> getFromLocation(double latitude, double longitude) {
    return Observable.create(subscriber -> {
      try {
        subscriber.onNext(geocoder.getFromLocation(latitude, longitude, MAX_RESULTS));
        subscriber.onComplete();
      } catch (IOException e) {
        subscriber.onError(e);
      }
    });
  }
}

@@ -597,32 +597,29 @@
 able.create(
-subscrib
+emitt
 er -%3E %7B%0A    
@@ -630,32 +630,29 @@
 y %7B%0A        
-subscrib
+emitt
 er.onNext(ge
@@ -704,32 +704,29 @@
 ));%0A        
-subscrib
+emitt
 er.onComplet
@@ -766,36 +766,36 @@
 ) %7B%0A        
-subscriber.o
+emitter.tryO
 nError(e);%0A 
@@ -965,32 +965,29 @@
 able.create(
-subscrib
+emitt
 er -%3E %7B%0A    
@@ -998,32 +998,29 @@
 y %7B%0A        
-subscrib
+emitt
 er.onNext(ge
@@ -1168,32 +1168,29 @@
 ));%0A        
-subscrib
+emitt
 er.onComplet
@@ -1230,36 +1230,36 @@
 ) %7B%0A        
-subscriber.o
+emitter.tryO
 nError(e);%0A 
@@ -1407,24 +1407,21 @@
 .create(
-subscrib
+emitt
 er -%3E %7B%0A
@@ -1436,32 +1436,29 @@
 y %7B%0A        
-subscrib
+emitt
 er.onNext(ge
@@ -1520,32 +1520,29 @@
 ));%0A        
-subscrib
+emitt
 er.onComplet
@@ -1590,20 +1590,20 @@
     
-subscriber.o
+emitter.tryO
 nErr

Replace onError with tryOnError to avoid UndeliverableException (#129)