HomePresenterImpl.kt 13.2 KB
Newer Older
徐健 committed
1 2 3 4 5
package com.yidianling.home.presenter

import android.annotation.SuppressLint
import com.google.gson.Gson
import com.ydl.ydlcommon.base.BaseApp
徐健 committed
6
import com.ydl.ydlcommon.data.http.RxUtils
徐健 committed
7
import com.ydl.ydlcommon.data.http.ThrowableConsumer
徐健 committed
8
import com.ydl.ydlcommon.modular.ModularServiceManager
徐健 committed
9
import com.ydl.ydlcommon.utils.SharedPreferencesEditor
konghaorui committed
10
import com.ydl.ydlcommon.utils.ThreadUtils
11 12 13
import com.yidianling.common.tools.LogUtil
import com.yidianling.common.tools.RxAppTool
import com.yidianling.consultant.api.IConsultantService
konghaorui committed
14
import com.yidianling.home.constract.HomeViewConfig
徐健 committed
15 16 17 18 19 20 21 22
import com.yidianling.home.constract.IHomeContract
import com.yidianling.home.model.HomeModelImpl
import com.yidianling.home.model.bean.*
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
konghaorui committed
23
import java.util.*
徐健 committed
24 25 26 27 28 29 30 31 32


/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述:
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/02/13
 */
33 34 35
class HomePresenterImpl: BaseHomePresenterImpl<IHomeContract.View, IHomeContract.Model>(), IHomeContract.Presenter {
    override fun createModel(): IHomeContract.Model {
        return HomeModelImpl()
徐健 committed
36 37 38 39 40 41
    }

