// Path: app/src/main/java/com/packtpub/apps/rxjava_essentials/chapter8/api/openweathermap/OpenWeatherMapApiManager.java
// public class OpenWeatherMapApiManager {
// 
//   @Getter private static OpenWeatherMapApiManager instance = new OpenWeatherMapApiManager();
// 
//   private final OpenWeatherMapService mOpenWeatherMapService;
// 
//   private OpenWeatherMapApiManager() {
//     RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint("http://api.openweathermap.org")
//         .setLogLevel(RestAdapter.LogLevel.BASIC)
//         .build();
// 
//     mOpenWeatherMapService = restAdapter.create(OpenWeatherMapService.class);
//   }
// 
//   public Observable<WeatherResponse> getForecastByCity(String city) {
//     return mOpenWeatherMapService.getForecastByCity(city)
//         .subscribeOn(Schedulers.io())
//         .observeOn(AndroidSchedulers.mainThread());
//   }
// }


import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.packtpub.apps.rxjava_essentials.App;
import com.packtpub.apps.rxjava_essentials.R;
import com.packtpub.apps.rxjava_essentials.chapter8.api.openweathermap.OpenWeatherMapApiManager;
import com.packtpub.apps.rxjava_essentials.chapter8.api.openweathermap.models.WeatherResponse;
import com.packtpub.apps.rxjava_essentials.chapter8.api.stackexchange.models.User;
import java.util.ArrayList;
import java.util.List;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.android.view.ViewObservable;
import rx.schedulers.Schedulers;
import static rx.android.internal.Preconditions.checkNotNull;
      });
    }

    public void setUser(User user) {
      name.setText(user.getDisplayName());
      city.setText(user.getLocation());
      reputation.setText(String.valueOf(user.getReputation()));

      ImageLoader.getInstance().displayImage(user.getProfileImage(), user_image);

      displayWeatherInfos(user);

      ViewObservable.clicks(mView).subscribe(onClickEvent -> {
        checkNotNull(mProfileListener, "Must implement OpenProfileListener");

        String url = user.getWebsiteUrl();
        if (url != null && !url.equals("") && !url.contains("search")) {
          mProfileListener.open(url);
        } else {
          mProfileListener.open(user.getLink());
        }
      });
    }

    private void displayWeatherInfos(User user) {
      String location = user.getLocation();
      int separatorPosition = getSeparatorPosition(location);

      if (isCityValid(location)) {
        String city = getCity(location, separatorPosition);
// Path: app/src/main/java/com/packtpub/apps/rxjava_essentials/chapter8/api/openweathermap/OpenWeatherMapApiManager.java
// public class OpenWeatherMapApiManager {
// 
//   @Getter private static OpenWeatherMapApiManager instance = new OpenWeatherMapApiManager();
// 
//   private final OpenWeatherMapService mOpenWeatherMapService;
// 
//   private OpenWeatherMapApiManager() {
//     RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint("http://api.openweathermap.org")
//         .setLogLevel(RestAdapter.LogLevel.BASIC)
//         .build();
// 
//     mOpenWeatherMapService = restAdapter.create(OpenWeatherMapService.class);
//   }
// 
//   public Observable<WeatherResponse> getForecastByCity(String city) {
//     return mOpenWeatherMapService.getForecastByCity(city)
//         .subscribeOn(Schedulers.io())
//         .observeOn(AndroidSchedulers.mainThread());
//   }
// }


// Path: app/src/main/java/com/packtpub/apps/rxjava_essentials/chapter8/SoAdapter.java
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.packtpub.apps.rxjava_essentials.App;
import com.packtpub.apps.rxjava_essentials.R;
import com.packtpub.apps.rxjava_essentials.chapter8.api.openweathermap.OpenWeatherMapApiManager;
import com.packtpub.apps.rxjava_essentials.chapter8.api.openweathermap.models.WeatherResponse;
import com.packtpub.apps.rxjava_essentials.chapter8.api.stackexchange.models.User;
import java.util.ArrayList;
import java.util.List;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.android.view.ViewObservable;
import rx.schedulers.Schedulers;
import static rx.android.internal.Preconditions.checkNotNull;

      });
    }

    public void setUser(User user) {
      name.setText(user.getDisplayName());
      city.setText(user.getLocation());
      reputation.setText(String.valueOf(user.getReputation()));

      ImageLoader.getInstance().displayImage(user.getProfileImage(), user_image);

      displayWeatherInfos(user);

      ViewObservable.clicks(mView).subscribe(onClickEvent -> {
        checkNotNull(mProfileListener, "Must implement OpenProfileListener");

        String url = user.getWebsiteUrl();
        if (url != null && !url.equals("") && !url.contains("search")) {
          mProfileListener.open(url);
        } else {
          mProfileListener.open(user.getLink());
        }
      });
    }

    private void displayWeatherInfos(User user) {
      String location = user.getLocation();
      int separatorPosition = getSeparatorPosition(location);

      if (isCityValid(location)) {
        String city = getCity(location, separatorPosition);
        OpenWeatherMapApiManager.getInstance()