package com.yidianling.tests.list.presenter

import com.yidianling.tests.list.model.TestListCommand
import com.yidianling.tests.TestRetrofitApi
import com.yidianling.tests.list.view.TestListFragmentView
import com.hannesdorfmann.mosby3.mvp.MvpNullObjectBasePresenter
import com.yidianling.ydlcommon.http.YdlRetrofitUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * TestListFragmentPresenter
 * Created by zqk on 17-11-22.
 */
class TestListFragmentPresenter : MvpNullObjectBasePresenter<TestListFragmentView>() {
    fun fetchTestList(type: String?, currentPage: Int) {
        val cmd = TestListCommand()
        cmd.tab = type
        cmd.page = currentPage
        TestRetrofitApi.getTestRetrofitApi()
                .fetchTestList(YdlRetrofitUtils.getMaps(cmd))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ resp ->
                    view.dismissLoading()
                    if (resp.code == 0) {
                        view.onTestListFetched(resp.data, currentPage)
                    } else {
                        view.onFailed(resp.msg)
                    }
                }, { t ->
                    view.onError(t)
                })

    }
}