package study.presenter;

import android.os.SystemClock;

import java.util.ArrayList;
import java.util.Random;

import study.model.bean.FruitBean;
import study.presenter.contract.HomepageContract;
import study.sunshine.newTechnique.R;
import study.ui.home.activity.HomepageActivity;
import study.ui.home.adapter.HomepageAdapter;

/**
 * Created by xiaofeng on 2016/12/20.
 */

public class HomepagePresenter implements HomepageContract.Presenter {
    private HomepageContract.View mView;

    private FruitBean[] fruits = {new FruitBean("Apple", R.drawable.apple),
            new FruitBean("Banana", R.drawable.banana), new FruitBean("Pear", R.drawable.pear),
            new FruitBean("Grape", R.drawable.pear), new FruitBean("pineapple", R.drawable.pineapple),
            new FruitBean("strawberry", R.drawable.strawberry), new FruitBean("mango", R.drawable.mango)};
    private ArrayList<FruitBean> fruitList = new ArrayList<>();
    private HomepageAdapter mHomepageAdapter;

    @Override
    public void refreshFruits() {
        new Thread(() -> {
            SystemClock.sleep(1500);
            ((HomepageActivity) mView).runOnUiThread(() -> {
                        initFruitData();
                        mView.refreshFruitAdapter(mHomepageAdapter);
                        mView.setRefreshing(false);
                    }
            );
        }).start();
    }

    @Override
    public void initFruitData() {
        fruitList.clear();
        for (int i = 0; i < 50; i++) {
            fruitList.add(fruits[new Random().nextInt(fruits.length)]);
        }
    }

    @Override
    public void attachView(HomepageContract.View view) {
        mView = view;
    }

    @Override
    public void detachView() {
        this.mView = null;
    }

    @Override
    public void initServerData() {

    }

    @Override
    public void initCacheData() {
        initFruitData();
        mHomepageAdapter = new HomepageAdapter(fruitList);
        mView.setFruitAdapter(mHomepageAdapter);
    }
}

@@ -49,16 +49,41 @@
 emClock;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -130,16 +130,271 @@
 andom;%0A%0A
+import io.reactivex.Observable;%0Aimport io.reactivex.ObservableEmitter;%0Aimport io.reactivex.ObservableOnSubscribe;%0Aimport io.reactivex.android.schedulers.AndroidSchedulers;%0Aimport io.reactivex.functions.Consumer;%0Aimport io.reactivex.schedulers.Schedulers;%0A
 import s
@@ -512,56 +512,8 @@
 .R;%0A
-import study.ui.home.activity.HomepageActivity;%0A
 impo
@@ -673,16 +673,75 @@
 enter %7B%0A
+    private static final String TAG = %22HomepagePresenter%22;%0A
     priv
@@ -1327,27 +1327,246 @@
     
-new Thread(() -%3E %7B%0A
+Observable.create(new ObservableOnSubscribe%3CString%3E() %7B%0A            @Override%0A            public void subscribe(ObservableEmitter%3CString%3E e) throws Exception %7B%0A                Log.d(TAG, %22subscribe: %22+Thread.currentThread().getName());%0A  
     
@@ -1573,16 +1573,18 @@
         
+  
 SystemCl
@@ -1616,54 +1616,194 @@
     
-((HomepageActivity) mView).runOnUi
+    initFruitData();%0A                e.onNext(%22success%22);%0A            %7D%0A        %7D).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.main
 Thread(
-() -%3E
+)).subscribe(new Consumer%3CString%3E()
  %7B%0A 
@@ -1805,32 +1805,42 @@
 ) %7B%0A            
+@Override%0A
             init
@@ -1839,33 +1839,56 @@
     
-initFruitData();%0A        
+public void accept(String s) throws Exception %7B%0A
     
@@ -1956,32 +1956,24 @@
             
-        
 mView.setRef
@@ -1996,32 +1996,24 @@
             
-        
 %7D%0A          
@@ -2014,32 +2014,9 @@
     
-    );%0A        %7D).start(
+%7D
 );%0A 

add rxjava2