ConsultAssistantDialogUtils.kt 15.7 KB
Newer Older
1 2
package com.yidianling.consultant.modular.singlton

3
import android.annotation.SuppressLint
4
import android.app.Activity
YKai committed
5
import androidx.appcompat.app.AppCompatActivity
6
import com.ydl.ydlcommon.data.http.ThrowableConsumer
霍志良 committed
7
import com.ydl.ydlcommon.utils.ActivityManager
8 9
import com.ydl.ydlcommon.utils.Utils
import com.ydl.ydlnet.YDLHttpUtils.Companion.obtainApi
10
import com.yidianling.consultant.ConsultAssistantCenterActivity
11
import com.yidianling.consultant.ExpertSearchActivity
严久程 committed
12
import com.yidianling.consultant.constants.ConsultBIConstants
13 14 15
import com.yidianling.consultant.dialog.ConsultAssistantDialog
import com.yidianling.consultant.model.SearchApi
import com.yidianling.consultant.router.ConsultantIn
16
import com.yidianling.home.api.event.HomeModuleTabEvent
17
import de.greenrobot.event.EventBus
18
import io.reactivex.android.schedulers.AndroidSchedulers
19
import io.reactivex.functions.Consumer
20 21 22 23 24
import io.reactivex.schedulers.Schedulers

/**
 * Created by xj on 2019/11/14.
 */
徐健 committed
25
class ConsultAssistantDialogUtils private constructor() {
26 27 28

    companion object {
        val INSTANCE by lazy { ConsultAssistantDialogUtils() }
29 30
        var isSHowDesc = true
        var REALATION_EDUCATION = false//是否跳转亲子教育字段,接口加ffrom2="learning"
31 32
    }

徐健 committed
33
    var consultAssistantDialogFromHomePage: ConsultAssistantDialog? = null // 首页展示的dialog
严久程 committed
34
    var consultAssistantDialogFromMine: ConsultAssistantDialog? = null // 我的展示的dialog
35 36 37
    var consultAssistantFragmentDialog: ConsultAssistantDialog? = null //咨询师列表fragment页面展示的dialog
    var expertSearchPageHasShown: Boolean = false // 专家咨询列表fragment页面是否已经展示
    var consultAssistantActivityDialog: ConsultAssistantDialog? = null //咨询师列表activity页面展示的dialog
38
    var confideListDialog: ConsultAssistantDialog? = null //倾诉列表activity页面展示的dialog
39
    var expertSearchActivityPageHasShown: Boolean = false // 专家咨询列表activity页面是否已经展示
严久程 committed
40

41 42
    //    var ASSISTANT_DIALOG_SP_TAG = "assistant_dialog_sp_tag" // 是否展示左侧文本的缓存key
    var minWidth = 0 // 最小宽度
43

徐健 committed
44 45 46 47

    /**
     *判断是否符合代码展示逻辑
     * origin 展示请求来源    首页 home_index 咨询列表 doctor_list
48
     * fromActivity 默认不是来自于咨询师列表activity
徐健 committed
49
     */
50
    fun fitRequest(activity: Activity, origin: String, fromActivity: Boolean = false) {
51 52 53
        when (origin) {
            "home_index" -> {
                if (ConsultantIn.isLogin() &&
54 55
                    ConsultantIn.getUserImpl().getUserInfo()?.user_type == 1
                ) {
56 57 58 59
                    shouldShowDialog(activity, origin)
                }
            }
            "doctor_list" -> {
60 61 62
                if (!ConsultantIn.isLogin() || (ConsultantIn.getUserImpl()
                        .getUserInfo()?.user_type == 1 && ConsultantIn.isLogin())
                ) {
63 64
                    shouldShowDialog(activity, origin, fromActivity = fromActivity)
                }
65
            }
严久程 committed
66 67 68 69 70 71 72
            "mine_index" -> {
                if (ConsultantIn.isLogin() &&
                    ConsultantIn.getUserImpl().getUserInfo()?.user_type == 1
                ) {
                    shouldShowDialog(activity, origin)
                }
            }
徐健 committed
73
        }
74

徐健 committed
75 76 77 78 79
    }