    /**
     * 获取首页数据请求
     */
    @SuppressLint("CheckResult")
42
    override fun loadHomeData() {
徐健 committed
43 44
        disposeHome()
        val homeList: MutableList<HomePagerDataBean> = mutableListOf()
45
        LogUtil.d("mConsultType-loadHomeData:$mConsultType")
konghaorui committed
46 47 48 49

        val headerObservable = mModel.headerRequest()
            .compose(RxUtils.applySchedulers(mView)).map { it ->

50
                mView.homeHeadResponse(it)
徐健 committed
51
                //获取咨询分类后,再次请求咨询模块数据,防止出现默认分类为空,数据不对
52
                if (it.consultCategoryData != null && it.consultCategoryData!!.isNotEmpty()) {
徐健 committed
53
                    //咨询分类和缓存分类不一致,重新加载咨询列表数据
54
                    if (it.consultCategoryData!![0].id.toString() != mConsultType) {
55
                        getConsultData("${it.consultCategoryData!![0].id}", 0, false)
徐健 committed
56 57 58
                    }
                }
                //保存热门搜索列表
59 60 61
                it.keywordData?.let {
                    ModularServiceManager.provide(IConsultantService::class.java).setHotSearch(it)
                }
konghaorui committed
62 63 64 65 66 67 68 69 70 71 72
                //首页头部数据 分类 & Banner
                homeList.add(
                    getHomeItemFromTempData(
                        it,
                        HomeViewConfig.getOrder().bannerIndex!!
                    )
                )
            }

        val consultObservable = mModel.consultRequest(mConsultType)
            .compose(RxUtils.applySchedulers(mView)).map {
徐健 committed
73
                //首页咨询数据
konghaorui committed
74 75 76 77 78 79
                homeList.add(getHomeItemFromTempData(it, HomeViewConfig.getOrder().consultIndex!!))
            }

        //GoodType为空 获取默认类型
        val confideObservable = mModel.confideRequest(mConfideType)
            .compose(RxUtils.applySchedulers(mView)).map {
徐健 committed
80
                //首页倾述数据
konghaorui committed
81 82 83 84 85
                homeList.add(getHomeItemFromTempData(it, HomeViewConfig.getOrder().confideIndex!!))
            }

        val courseObservable = mModel.courseRequest()
            .compose(RxUtils.applySchedulers(mView)).map {
徐健 committed
86
                //首页课程数据
konghaorui committed
87 88 89 90 91
                homeList.add(getHomeItemFromTempData(it, HomeViewConfig.getOrder().courseIndex!!))
            }

        val testObservable = mModel.testRequest()
            .compose(RxUtils.applySchedulers(mView)).map {
徐健 committed
92 93 94 95 96 97
                //首页测试数据
                val homeTestBean = HomeTestBean()
                homeTestBean.testItemList = it.data
                homeList.add(
                    getHomeItemFromTempData(
                        homeTestBean,
98
                        HomeViewConfig.getOrder().testsIndex!!
徐健 committed
99 100
                    )
                )
konghaorui committed
101 102 103 104
            }

        val askObservable = mModel.askRequest()
            .compose(RxUtils.applySchedulers(mView)).map {
徐健 committed
105
                //首页问答数据
106
                homeList.add(getHomeItemFromTempData(it, HomeViewConfig.getOrder().dynamicIndex!!))
konghaorui committed
107 108 109 110
            }

        val museObservable = mModel.museRequest()
            .compose(RxUtils.applySchedulers(mView)).map {
徐健 committed
111
                //首页冥想数据
konghaorui committed
112 113 114
                homeList.add(getHomeItemFromTempData(it, HomeViewConfig.getOrder().museIndex!!))
            }

115 116 117 118 119 120

        val meditationObservable = mModel.getMeditation()
            .compose(RxUtils.applySchedulers(mView)).map {
                homeList.add(getHomeItemFromTempData(it,HomeViewConfig.getOrder().museIndex!!))
            }

konghaorui committed
121 122
        val fmObservable = mModel.fmRequest()
            .compose(RxUtils.applySchedulers(mView)).map {
徐健 committed
123 124 125 126
                //首页电台数据
                homeList.add(
                    getHomeItemFromTempData(
                        it,
konghaorui committed
127
                        HomeViewConfig.getOrder().fmIndex!!
徐健 committed
128 129
                    )
                )
konghaorui committed
130 131 132 133
            }

        val articleObservable = mModel.articleRequest()
            .compose(RxUtils.applySchedulers(mView)).map {
徐健 committed
134
                //首页文章数据
konghaorui committed
135 136 137 138 139 140 141 142
                homeList.add(getHomeItemFromTempData(it, HomeViewConfig.getOrder().articleIndex!!))
            }

        //动态添加各业务模块请求
        val mergeArrays:ArrayList<Observable<Boolean?>> = arrayListOf()
        if (HomeViewConfig.getOrder().bannerIndex!=null){
            mergeArrays.add(headerObservable)
        }
143

konghaorui committed
144 145 146 147 148 149 150 151 152
        if (HomeViewConfig.getOrder().consultIndex!=null){
            mergeArrays.add(consultObservable)
        }
        if (HomeViewConfig.getOrder().courseIndex!=null){
            mergeArrays.add(courseObservable)
        }
        if (HomeViewConfig.getOrder().confideIndex!=null){
            mergeArrays.add(confideObservable)
        }
153
        if (HomeViewConfig.getOrder().testsIndex!=null){
konghaorui committed
154 155 156 157 158 159
            mergeArrays.add(testObservable)
        }
        if (HomeViewConfig.getOrder().dynamicIndex!=null){
            mergeArrays.add(askObservable)
        }
        if (HomeViewConfig.getOrder().museIndex!=null){
160
            mergeArrays.add(meditationObservable)
konghaorui committed
161
        }
162 163 164
//        if (HomeViewConfig.getOrder().museIndex!=null){
//            mergeArrays.add(museObservable)
//        }
konghaorui committed
165 166 167 168 169 170 171 172 173
        if (HomeViewConfig.getOrder().fmIndex!=null){
            mergeArrays.add(fmObservable)
        }
        if (HomeViewConfig.getOrder().articleIndex!=null){
            mergeArrays.add(articleObservable)
        }

        //将集合转化为数组
        Observable.mergeArray(*mergeArrays.toTypedArray())
徐健 committed
174 175 176 177
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(Consumer {}, object : ThrowableConsumer() {
                override fun accept(msg: String) {
178
                    mView.homeDataFail(msg)
179
                    mHomeDisposable?.dispose()
徐健 committed
180 181
                }
            }, Action {
konghaorui committed
182 183 184 185 186 187
                LogUtil.i("homeList Size:"+homeList.size+" - "+mergeArrays.size)
                if (homeList.size != mergeArrays.size){
                    mView.homeDataFail("")
                    return@Action
                }

konghaorui committed
188 189 190 191 192
                if (HomeViewConfig.getOrder().buttonIndex != null){
                    homeList.add(HomePagerDataBean(HomeViewConfig.getOrder().buttonIndex!!))
                }
                if (HomeViewConfig.getOrder().footerIndex!=null){
                    homeList.add(HomePagerDataBean(HomeViewConfig.getOrder().footerIndex!!))
193
                }
徐健 committed
194
                homeList.sortWith(compareBy { it.type })
195

196
                mView.homeDataResponse(homeList)
197

konghaorui committed
198
                //更新本地缓存数据
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
                ThreadUtils.doOnThread(object : ThreadUtils.ThreadTask{
                    override fun doOnThread() {
                        SharedPreferencesEditor.putString(CACHE_HOME_PAGER_DATA, Gson().toJson(homeList))
                        SharedPreferencesEditor.putString(
                            HOME_PAGER_DATA_CACHE_VERISON,
                            RxAppTool.getAppVersionCode(BaseApp.getApp()).toString()
                        )
                    }
                })

                //长日志打印
//                HomeLogPrinter.printLongLog(
//                    HomePresenterImpl::class.java.simpleName,
//                    Gson().toJson(homeList)
//                )
徐健 committed
214 215 216
            })
    }

217 218 219
    override fun refreshHomeData() {
        val homeItemFromTempData = getHomeItemFromTempData(
            HomeHeaderBean(true),
konghaorui committed
220
            HomeViewConfig.getOrder().bannerIndex!!
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
        )

        mConsultType =
            if (homeItemFromTempData.headerBean?.listenCategoryDate == null || homeItemFromTempData.headerBean?.listenCategoryDate!!.isEmpty()) {
                "1"
            } else {
                homeItemFromTempData.headerBean?.consultCategoryData!![0].id.toString()
            }

        mConfideType = ""

        mView.resetModuleTabPosition()
        loadHomeData()
    }

