package com.github.piasy.template.ui.search.mvp;

import android.support.annotation.NonNull;
import com.github.piasy.common.android.utils.net.RxUtil;
import com.github.piasy.common.android.utils.roms.MiUIUtil;
import com.github.piasy.model.dao.GithubUserDAO;
import com.github.piasy.template.TemplateApp;
import com.github.piasy.template.base.mvp.BaseRxPresenter;
import de.greenrobot.event.EventBus;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import timber.log.Timber;

/**
 * Created by Piasy{github.com/Piasy} on 15/7/24.
 */
public class GithubSearchPresenterImpl extends BaseRxPresenter<GithubSearchView>
        implements GithubSearchPresenter {

    private final EventBus mBus;
    private final GithubUserDAO mGithubUserDAO;
    private final RxUtil.RxErrorProcessor mRxErrorProcessor;
    private final MiUIUtil mMiUIUtil;

    public GithubSearchPresenterImpl(EventBus bus, GithubUserDAO githubUserDAO,
            RxUtil.RxErrorProcessor rxErrorProcessor, MiUIUtil miUIUtil) {
        mBus = bus;
        mGithubUserDAO = githubUserDAO;
        mRxErrorProcessor = rxErrorProcessor;
        mMiUIUtil = miUIUtil;
    }

    @NonNull
    @Override
    protected EventBus getEventBus() {
        return mBus;
    }

    @Override
    public void loadUser() {
        // load user from dao, dao take responsibility for where to get the real data
        addSubscription(mGithubUserDAO.getUsers()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(githubUsers -> {
                    if (isViewAttached()) {
                        getView().showSearchUserResult(githubUsers);
                    }
                }, mRxErrorProcessor));

        Timber.i("AutoBoot: Presenter, " + TemplateApp.getInstance().getStartFrom());
        if (TemplateApp.getInstance().getStartFrom() != TemplateApp.StartFrom.BOOT &&
                mMiUIUtil.isMiUI()) {
            addSubscription(mGithubUserDAO.getUsers()
                    .subscribeOn(Schedulers.io())
                    .take(1)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(githubUsers -> {
                        if (isViewAttached()) {
                            getView().showHelpBar();
                        }
                    }, mRxErrorProcessor));
        }
    }
}

@@ -256,54 +256,8 @@
 AO;%0A
-import com.github.piasy.template.TemplateApp;%0A
 impo
@@ -432,34 +432,8 @@
 ers;
-%0Aimport timber.log.Timber;
 %0A%0A/*
@@ -1692,644 +1692,8 @@
 r));
-%0A%0A        Timber.i(%22AutoBoot: Presenter, %22 + TemplateApp.getInstance().getStartFrom());%0A        if (TemplateApp.getInstance().getStartFrom() != TemplateApp.StartFrom.BOOT &&%0A                mMiUIUtil.isMiUI()) %7B%0A            addSubscription(mGithubUserDAO.getUsers()%0A                    .subscribeOn(Schedulers.io())%0A                    .take(1)%0A                    .observeOn(AndroidSchedulers.mainThread())%0A                    .subscribe(githubUsers -%3E %7B%0A                        if (isViewAttached()) %7B%0A                            getView().showHelpBar();%0A                        %7D%0A                    %7D, mRxErrorProcessor));%0A        %7D
 %0A   

build fix