    /**
     * 判断是否符合接口展示逻辑
     */
严久程 committed
80
    @SuppressLint("CheckResult")
徐健 committed
81 82 83 84 85 86
    private fun shouldShowDialog(
        activity: Activity,
        origin: String,
        fromActivity: Boolean = false
    ) {

徐健 committed
87
        // 请求接口判断导医咨询助理按钮是否展示
徐健 committed
88 89 90 91 92 93 94 95
        SearchApi.getSearchApi().getConsultAssistantRequest(origin)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                if (it.data) {
                    if (origin == "home_index") {
                        showFromYdlHome(activity)
                    } else if (origin == "doctor_list") {
96
                        show(activity, origin, fromActivity)
严久程 committed
97 98
                    } else if (origin == "mine_index") {
                        showFromMine(activity)
徐健 committed
99 100 101
                    }
                }
            }
徐健 committed
102 103
    }

徐健 committed
104 105 106 107 108 109
    //////////////////////////////////////  首页的展示隐藏逻辑 代码块start  ///////////////////////////////////////////////////

    /**
     * 首页页展示
     */
    fun showFromYdlHome(activity: Activity) {
徐健 committed
110 111 112
        if (!activity.isFinishing) {
            if (consultAssistantDialogFromHomePage == null) {
                consultAssistantDialogFromHomePage = ConsultAssistantDialog(
徐健 committed
113 114 115
                    activity,
                    object : ConsultAssistantDialog.OnConsultAssistantClickListener {
                        override fun onClickAction() {
116 117 118
                            if (Utils.isFastClick()) {
                                return onClickAction()
                            }
徐健 committed
119
                            //获取用户uid
120
                            getConsultAssistantUid("", activity, 1)
徐健 committed
121
                        }
徐健 committed
122

徐健 committed
123 124
                    })
            }
徐健 committed
125 126
            consultAssistantDialogFromHomePage?.show()
        }
徐健 committed
127 128 129 130 131 132
    }

    /**
     * 首页隐藏
     */
    fun hideFromHomePage() {
徐健 committed
133
        consultAssistantDialogFromHomePage?.hide()
徐健 committed
134 135 136 137
    }

    //////////////////////////////////////  首页的展示隐藏逻辑 代码块end  ///////////////////////////////////////////////////

138 139 140 141 142 143
    //////////////////////////////////////  倾诉列表页展示隐藏逻辑 代码块start  ///////////////////////////////////////////////////
    /**
     * 倾诉列表页展示
     */
    fun showFromConfideListHome(activity: Activity) {
        if (!activity.isFinishing) {
144
            confideListDialog = ConsultAssistantDialog(
145 146 147 148 149 150 151 152
                activity,
                object : ConsultAssistantDialog.OnConsultAssistantClickListener {
                    override fun onClickAction() {
                        if (Utils.isFastClick()) {
                            return onClickAction()
                        }
                        if (!ConsultantIn.getUserImpl().loginByOneKeyLogin(activity, true)) {
                            return
153
                        }
154 155
                        getConsultAssistantUid("", activity, 17)
                    }
156

157
                })
158 159 160 161 162 163 164 165 166 167 168 169
            confideListDialog?.show()
        }
    }

    /**
     * 倾诉列表页隐藏
     */
    fun hideFromConfideListPage() {
        confideListDialog?.hide()
    }

    //////////////////////////////////////  倾诉列表页展示隐藏逻辑 代码块end  ///////////////////////////////////////////////////
严久程 committed
170 171 172 173 174 175 176 177 178 179 180 181 182

    //////////////////////////////////////  我的展示隐藏逻辑 代码块start  ///////////////////////////////////////////////////

    /**
     * 我的页展示
     */
    fun showFromMine(activity: Activity) {
        if (!activity.isFinishing) {
            if (consultAssistantDialogFromMine == null) {
                consultAssistantDialogFromMine = ConsultAssistantDialog(
                    activity,
                    object : ConsultAssistantDialog.OnConsultAssistantClickListener {
                        override fun onClickAction() {
183 184 185
                            if (Utils.isFastClick()) {
                                return onClickAction()
                            }
严久程 committed
186
                            //获取用户uid
187
                            getConsultAssistantUid("", activity, 4)
严久程 committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
                        }

                    })
            }
            consultAssistantDialogFromMine?.show()
        }
    }

    /**
     * 我的隐藏
     */
    fun hideFromMine() {
        consultAssistantDialogFromMine?.hide()
    }

    //////////////////////////////////////  我的展示隐藏逻辑 代码块end  ///////////////////////////////////////////////////


