TestSearchPresenter.kt 4.33 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
import com.ydl.ydlcommon.mvp.base.SimplePresenter
import com.ydl.ydlcommon.utils.NetworkParamsUtils
霍志良 committed
11
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils
konghaorui committed
12
import com.ydl.ydlcommon.utils.remind.HttpErrorUtils
konghaorui committed
13 14
import com.yidianling.common.tools.ToastUtil
import com.yidianling.tests.TestRetrofitApi
霍志良 committed
15
import com.yidianling.tests.home.config.TestBIConstants.Companion.TEST_MAIN_SEARCH_CONTENT_CLICK
konghaorui committed
16 17 18 19 20 21 22 23 24 25 26 27
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
28
class TestSearchPresenter : SimplePresenter<TestSearchView>() {
konghaorui committed
29

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

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


    /**
     * 测评热门推荐
     */
konghaorui committed
57
    @SuppressLint("CheckResult")
konghaorui committed
58
    fun hotRecommendRequest() {
konghaorui committed
59
        mView.showLoadingView()
konghaorui committed
60 61 62
        TestHomeDataManager
                .getHttp()
                .newHomeRequest(TestHomeParam( 0))
konghaorui committed
63
                .compose(RxUtils.applySchedulers(mView))
konghaorui committed
64 65 66 67 68 69
                .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
70 71
                        mView.hideNoResultView()
                        mView.onHotRecommendResponse(list)
konghaorui committed
72
                    }else{
konghaorui committed
73
                        mView.hideLoadingView()
konghaorui committed
74 75 76
                    }
                }, object : ThrowableConsumer() {
                    override fun accept(msg: String) {
konghaorui committed
77
                        mView.showError(msg)
konghaorui committed
78 79 80 81
                    }
                })
    }

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