package com.jraska.github.client.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;
import com.jraska.github.client.R;
import com.jraska.github.client.users.User;
import com.jraska.github.client.users.UserDetail;
import com.jraska.github.client.users.UserStats;
import com.jraska.github.client.users.UsersRepository;
import com.squareup.picasso.Picasso;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import javax.inject.Inject;
import java.text.DateFormat;
import java.util.Date;

public class UserDetailActivity extends BaseActivity {

  static final DateFormat JOINED_FORMAT = DateFormat.getDateInstance(DateFormat.MEDIUM);
  static final String EXTRA_USER_KEY = "user";

  @Bind(R.id.user_detail_avatar) ImageView _avatarView;
  @Bind(R.id.user_detail_repos_count) TextView _reposCountTextView;
  @Bind(R.id.user_detail_following_count) TextView _followingTextView;
  @Bind(R.id.user_detail_followers_count) TextView _followersTextView;
  @Bind(R.id.user_detail_joined) TextView _joinedTextView;

  @Inject Picasso _picasso;
  @Inject GitHubIconClickHandler _gitHubIconClickHandler;
  @Inject UsersRepository _usersRepository;

  private User _user;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_detail);
    getComponent().inject(this);

    _user = getIntent().getParcelableExtra(EXTRA_USER_KEY);

    setTitle(_user._login);
    _picasso.load(_user._avatarUrl).into(_avatarView);

    _usersRepository.getUserDetail(_user._login)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(this::setUserDetail);
  }

  @OnClick(R.id.user_detail_github_fab) void gitHubFabClicked() {
    _gitHubIconClickHandler.userGitHubClicked(_user);
  }

  void setUserDetail(UserDetail userDetail) {
    UserStats basicStats = userDetail._basicStats;

    _followersTextView.setText(String.valueOf(basicStats._followers));
    _followingTextView.setText(String.valueOf(basicStats._following));
    _reposCountTextView.setText(String.valueOf(basicStats._publicRepos));

    String joinedDateText = formatJoinedDate(basicStats._joined);
    String joinedText = getString(R.string.user_detail_joined_template, joinedDateText);
    _joinedTextView.setText(joinedText);
  }

  static String formatJoinedDate(Date joinedDate) {
    synchronized (JOINED_FORMAT) {
      return JOINED_FORMAT.format(joinedDate);
    }
  }

  public static void start(Activity inActivity, User user) {
    Intent intent = new Intent(inActivity, UserDetailActivity.class);
    intent.putExtra(EXTRA_USER_KEY, user);

    inActivity.startActivity(intent);
  }
}

@@ -178,24 +178,53 @@
 t.TextView;%0A
+import android.widget.Toast;%0A
 import butte
@@ -236,16 +236,16 @@
 e.Bind;%0A
-
 import b
@@ -299,16 +299,180 @@
 ient.R;%0A
+import com.jraska.github.client.rx.ActivityErrorMethod;%0Aimport com.jraska.github.client.rx.ActivityNextMethod;%0Aimport com.jraska.github.client.rx.ObservableLoader;%0A
 import c
@@ -701,89 +701,8 @@
 sso;
-%0Aimport rx.android.schedulers.AndroidSchedulers;%0Aimport rx.schedulers.Schedulers;
 %0A%0Aim
@@ -836,16 +836,253 @@
 vity %7B%0A%0A
+  static final ActivityNextMethod%3CUserDetail, UserDetailActivity%3E SET_USER_METHOD = UserDetailActivity::setUserDetail;%0A  static final ActivityErrorMethod%3CUserDetailActivity%3E USER_LOAD_ERROR_METHOD = UserDetailActivity::onUserLoadError;%0A%0A
   static
@@ -1665,16 +1665,62 @@
 ository;
+%0A  @Inject ObservableLoader _observableLoader;
 %0A%0A  priv
@@ -2073,24 +2073,47 @@
 View);%0A%0A    
+_observableLoader.load(
 _usersReposi
@@ -2148,136 +2148,49 @@
 gin)
-%0A        .subscribeOn(Schedulers.io())%0A        .observeOn(AndroidSchedulers.mainThread())%0A        .subscribe(this::setUserDetail
+, SET_USER_METHOD, USER_LOAD_ERROR_METHOD
 );%0A 
@@ -2788,16 +2788,16 @@
 eText);%0A
-
     _joi
@@ -2834,16 +2834,133 @@
 );%0A  %7D%0A%0A
+  void onUserLoadError(Throwable error) %7B%0A    Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show();%0A  %7D%0A%0A
   static

Use loading fragment in UserDetail activity