徐健 committed
206
    //////////////////////////////////////  咨询师列表页面的展示隐藏逻辑 代码块start  ///////////////////////////////////////////////////
徐健 committed
207
    /**
徐健 committed
208
     * 咨询师列表页展示
徐健 committed
209
     */
210
    fun show(activity: Activity, origin: String, fromActivity: Boolean = false) {
211 212
        // 来自于fragment咨询师列表
        if (!fromActivity) {
徐健 committed
213 214
            if (!expertSearchPageHasShown) {
                expertSearchPageHasShown = true
215
                showDialog(origin, activity)
216
            } else {
217
                showDialog(origin, activity)
218
            }
徐健 committed
219 220 221
        } else {// 来自于activity咨询师列表
            if (!expertSearchActivityPageHasShown) {
                expertSearchActivityPageHasShown = true
222
                showDialog(origin, activity, true)
223
            } else {
224
                showDialog(origin, activity, true)
225
            }
徐健 committed
226 227 228 229
        }
    }

    /**
徐健 committed
230
     * 咨询师列表页展示浮层
徐健 committed
231
     */
232
    fun showDialog(origin: String, activity: Activity, fromActivity: Boolean = false) {
233 234 235 236
        if (!fromActivity) {
            if (!activity.isFinishing) {
                if (consultAssistantFragmentDialog == null) {
                    consultAssistantFragmentDialog = ConsultAssistantDialog(
徐健 committed
237 238 239
                        activity,
                        object : ConsultAssistantDialog.OnConsultAssistantClickListener {
                            override fun onClickAction() {
240 241 242
                                if (Utils.isFastClick()) {
                                    return onClickAction()
                                }
徐健 committed
243
                                // 咨询师列表页面且未登录情况下,跳转登录页面
244
                                if (ConsultantIn.loginByOneKeyLogin(activity, true)) {
徐健 committed
245
                                    //获取用户uid
246
                                    getConsultAssistantUid(origin, activity, 6)
徐健 committed
247
                                }
徐健 committed
248
                            }
徐健 committed
249

徐健 committed
250 251
                        })
                }
252 253
                consultAssistantFragmentDialog?.show()
            }
徐健 committed
254
        } else {
255 256 257
            if (!activity.isFinishing) {
                if (consultAssistantActivityDialog == null) {
                    consultAssistantActivityDialog = ConsultAssistantDialog(
徐健 committed
258 259 260
                        activity,
                        object : ConsultAssistantDialog.OnConsultAssistantClickListener {
                            override fun onClickAction() {
261 262 263
                                if (Utils.isFastClick()) {
                                    return onClickAction()
                                }
徐健 committed
264
                                // 咨询师列表页面且未登录情况下,跳转登录页面
265
                                if (ConsultantIn.loginByOneKeyLogin(activity, true)) {
徐健 committed
266
                                    //获取用户uid
267
                                    getConsultAssistantUid(origin, activity, 6)
268
                                }
徐健 committed
269
                            }
270

徐健 committed
271 272
                        })
                }
273
                consultAssistantActivityDialog?.show()
徐健 committed
274 275
            }
        }
276 277
    }

徐健 committed
278 279

    /**
280
     * 咨询师列表页Fragment隐藏
徐健 committed
281 282
     */
    fun hide() {
ydl committed
283
        consultAssistantFragmentDialog?.dismiss()
徐健 committed
284 285
    }

286 287 288 289
    /**
     * 咨询师列表页Fragment隐藏
     */
    fun hideAssistantActivity() {
ydl committed
290
        consultAssistantActivityDialog?.dismiss()
291 292
    }

徐健 committed
293
    /**
294 295 296
     * 释放本单例所有资源(咨询师列表fragmnet在MainActivity,
     * 且在MainActivity销毁的时候,会先执行咨询师列表fragmnet
     * 的ondeatroy,所以这边直接在该专家咨询列表碎片执行该方法)
徐健 committed
297 298
     */
    fun resetStatus() {
徐健 committed
299
        consultAssistantDialogFromHomePage?.dismiss()
严久程 committed
300
        consultAssistantDialogFromMine?.dismiss()
301 302
        consultAssistantFragmentDialog?.dismiss()
        consultAssistantActivityDialog?.dismiss()
徐健 committed
303

徐健 committed
304
        expertSearchPageHasShown = false
305
        expertSearchActivityPageHasShown = false
徐健 committed
306
        consultAssistantDialogFromHomePage = null
严久程 committed
307
        consultAssistantDialogFromMine = null
308 309 310 311 312 313 314 315
        consultAssistantFragmentDialog = null
        consultAssistantActivityDialog = null
    }

    /**
     * 咨询师列表activity页面使用的重置本单例关于专家咨询列表页面得状态
     */
    fun expertSearchResetStatus() {
316
        //页面关闭后重置亲子列表状态
317
        REALATION_EDUCATION = false
318
        consultAssistantActivityDialog?.dismiss()
徐健 committed
319

320 321
        expertSearchActivityPageHasShown = false
        consultAssistantActivityDialog = null
322 323
    }