    override fun saveTempData(list: MutableList<HomePagerDataBean>) {
徐健 committed
237 238 239
        val homeBean = HomePagerDataBean()
        for (dataBean in list) {
            when (dataBean.type) {
konghaorui committed
240
                HomeViewConfig.getOrder().bannerIndex -> {
徐健 committed
241 242
                    homeBean.headerBean = dataBean.headerBean
                    dataBean.headerBean?.keywordData?.let {
243 244
                        ModularServiceManager.provide(IConsultantService::class.java)
                            .setHotSearch(it)
徐健 committed
245 246
                    }
                }
konghaorui committed
247
                HomeViewConfig.getOrder().consultIndex -> {
徐健 committed
248 249
                    homeBean.consultBean = dataBean.consultBean
                }
konghaorui committed
250
                HomeViewConfig.getOrder().confideIndex -> {
徐健 committed
251 252
                    homeBean.confideBean = dataBean.confideBean
                }
konghaorui committed
253
                HomeViewConfig.getOrder().courseIndex -> {
徐健 committed
254 255
                    homeBean.courseBean = dataBean.courseBean
                }
256
                HomeViewConfig.getOrder().testsIndex -> {
徐健 committed
257 258
                    homeBean.testListBean = dataBean.testListBean
                }
konghaorui committed
259
                HomeViewConfig.getOrder().dynamicIndex -> {
260
                    mHomeAskBean = dataBean.askBean
徐健 committed
261 262
                    homeBean.askBean = dataBean.askBean
                }
konghaorui committed
263
                HomeViewConfig.getOrder().museIndex -> {
徐健 committed
264 265
                    homeBean.museBean = dataBean.museBean
                }
konghaorui committed
266
                HomeViewConfig.getOrder().fmIndex -> {
徐健 committed
267 268
                    homeBean.fmBean = dataBean.fmBean
                }
konghaorui committed
269
                HomeViewConfig.getOrder().articleIndex -> {
徐健 committed
270 271 272 273 274 275 276
                    homeBean.articleBean = dataBean.articleBean
                }
            }
        }
        homeTempData = homeBean
    }

277
    override fun getHomeItemFromTempData(
徐健 committed
278 279 280 281 282 283 284
        dataBean: HomeItemBaseBean,
        type: Int
    ): HomePagerDataBean {

        val item = HomePagerDataBean(type)

        when (type) {
konghaorui committed
285
            HomeViewConfig.getOrder().bannerIndex -> {
徐健 committed
286 287 288
                item.headerBean =
                    if (dataBean.isRealEmpty || dataBean !is HomeHeaderBean) homeTempData.headerBean else dataBean
            }
konghaorui committed
289
            HomeViewConfig.getOrder().consultIndex -> {
徐健 committed
290 291 292
                item.consultBean =
                    if (dataBean.isRealEmpty || dataBean !is HomeConsultBean) homeTempData.consultBean else dataBean
            }
konghaorui committed
293
            HomeViewConfig.getOrder().confideIndex -> {
徐健 committed
294 295 296
                item.confideBean =
                    if (dataBean.isRealEmpty || dataBean !is HomeConfideBean) homeTempData.confideBean else dataBean
            }
konghaorui committed
297
            HomeViewConfig.getOrder().courseIndex -> {
徐健 committed
298 299 300
                item.courseBean =
                    if (dataBean.isRealEmpty || dataBean !is HomeCourseBean) homeTempData.courseBean else dataBean
            }
301
            HomeViewConfig.getOrder().testsIndex -> {
徐健 committed
302 303 304
                item.testListBean =
                    if (dataBean.isRealEmpty || dataBean !is HomeTestBean) homeTempData.testListBean else dataBean.testItemList
            }
konghaorui committed
305
            HomeViewConfig.getOrder().dynamicIndex -> {
徐健 committed
306 307 308 309
                item.askBean =
                    if (dataBean.isRealEmpty || dataBean !is HomeAskBean) homeTempData.askBean else dataBean
                mHomeAskBean = item.askBean
            }
310 311 312 313
//            HomeViewConfig.getOrder().museIndex -> {
//                item.museBean =
//                    if (dataBean.isRealEmpty || dataBean !is MuseModuleBean) homeTempData.museBean else dataBean
//            }
konghaorui committed
314
            HomeViewConfig.getOrder().museIndex -> {
315 316
                item.meditationBean =
                    if (dataBean.isRealEmpty || dataBean !is MeditationModuleBean) homeTempData.meditationBean else dataBean
徐健 committed
317
            }
konghaorui committed
318
            HomeViewConfig.getOrder().fmIndex -> {
徐健 committed
319 320 321
                item.fmBean =
                    if (dataBean.isRealEmpty || dataBean !is HomeFMBean) homeTempData.fmBean else dataBean
            }
konghaorui committed
322
            HomeViewConfig.getOrder().articleIndex -> {
徐健 committed
323 324 325 326 327 328 329
                item.articleBean =
                    if (dataBean.isRealEmpty || dataBean !is HomeArticleBean) homeTempData.articleBean else dataBean
            }
        }
        return item
    }
}