ExpertSearchPresenter.kt 7.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
package com.yidianling.consultant

import android.annotation.SuppressLint
import android.text.TextUtils
import com.google.gson.Gson
import com.ydl.ydlcommon.base.BaseApp
import com.ydl.ydlcommon.data.http.RxUtils
import com.ydl.ydlcommon.data.http.ThrowableConsumer
import com.ydl.ydlcommon.mvp.base.SimplePresenter
import com.ydl.ydlcommon.utils.RxLifecycleUtils
import com.ydl.ydlcommon.utils.YDLAsyncUtils
import com.ydl.ydlcommon.utils.YDLCacheUtils
import com.ydl.ydlcommon.utils.remind.HttpErrorUtils
import com.yidianling.consultant.http.ExpertSearchDataManager
import com.yidianling.consultant.model.SearchApi
import com.yidianling.consultant.model.bean.AllFilter
import com.yidianling.consultant.model.bean.ExpertSearchBean
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers

/**
 * 专家搜索页Presenter
 * Created by zqk on 17-9-19.
 */
class ExpertSearchPresenter : SimplePresenter<IExpertSearchView>() {


    @SuppressLint("CheckResult")
    fun fetchListHead() {
        SearchApi.getSearchApi()
                .searchConditions()
                .compose(RxLifecycleUtils.bindToLifecycle(mView!!))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .compose(RxUtils.resultJavaData())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ resp ->
                    mView.onHeadFetched(resp)
                }, { t ->
                    HttpErrorUtils.handleError(BaseApp.getApp(), t)
                    mView.fetchFailed(t.message)
                })
    }


    fun fetchBannerList() {
        ExpertSearchDataManager.getHttp().getBannerList()
            .subscribeOn(Schedulers.io())
            .compose(RxLifecycleUtils.bindToLifecycle(mView!!)).compose(RxUtils.resultJavaData())
            .map { it }
            .filter { it != null }
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(Consumer {
                mView.onBannerListFetched(it)
            }, object : ThrowableConsumer() {
                override fun accept(msg: String) {
                    mView.onBannerListFetched(null)
//                        mView.fetchFailed(msg)
                }
            })
    }

    /**
     * 加载缓存
     */
    fun localData(showType : Int){
        YDLAsyncUtils.asyncAsResult(object : YDLAsyncUtils.AsyncObjecyerResult{
            override fun doAsyncAction(): Any {
                return when(showType){
                    0 ->{//按专家
                        YDLCacheUtils.getDoctorListData()
                    }
                    1 ->{//按服务
                        YDLCacheUtils.getServerListData()
                    }
                    else ->{
                        YDLCacheUtils.getServerListData()
                    }
                }
            }

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

                if (`object` is String){
                    val gson = Gson()
                    val bean = gson.fromJson<ExpertSearchBean>(`object`,ExpertSearchBean::class.java)
//                    val bean = gson.fromJson<ExpertSearchBean>(`object`, object : TypeToken<ExpertSearchBean>() {}.type)
                    if(bean?.list != null){
                        when(showType){
                            0 ->{
                                mView.onDoctorListFetched(bean.list, 1, 1)
                            }
                            1 ->{
                                mView.onServiceListFetched(bean.list, 1, 1)
                            }
                            else ->{
                                mView.onServiceListFetched(bean.list, 1, 1)
                            }
                        }

                    }
                }
            }
        })
    }

    fun updateCache(showType: Int,searchBean: ExpertSearchBean){
        val gson = Gson()
        val json = gson.toJson(searchBean)
        when(showType){
            0 ->{
                YDLCacheUtils.saveDoctorListData(json)
            }
            1 ->{
                YDLCacheUtils.saveServerListData(json)
            }
        }
    }

    fun fetchListData(allFilter: AllFilter, page: Int) {
        var showType = 0

        val sb = StringBuffer()
        sb.append("searchWord=").append(if(TextUtils.isEmpty(allFilter.searchWord)) "" else allFilter.searchWord)
        if (allFilter.categories.isNotEmpty()) {
            var categorys = allFilter.categories.map { it.cateId }.joinToString(",")
            if ("0" == categorys) {
                categorys = ""
            }
            sb.append("&categories=").append(categorys)
        }
        if (allFilter.sub.key != null) {
            sb.append("&city=").append(Integer.parseInt(allFilter.sub.key))
        }
        if (allFilter.region.key != null) {
            sb.append("&province=").append(Integer.parseInt(allFilter.region.key))
        }
        if (allFilter.reorder.key != null) {
            sb.append("&reorder=").append(allFilter.reorder.key)
        }
        if (allFilter.enquiries.isNotEmpty()) {
            sb.append("&enquirys=").append(allFilter.enquiries.map { it.key }.joinToString(","))
        }
        if (allFilter.ages.isNotEmpty()) {
            sb.append("&ages=").append(allFilter.ages.map { it.key }.joinToString(","))
        }
        if (allFilter.others.isNotEmpty()) {
            sb.append("&others=").append(allFilter.others.map { it.key }.joinToString(","))
        }
        if (allFilter.showType.key != null) {
            showType = allFilter.showType.key!!
            sb.append("&showType=").append(allFilter.showType.key!!)
        }
        if(allFilter.title.isNotEmpty()){
            sb.append("&title=").append(allFilter.title.map { it.key }.joinToString(","))
        }
        if (allFilter.priceRanges !=null) {
            sb.append("&minPrice=").append(allFilter.priceRanges?.minPrice)
            sb.append("&maxPrice=").append(allFilter.priceRanges?.maxPrice)
        }
        sb.append("&page=").append(page)
        ExpertSearchDataManager.getHttp().searchDoctor(sb.toString())
            .compose(RxLifecycleUtils.bindToLifecycle(mView!!)).compose(RxUtils.resultJavaData())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(Consumer {
                if (null != it.list && !it.list!!.isEmpty()) {
                    if (showType == 0) {
                        mView.onDoctorListFetched(it.list!!, page, it.pages)
                    } else {
                        mView.onServiceListFetched(it.list!!, page, it.pages)
                    }
                    //更新缓存 只更新第一页的缓存
                    if (page == 1){
                        updateCache(showType,it)
                    }
                } else {
                    mView.fetchListEmpty("没有搜到相关信息,换个关键词看看吧")
                }
            }, object : ThrowableConsumer() {
                override fun accept(msg: String) {
                    mView.fetchListFailed(msg)
                }
            })
    }
}