徐健 committed
324 325
    //////////////////////////////////////  咨询师列表页面的展示隐藏逻辑 代码块end  ///////////////////////////////////////////////////

326
    @SuppressLint("CheckResult")
327 328
    fun getConsultAssistantUid(origin: String, activity: Activity, location: Int) {
        if (origin == "doctor_list") {
329
            // 请求接口获取咨询助理的uid
330 331 332 333
            if (REALATION_EDUCATION) {
                getConsultAssistantUid(location, activity, "learning")
            } else {
                getConsultAssistantUid(location, activity, null)
334 335
            }

336
        } else {
337
            // 请求接口获取咨询助理的uid
338
            getConsultAssistantUid(location, activity, null)
339 340
        }

341 342 343
    }

    @SuppressLint("CheckResult")
344 345 346
    private fun getConsultAssistantUid(location: Int, activity: Activity, ffrom: String?) {

        obtainApi(SearchApi::class.java).getChatRouteConfig(location)
347 348
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
严久程 committed
349
            .subscribe(Consumer {
350 351

                if (it.data == 100L && (location <= 17)) {
352
                    //跳转咨询tab栏目
353 354
                    if (location == 1 || location == 4 || location == 6 || location == 3) {
                        if (activity is ExpertSearchActivity && !activity.isFinishing) {
355 356 357
                            activity.finish()
                        }
                        EventBus.getDefault().post(HomeModuleTabEvent(2))
358 359 360 361
                    } else {
                        if (activity.componentName.toString()
                                .contains("CourseListContainerActivity")
                        ) {
霍志良 committed
362 363
                            ActivityManager.getInstance()
                                .getSecondTaskActivity()?.finish()
364
                            activity.finish()
365 366
                        } else {
                            if (null != activity && !activity.isFinishing) {
霍志良 committed
367 368
                                activity.finish()
                            }
369 370 371
                        }
                        EventBus.getDefault().post(HomeModuleTabEvent(2))
                    }
372 373 374 375 376 377 378
                } else if (it.data == 0L) {
                    //去前置信息收集页
                    ConsultantIn.startP2PSession(
                        activity as AppCompatActivity,
                        location,
                        ffrom
                    )
379
                }
380
                if (null != activity && activity is ConsultAssistantCenterActivity && !activity.isFinishing) activity.finish()
严久程 committed
381
            }, object : ThrowableConsumer() {
382 383 384
                override fun accept(msg: String) {
                    if (null != activity && activity is ConsultAssistantCenterActivity && !activity.isFinishing) activity.finish()
                }
385
            }
严久程 committed
386
            )
387
    }
徐健 committed
388 389 390

    // 判定是否展示左侧的描述文本
    fun canShowDesc(): Boolean {
严久程 committed
391 392 393 394 395 396 397 398 399 400 401
//        if (TextUtils.isEmpty(SharedPreferencesEditor.getString(ASSISTANT_DIALOG_SP_TAG))) {
//            //如果没有缓存,则展示
//            return true
//        } else if (System.currentTimeMillis() - SharedPreferencesEditor.getString(
//                ASSISTANT_DIALOG_SP_TAG
//            ).toLong() > 24 * 60 * 60 * 1000
//        ) {
//            //如果缓存时间超过一天,则展示
//            return true
//        }
        return isSHowDesc
徐健 committed
402 403 404 405
    }

    fun setDescHide() {
        consultAssistantDialogFromHomePage?.setDescHide()
严久程 committed
406
        consultAssistantDialogFromMine?.setDescHide()
徐健 committed
407 408 409
        consultAssistantActivityDialog?.setDescHide()
        consultAssistantFragmentDialog?.setDescHide()
    }
410
}