HotSearchPresenterImpl.kt 3.95 KB
Newer Older
1 2
package com.yidianling.consultant.presenter

upwork.021 committed
3
import android.annotation.SuppressLint
4 5 6
import android.content.Context
import android.text.TextUtils
import com.google.gson.Gson
7
import com.ydl.ydlcommon.data.http.BaseAPIResponse
8 9 10 11 12
import com.ydl.ydlcommon.data.http.ThrowableConsumer
import com.ydl.ydlcommon.mvp.base.BasePresenter
import com.ydl.ydlcommon.utils.RxLifecycleUtils
import com.ydl.ydlcommon.utils.YDLAsyncUtils
import com.ydl.ydlcommon.utils.YDLCacheUtils
upwork.021 committed
13
import com.yidianling.common.tools.ToastUtil
14 15 16
import com.yidianling.consultant.bean.HotSearchBean
import com.yidianling.consultant.contract.IHotSearchContract
import com.yidianling.consultant.model.HotSearchModelImpl
17 18
import com.yidianling.consultant.model.bean.FunctionWordBean
import io.reactivex.Observable
19 20
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
upwork.021 committed
21 22
import io.reactivex.schedulers.Schedulers

23 24 25 26 27 28 29
/**
 * @author yuanwai
 * @描述:搜索页面逻辑实现类
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2019/3/19
 */
upwork.021 committed
30
class HotSearchPresenterImpl : BasePresenter<IHotSearchContract.View, IHotSearchContract.Model>(), IHotSearchContract.Presenter{
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

    /**
     * 实例化数据模型
     */
    override fun createModel(): IHotSearchContract.Model {
        return HotSearchModelImpl()
    }

    override fun localData(context: Context) {
        YDLAsyncUtils.asyncAsResult(object : YDLAsyncUtils.AsyncObjecyerResult{
            override fun doAsyncAction(): Any {
                return YDLCacheUtils.getHotSearchData()
            }

            override fun asyncResult(`object`: Any?) {
                //如果没有缓存数据,显示加载框
                if (`object` !is String || TextUtils.isEmpty(`object`)){

                }

                if (`object` is String){
                    val gson = Gson()
                    val bean = gson.fromJson<HotSearchBean>(`object`,HotSearchBean::class.java)
                    if(null != bean){
                        mView.searchDataResponse(bean)
                    }
                }
                searchData()
            }
        })
    }

    /**
     * 搜索页接口
     */
66
    @SuppressLint("CheckResult")
67 68 69 70 71
    override fun searchData() {
         mModel.searchData().map { it }
                .filter { it != null }
                .compose(RxLifecycleUtils.bindToLifecycle(mView!!))
                .observeOn(AndroidSchedulers.mainThread())
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
             .subscribe(Consumer {
                 mView.searchDataResponse(it)
                 YDLCacheUtils.saveHotSearchData(Gson().toJson(it))
             }, object : ThrowableConsumer() {
                 override fun accept(msg: String) {
                     mView.requestFail()
                 }
             })
    }

    @SuppressLint("CheckResult")
    override fun searchHotWordData(map: HashMap<String, Any>) {
        mModel.searchHotWordData(map).compose(RxLifecycleUtils.bindToLifecycle(mView!!))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(Consumer {
                mView.searchHotWordData(it.data)
            }, object : ThrowableConsumer() {
                override fun accept(msg: String) {
                    mView.requestFail()
                }
            })
94
    }
upwork.021 committed
95 96

    @SuppressLint("CheckResult")
97 98 99 100 101
    override fun getSearchWords(
        map: HashMap<String, Any>,
        searchContent: String,
        isClickWords: Boolean
    ) {
upwork.021 committed
102
        mModel.getSearchWords(map)
103
//            .debounce(500L, TimeUnit.MILLISECONDS)
upwork.021 committed
104 105 106 107
            .compose(RxLifecycleUtils.bindToLifecycle(mView))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(Consumer {
108
                mView.getSearchWordsSuccess(it, searchContent, isClickWords)
upwork.021 committed
109 110 111 112 113 114
            }, object : ThrowableConsumer() {
                override fun accept(msg: String) {
                   ToastUtil.toastShort(msg)
                }
            })
    }
115
}