TestSearchPresenter.kt 4.06 KB
Newer Older
konghaorui committed
1 2
package com.yidianling.tests.search

konghaorui committed
3
import android.annotation.SuppressLint
konghaorui committed
4 5
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
konghaorui committed
6
import com.ydl.ydlcommon.base.BaseApp
konghaorui committed
7 8
import com.ydl.ydlcommon.data.http.RxUtils
import com.ydl.ydlcommon.data.http.ThrowableConsumer
konghaorui committed
9 10 11
import com.ydl.ydlcommon.mvp.base.SimplePresenter
import com.ydl.ydlcommon.utils.NetworkParamsUtils
import com.ydl.ydlcommon.utils.remind.HttpErrorUtils
konghaorui committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25
import com.yidianling.common.tools.ToastUtil
import com.yidianling.tests.TestRetrofitApi
import com.yidianling.tests.home.http.TestHomeDataManager
import com.yidianling.tests.home.param.TestHomeParam
import com.yidianling.tests.list.model.TestListCommand
import com.yidianling.tests.list.model.bean.Test
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers

/**
 * TestSearchPresenter
 * Created by zqk on 17-12-5.
 */
konghaorui committed
26
class TestSearchPresenter : SimplePresenter<TestSearchView>() {
konghaorui committed
27

konghaorui committed
28
    @SuppressLint("CheckResult")
konghaorui committed
29
    fun fetchHotTests() {
konghaorui committed
30
        mView.showLoadingView()
konghaorui committed
31 32 33 34 35
        val cmd = TestListCommand()
        cmd.tab = "hot_test"
        cmd.page = 1
        TestRetrofitApi.getTestRetrofitApi()
                .fetchTopSearch()
konghaorui committed
36
                .compose(RxUtils.applySchedulers(mView))
konghaorui committed
37 38
                .subscribe({ resp ->
                    if (resp.code == "200") {
konghaorui committed
39 40
                        mView.hideNoResultView()
                        mView.onRecommendSearchListResponse(resp.data)
konghaorui committed
41 42
                    } else {
                        ToastUtil.toastShort(resp.msg)
konghaorui committed
43
                        mView.hideLoadingView()
konghaorui committed
44 45 46
                    }

                }, { throwble ->
konghaorui committed
47
                    HttpErrorUtils.handleError(BaseApp.getApp(), throwble)
konghaorui committed
48 49 50 51 52 53 54
                })
    }


    /**
     * 测评热门推荐
     */
konghaorui committed
55
    @SuppressLint("CheckResult")
konghaorui committed
56
    fun hotRecommendRequest() {
konghaorui committed
57
        mView.showLoadingView()
konghaorui committed
58 59 60
        TestHomeDataManager
                .getHttp()
                .newHomeRequest(TestHomeParam( 0))
konghaorui committed
61
                .compose(RxUtils.applySchedulers(mView))
konghaorui committed
62 63 64 65 66 67
                .compose(RxUtils.resultData())
                .subscribe(Consumer {
                    if (it[3].body!=null){
                        var jsonStr = Gson().toJson(it[3].body)
                        val list = Gson().fromJson<MutableList<Test>>(jsonStr, object : TypeToken<MutableList<Test>>() {
                        }.type)
konghaorui committed
68 69
                        mView.hideNoResultView()
                        mView.onHotRecommendResponse(list)
konghaorui committed
70
                    }else{
konghaorui committed
71
                        mView.hideLoadingView()
konghaorui committed
72 73 74
                    }
                }, object : ThrowableConsumer() {
                    override fun accept(msg: String) {
konghaorui committed
75
                        mView.showError(msg)
konghaorui committed
76 77 78 79 80 81
                    }
                })
    }

    fun searchTests(keyword: String?, page: Int) {
        if (page == 1) {
konghaorui committed
82
            mView.showLoadingView()
konghaorui committed
83 84 85 86 87 88
        }
        val cmd = TestListCommand()
        cmd.tab = "search"
        cmd.page = page
        cmd.keyword = keyword
        TestRetrofitApi.getTestRetrofitApi()
konghaorui committed
89
                .fetchTestList(NetworkParamsUtils.getMaps(cmd))
konghaorui committed
90 91 92 93
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ resp ->
                    if (resp.code == 0) {
konghaorui committed
94
                        mView.hideLoadingView()
konghaorui committed
95
                        if (page == 1 && (resp.data.testList == null || resp.data.testList?.size == 0)) {
konghaorui committed
96 97
                            mView.showNoResultView()
                            mView.showSearchResultView(resp.data.testList, page)
konghaorui committed
98
                        } else {
konghaorui committed
99 100
                            mView.hideNoResultView()
                            mView.showSearchResultView(resp.data.testList, page)
konghaorui committed
101 102
                        }
                    } else {
konghaorui committed
103
                        mView.hideLoadingView()
konghaorui committed
104 105 106
                        ToastUtil.toastShort(resp.msg)
                    }
                }, { throwble ->
konghaorui committed
107
                    HttpErrorUtils.handleError(BaseApp.getApp(), throwble)
konghaorui committed
108 109 110
                })
    }
}