package com.kkk.retrofitdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.kkk.retrofitdemo.bean.Repo;
import com.kkk.retrofitdemo.bean.SearchRepoResult;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    public static final String TAG = MainActivity.class.getSimpleName();

    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.textView);

        //默认CallAdapter
        mTextView.setText("search kylm53's repos:\n");
        final Call<List<Repo>> repoCall = APIUtils.service.listRepos("kylm53");
        repoCall.enqueue(listRepoCallback);
    }

    //查看user所有repo的callback
    Callback<List<Repo>> listRepoCallback = new Callback<List<Repo>>() {
        @Override
        public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
            List<Repo> repos = response.body();
            StringBuilder builder = new StringBuilder();
            for (Repo repo : repos) {
                Log.i(TAG, repo.getFull_name());
                builder.append(repo.getFull_name())
                        .append("\n")
                        .append(repo.getHtml_url())
                        .append("\n\n");
            }
            mTextView.setText(mTextView.getText() + builder.toString());

            //search rxjava
            searchRepo("rxandroid");
        }

        @Override
        public void onFailure(Call<List<Repo>> call, Throwable t) {
            mTextView.setText(mTextView.getText() + t.getLocalizedMessage());
            Log.i(TAG, t.getLocalizedMessage());
        }
    };

    private void searchRepo(String keyword) {
        //与rxjava结合使用
        mTextView.setText(mTextView.getText() + "\n" + "search rxandroid:");
        Observable<SearchRepoResult> searchRepos = APIUtils.service.
                searchRepos(keyword, "stars", "desc");

        searchRepos.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(searchRepoResultSubscriber);
    }

    //搜索repo的subscriber
    Subscriber<SearchRepoResult> searchRepoResultSubscriber = new Subscriber<SearchRepoResult>() {
        @Override
        public void onCompleted() {
            mTextView.setText(mTextView.getText() + "\n" + "completed!!!");
            Toast.makeText(MainActivity.this, "complete", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(Throwable e) {
            mTextView.setText(mTextView.getText() + "\n" + e.getLocalizedMessage());
            Log.e(TAG, e.getLocalizedMessage());
        }

        @Override
        public void onNext(SearchRepoResult repoResult) {
            List<Repo> repos = repoResult.getItems();
            mTextView.setText(mTextView.getText() + "total(" + repoResult.getTotal_count() + ")\n");
            StringBuilder builder = new StringBuilder();
            for (Repo repo : repos) {
                Log.i(TAG, repo.getFull_name());
                builder.append(repo.getFull_name())
                        .append("\n")
                        .append(repo.getHtml_url())
                        .append("\n\n");
            }
            mTextView.setText(mTextView.getText() + builder.toString());
        }
    };
}

@@ -470,16 +470,43 @@
 dulers;%0A
+import rx.functions.Func1;%0A
 import r
@@ -2476,24 +2476,893 @@
 inThread())%0A
+                .map(new Func1%3CSearchRepoResult, String%3E() %7B%0A                    @Override%0A                    public String call(SearchRepoResult repoResult) %7B%0A                        List%3CRepo%3E repos = repoResult.getItems();%0A                        mTextView.setText(mTextView.getText() + %22total(%22 + repoResult.getTotal_count() + %22)%5Cn%22);%0A                        StringBuilder builder = new StringBuilder();%0A                        for (Repo repo : repos) %7B%0A                            Log.i(TAG, repo.getFull_name());%0A                            builder.append(repo.getFull_name())%0A                                    .append(%22%5Cn%22)%0A                                    .append(repo.getHtml_url())%0A                                    .append(%22%5Cn%5Cn%22);%0A                        %7D%0A                        return builder.toString();%0A                    %7D%0A                %7D)%0A
             
@@ -3452,31 +3452,21 @@
 criber%3CS
-earchRepoResult
+tring
 %3E search
@@ -3504,31 +3504,21 @@
 criber%3CS
-earchRepoResult
+tring
 %3E() %7B%0A  
@@ -3994,31 +3994,21 @@
 onNext(S
-earchRepoResult
+tring
  repoRes
@@ -4018,62 +4018,8 @@
 ) %7B%0A
-            List%3CRepo%3E repos = repoResult.getItems();%0A
     
@@ -4061,35 +4061,24 @@
 .getText() +
- %22total(%22 +
  repoResult.
@@ -4080,447 +4080,8 @@
 sult
-.getTotal_count() + %22)%5Cn%22);%0A            StringBuilder builder = new StringBuilder();%0A            for (Repo repo : repos) %7B%0A                Log.i(TAG, repo.getFull_name());%0A                builder.append(repo.getFull_name())%0A                        .append(%22%5Cn%22)%0A                        .append(repo.getHtml_url())%0A                        .append(%22%5Cn%5Cn%22);%0A            %7D%0A            mTextView.setText(mTextView.getText() + builder.toString()
 );%0A 

use map operator