HotSearchActivity.kt 19.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
package com.yidianling.consultant

import android.content.Context
import android.graphics.Rect
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.TextView
upwork.021 committed
14
import androidx.recyclerview.widget.LinearLayoutManager
15 16 17
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
霍志良 committed
18 19
import com.ydl.webview.H5Params
import com.ydl.webview.NewH5Activity
严久程 committed
20
import com.ydl.ydl_image.module.GlideApp
21 22 23
import com.ydl.ydl_router.manager.YDLRouterParams
import com.ydl.ydlcommon.base.BaseMvpActivity
import com.ydl.ydlcommon.bean.StatusBarOptions
严久程 committed
24
import com.ydl.ydlcommon.modular.ModularServiceManager
25
import com.ydl.ydlcommon.router.IYDLRouterConstant
upwork.021 committed
26
import com.ydl.ydlcommon.utils.*
霍志良 committed
27
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils
28 29 30
import com.ydl.ydlcommon.view.banner.GlideImageLoader
import com.yidianling.common.tools.RxDeviceTool
import com.yidianling.common.tools.RxImageTool
upwork.021 committed
31
import com.yidianling.consultant.adapter.SearchWordsAdapter
严久程 committed
32
import com.yidianling.consultant.api.IConsultantService
upwork.021 committed
33
import com.yidianling.consultant.bean.*
霍志良 committed
34
import com.yidianling.consultant.constants.ConsultBIConstants
霍志良 committed
35
import com.yidianling.consultant.constants.ConsultBIConstants.ConsultEvent.Companion.SEARCH_BANNERWORD_CLICK
36
import com.yidianling.consultant.contract.IHotSearchContract
37
import com.yidianling.consultant.model.bean.FunctionWordBean
严久程 committed
38
import com.yidianling.consultant.modular.utils.ConsultAssistantEntryUtils
39
import com.yidianling.consultant.modular.utils.TempH5RouteUtils
40
import com.yidianling.consultant.presenter.HotSearchPresenterImpl
41 42
import kotlinx.android.synthetic.main.consultant_activity_hot_search.*
import kotlinx.android.synthetic.main.consultant_item_expert_hot_search.view.*
43 44

@Route(path = "/consult/hot_search")
霍志良 committed
45 46
class HotSearchActivity : BaseMvpActivity<IHotSearchContract.View, IHotSearchContract.Presenter>(),
    IHotSearchContract.View {
霍志良 committed
47

霍志良 committed
48
    private lateinit var searchWordsAdapter: SearchWordsAdapter
upwork.021 committed
49
    private val searchSuggestList: ArrayList<SearchSuggestListBean> = ArrayList()
霍志良 committed
50
    private var mSearchContent: String = ""
霍志良 committed
51

52 53
    private var historyList: FixSizeLinkedList<String> = FixSizeLinkedList(15)
    private val bannerList = ArrayList<String>()
严久程 committed
54

霍志良 committed
55 56 57 58 59 60
    companion object {
        private const val CACHE_CONSULT_SEARCH_HISTORY_DATA = "cache_consult_search_history_data"
        private const val HOT_SEARCH_DOCTOR_NAME = "hot_search_doctor_name"
        private const val JUMP_WORD = "jump_word"
    }

61 62
    //历史搜索内容的最大宽度
    private var maxWidth: Int = 0
严久程 committed
63

64 65
    //历史搜索实际宽度
    private var historyCurrentWidth: Int = 0
严久程 committed
66

67 68 69 70 71
    //热门专家搜索实际宽度
    private var expertWidth: Int = 0

    private var dp42: Int = 0

72

73
    override fun layoutResId(): Int {
74
        return R.layout.consultant_activity_hot_search
75 76 77 78 79 80 81 82 83
    }

    override fun createPresenter(): IHotSearchContract.Presenter {
        return HotSearchPresenterImpl()
    }

    override fun getStatusViewOptions(): StatusBarOptions {
        return StatusBarOptions(isAddStatusView = true)
    }
严久程 committed
84

85 86 87 88 89 90 91 92
    override fun initDataAndEvent() {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE or WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
        getDataFromIntent()
        initViews()
        initData()
    }

    private fun getDataFromIntent() {
upwork.021 committed
93
        val doctorName = intent.getStringExtra(HOT_SEARCH_DOCTOR_NAME)
94
        if (!TextUtils.isEmpty(doctorName)) {
霍志良 committed
95
            etSearch.hint = doctorName
96 97 98 99 100
            iv_delete_icon.visibility = View.VISIBLE
        }
    }

    private fun initViews() {
upwork.021 committed
101
        StatusBarUtils.statusBarLightMode(this)
霍志良 committed
102 103
        maxWidth =
            (2 * RxDeviceTool.getScreenWidth(this@HotSearchActivity)) - RxImageTool.dip2px(60f)
upwork.021 committed
104 105
        dp42 = RxImageTool.dip2px(60f)

106 107 108 109 110
        iv_delete_icon.setOnClickListener {
            etSearch.setText("")
        }
        etSearch.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
111 112
                // 搜索的关联词
                var relatedWords = ""
113
                var isRecommendWords = false
霍志良 committed
114
                if (searchSuggestList.isNotEmpty() && searchSuggestList.size > 0) {
霍志良 committed
115
                    if (searchSuggestList[0].suggest_relations.size > 0) {
116 117 118
                        relatedWords = searchSuggestList[0].suggest_relations[0]
                        isRecommendWords = true
                    }
霍志良 committed
119
                    if (TextUtils.isEmpty(relatedWords) && !TextUtils.isEmpty(searchSuggestList[0].suggest_content)) {
120 121
                        relatedWords = searchSuggestList[0].suggest_content
                    }
霍志良 committed
122

霍志良 committed
123
                    if (searchSuggestList?.get(0)?.suggest_types?.contains(JUMP_WORD)) {
霍志良 committed
124 125
                        NewH5Activity.start(this, H5Params(searchSuggestList[0].jump_url!!, null))
                    } else {
霍志良 committed
126 127 128 129 130
                        if (etSearch.text.toString().isNullOrEmpty()) {
                            doSearch(etSearch.hint.toString(), relatedWords, isRecommendWords)
                        } else {
                            doSearch(etSearch.text.toString(), relatedWords, isRecommendWords)
                        }
霍志良 committed
131
                    }
霍志良 committed
132
                } else {
133
                    val searchWords = etSearch.text.toString()
霍志良 committed
134
                    if (TextUtils.isEmpty(searchWords)) {
霍志良 committed
135
                        getSearchWords(etSearch.hint.toString(), true)
霍志良 committed
136 137
                    } else {
                        getSearchWords(etSearch.text.toString(), true)
138 139
                    }
                }
140 141 142 143 144 145 146 147 148 149
            }
            true
        }
        tv_search_cancle.setOnClickListener {
            finish()
        }
        iv_delete_history.setOnClickListener {
            SharedPreferencesEditor.putString(CACHE_CONSULT_SEARCH_HISTORY_DATA, "")
            initHistoryData()
        }
严久程 committed
150

霍志良 committed
151
        val data = ModularServiceManager.provide(IConsultantService::class.java).getGuideImage(5)
严久程 committed
152
        if (data?.size ?: 0 > 0) {
霍志良 committed
153 154
            val imageUrl = data?.get(0)?.pic
            iv_daoyi_image.visibility = View.VISIBLE
严久程 committed
155 156 157 158 159

            GlideApp.with(mContext)
                .load(imageUrl)
                .into(iv_daoyi_image)

严久程 committed
160 161
            iv_daoyi_image.setOnClickListener {
                LogUtil.e("跳转导医:location=${data?.get(0)!!.location},title=${data[0].title}")
霍志良 committed
162 163 164 165 166
                ConsultAssistantEntryUtils.jumpConsultAssistant(
                    this,
                    data[0].location.toInt(),
                    "learning"
                )
严久程 committed
167 168 169
            }
        }

upwork.021 committed
170 171 172 173
        searchWordsAdapter = SearchWordsAdapter(searchSuggestList)
        rv_search_words.layoutManager = LinearLayoutManager(this)
        rv_search_words.adapter = searchWordsAdapter
        searchWordsAdapter.setOnItemClickListener { adapter, view, position ->
174 175
            // 搜索的关联词
            var relatedWords = ""
176
            var isRecommendWords = false
霍志良 committed
177 178
            if (searchSuggestList.isNotEmpty() && searchSuggestList.size > 0) {
                if (searchSuggestList[position].suggest_relations.size > 0) {
179 180 181
                    relatedWords = searchSuggestList[position].suggest_relations[0]
                    isRecommendWords = true
                }
霍志良 committed
182
                if (TextUtils.isEmpty(relatedWords) && !TextUtils.isEmpty(searchSuggestList[position].suggest_content)) {
183
                    relatedWords = searchSuggestList[position].suggest_content
184 185
                }
            }
霍志良 committed
186
            if (searchSuggestList[position].suggest_types.contains(JUMP_WORD)) {
霍志良 committed
187 188 189 190 191 192 193 194
                NewH5Activity.start(this, H5Params(searchSuggestList[position].jump_url!!, null))
            } else {
                doSearch(
                    searchSuggestList[position].suggest_content,
                    relatedWords,
                    isRecommendWords
                )
            }
195 196

            // 埋点
霍志良 committed
197 198 199 200 201 202 203
            ActionCountUtils.baiDuCountSign3(
                ConsultBIConstants.PART_ID_YDL_USER_MAIN_PAGE,
                ConsultBIConstants.POSITION_YDL_USER_ASSOCIATE_WORD_CLICK,
                etSearch.text.toString(),
                searchSuggestList[position].suggest_content,
                "app"
            )
upwork.021 committed
204
        }
205 206 207 208
        etSearch.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                if (TextUtils.isEmpty(s)) {
                    iv_delete_icon.visibility = View.INVISIBLE
upwork.021 committed
209
                    rv_search_words.visibility = View.GONE
霍志良 committed
210
                    getSearchWords("", false)
211 212
                } else {
                    iv_delete_icon.visibility = View.VISIBLE
霍志良 committed
213
                    getSearchWords(s.toString(), false)
214
                }
upwork.021 committed
215

216 217 218 219 220 221 222 223 224
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

        })
upwork.021 committed
225 226
    }

227

upwork.021 committed
228 229
    /**
     * 获取搜索联想词
230
     * @param isClickWords 是否是点击历史搜索、热门搜索等进行搜索
upwork.021 committed
231
     */
霍志良 committed
232 233
    private fun getSearchWords(searchContent: String, isClickWords: Boolean) {
        if (!TextUtils.isEmpty(searchContent)) {
upwork.021 committed
234 235 236
            mSearchContent = searchContent
            val map = HashMap<String, Any>()
            map["content"] = searchContent
霍志良 committed
237 238
            mPresenter.getSearchWords(map, searchContent, isClickWords)
        } else {
upwork.021 committed
239 240 241 242
            rv_search_words.visibility = View.GONE
        }
    }

霍志良 committed
243 244 245 246 247 248
    override fun getSearchWordsSuccess(
        searchWordsBean: SearchWordsBean,
        searchContent: String,
        isClickWords: Boolean
    ) {
        if (isClickWords) {
霍志良 committed
249
            //判断 suggest_classify_types 有值则通过 jump_url跳转
250 251
            // 搜索的关联词
            var relatedWords = ""
252
            var isRecommendWords = false
霍志良 committed
253 254
            if (!searchWordsBean.search_suggests.isNullOrEmpty() && searchWordsBean.search_suggests.size > 0) {
                if (searchWordsBean.search_suggests[0].suggest_relations.size > 0) {
255 256 257
                    relatedWords = searchWordsBean.search_suggests[0].suggest_relations[0]
                    isRecommendWords = true
                }
霍志良 committed
258
                if (TextUtils.isEmpty(relatedWords) && !TextUtils.isEmpty(searchWordsBean.search_suggests[0].suggest_content)) {
259
                    relatedWords = searchWordsBean.search_suggests[0].suggest_content
260 261
                }
            }
霍志良 committed
262
            if (!searchWordsBean.search_suggests.isNullOrEmpty() && searchWordsBean.search_suggests.get(
霍志良 committed
263
                    0
霍志良 committed
264
                ).suggest_types.contains(JUMP_WORD)
霍志良 committed
265
            ) {
霍志良 committed
266 267 268 269 270 271 272 273 274
                //将数据增加到历史搜索中
                if (!TextUtils.isEmpty(searchContent)) {
                    historyList.remove(searchContent)
                    historyList.add(searchContent)
                    SharedPreferencesEditor.putString(
                        CACHE_CONSULT_SEARCH_HISTORY_DATA,
                        Gson().toJson(historyList)
                    )
                }
霍志良 committed
275 276 277 278
                NewH5Activity.start(
                    this,
                    H5Params(searchWordsBean.search_suggests[0].jump_url!!, null)
                )
霍志良 committed
279
                finish()
霍志良 committed
280 281 282
            } else {
                doSearch(searchContent, relatedWords, isRecommendWords)
            }
霍志良 committed
283
        } else {
284
            searchSuggestList.clear()
霍志良 committed
285
            if (!searchWordsBean.search_suggests.isNullOrEmpty()) {
286 287 288
                rv_search_words.visibility = View.VISIBLE
                searchSuggestList.addAll(searchWordsBean.search_suggests)
                searchWordsAdapter.notifyDataAndSetSearchWord(mSearchContent)
289 290

                // 埋点
霍志良 committed
291 292 293 294 295 296 297 298 299
                val sign2 = searchSuggestList.joinToString(",") { it.suggest_content }
                ActionCountUtils.baiDuCountSign3(
                    ConsultBIConstants.PART_ID_YDL_USER_MAIN_PAGE,
                    ConsultBIConstants.POSITION_YDL_USER_ASSOCIATE_WORD_VISIT,
                    searchContent,
                    sign2,
                    "app"
                )
            } else {
300 301
                rv_search_words.visibility = View.GONE
            }
upwork.021 committed
302
        }
303 304 305 306 307
    }

    private fun initData() {
        initHistoryData()
        mPresenter.localData(this)
308 309 310
        val mapType = HashMap<String, Any>()
        mapType["type"] = 1
        mPresenter.searchHotWordData(mapType)
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
        if (etSearch.requestFocus()) {
            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            val isShowing = imm.showSoftInput(etSearch, InputMethodManager.SHOW_IMPLICIT)
            if (!isShowing) {
                window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
            }
        }
    }

    override fun searchDataResponse(hotSearchBean: HotSearchBean) {
        //刷新 热门搜索
        refreshHotSearchData(hotSearchBean.keywordData)
        //刷新 本周热门专家
        refreshHotExpertData(hotSearchBean.popularDoctors)
        //刷新 banner
        refreshBanner(hotSearchBean.focusList)
    }

霍志良 committed
329
    override fun searchHotWordData(wordList: MutableList<FunctionWordBean>) {
330
        //热门词搜索结果
霍志良 committed
331
        if (wordList.isEmpty()) {
332 333 334 335 336
            llHotSearch.visibility = View.GONE
            return
        }
        llHotSearch.visibility = View.VISIBLE
        flHotSearch.removeAllViews()
337
        for (index in wordList.indices) {
严久程 committed
338 339
            val view = LayoutInflater.from(this)
                .inflate(R.layout.consultant_item_expert_hot_search, flHotSearch, false)
340
            view.tvHotSearch.text = wordList[index].word
341
            view.setOnClickListener {
霍志良 committed
342
                ActionCountUtils.count(SEARCH_BANNERWORD_CLICK, wordList[index].word!!, "热门搜索")
343
                getSearchWords(wordList[index].word!!, true)
344 345 346 347 348
            }
            flHotSearch.addView(view)
        }
    }

349 350 351 352 353 354 355 356 357 358
    override fun requestFail() {
        refreshBanner(null)
    }


    //刷新 热门搜索
    private fun refreshHotSearchData(keywordData: MutableList<HotSearchKeyWordDataBean>?) {
        //旧的热门搜索
    }

359 360 361 362 363 364 365 366 367 368
    //刷新 本周热门专家
    private fun refreshHotExpertData(hotSearchExpert: MutableList<HotSearchPopularDoctorBean>?) {
        if (null == hotSearchExpert || hotSearchExpert.isEmpty()) {
            llHotExpert.visibility = View.GONE
            return
        }
        llHotExpert.visibility = View.VISIBLE
        expertWidth = 0
        flHotExpert.removeAllViews()
        for (index in hotSearchExpert.indices) {
严久程 committed
369 370
            val view = LayoutInflater.from(this)
                .inflate(R.layout.consultant_item_expert_hot_search, flHotSearch, false)
371 372
            view.tvHotSearch.text = hotSearchExpert[index].name
            if (!TextUtils.isEmpty(hotSearchExpert[index].name)) {
严久程 committed
373 374 375 376 377 378 379
                expertWidth += getTextContentWidth(
                    view.findViewById(R.id.tvHotSearch),
                    if (hotSearchExpert[index].name!!.length > 7) hotSearchExpert[index].name!!.substring(
                        0,
                        8
                    ) else hotSearchExpert[index].name!!
                ) + dp42
380 381 382 383 384
                if (expertWidth > maxWidth) {
                    break
                }
            }
            view.setOnClickListener {
霍志良 committed
385 386 387 388 389 390
                ActionCountUtils.count(
                    SEARCH_BANNERWORD_CLICK,
                    hotSearchExpert[index].name.toString(),
                    "本周热门专家"
                )
                getSearchWords(hotSearchExpert[index].name.toString(), true)
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
            }
            flHotExpert.addView(view)
        }
    }

    //刷新 banner
    private fun refreshBanner(focusList: MutableList<HotSearchFocusItemBean>?) {
        if (null == focusList) {
            card_view.visibility = View.GONE
        }
        if (null != focusList && focusList.isNotEmpty()) {
            for (item in focusList) {
                if (!TextUtils.isEmpty(item.imageUrl)) {
                    bannerList.add(item.imageUrl!!)
                }
            }
        }
        banner.setOnBannerListener {
            if (null == focusList || focusList.isEmpty()) {
410
                TempH5RouteUtils.tempH5Route(
411
                    IYDLRouterConstant.ROUTER_H5_H5,
严久程 committed
412 413 414 415 416
                    YDLRouterParams().putExtra(
                        IYDLRouterConstant.EXTRA_URL,
                        "https://h2.yidianling.com/ct/list"
                    ), ""
                )
417
            } else {
418
                TempH5RouteUtils.tempH5Route(focusList[it].linkUrl)
419 420
            }
        }
严久程 committed
421 422
        banner.setImageLoader(GlideImageLoader(R.drawable.consultant_expert_banner_default))
            .setImages(bannerList).start()
423 424 425 426 427 428 429 430 431
        banner.setIndicatorBottomPadding(5)
    }

    private fun initHistoryData() {
        fl_search_history.removeAllViews()
        historyList.clear()
        val cacheHomeData = SharedPreferencesEditor.getString(CACHE_CONSULT_SEARCH_HISTORY_DATA)
        if (!TextUtils.isEmpty(cacheHomeData)) {
            ll_search_history.visibility = View.VISIBLE
严久程 committed
432 433 434 435 436
            val list = Gson().fromJson<MutableList<String>>(
                cacheHomeData,
                object : TypeToken<MutableList<String>>() {
                }.type
            )
437 438 439 440
            historyList.addAll(list)
            if (historyList.size > 0) {
                for (historyStr in historyList.reversed()) {

严久程 committed
441 442
                    val view = LayoutInflater.from(this)
                        .inflate(R.layout.consultant_item_expert_hot_search, flHotSearch, false)
443
                    view.tvHotSearch.text = historyStr
严久程 committed
444 445 446 447 448
                    historyCurrentWidth += getTextContentWidth(
                        view.findViewById(R.id.tvHotSearch),
                        if (historyStr.length > 7) historyStr.substring(0, 8) else historyStr
                    ) + RxImageTool.dp2px(50f)
                    if (historyCurrentWidth > maxWidth) {
449 450 451
                        break
                    }
                    view.setOnClickListener {
霍志良 committed
452
                        //历史搜索
霍志良 committed
453
                        ActionCountUtils.count(SEARCH_BANNERWORD_CLICK, historyStr, "历史搜索")
霍志良 committed
454
                        getSearchWords(historyStr, true)
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
                    }
                    fl_search_history.addView(view)
                }
            }
        } else {
            ll_search_history.visibility = View.GONE
        }
    }

    private fun getTextContentWidth(textView: TextView, text: String): Int {
        val rect = Rect()
        textView.paint.getTextBounds(text, 0, text.length, rect)
        return rect.width()//文字宽
    }

upwork.021 committed
470 471
    /**
     *  搜索
472
     *  @param isRecommendWords 是否是推荐词
upwork.021 committed
473
     */
霍志良 committed
474
    private fun doSearch(searchWords: String, relatedWords: String, isRecommendWords: Boolean) {
475 476 477 478 479
        val view = this.currentFocus
        if (view != null) {
            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(view.windowToken, 0)
        }
480

霍志良 committed
481
        ActionCountUtils.count(ConsultBIConstants.UserMainEvent.YDL_USER_SEARCH_CLICK, searchWords)
upwork.021 committed
482 483 484
        if (!TextUtils.isEmpty(searchWords)) {
            historyList.remove(searchWords)
            historyList.add(searchWords)
严久程 committed
485 486 487 488
            SharedPreferencesEditor.putString(
                CACHE_CONSULT_SEARCH_HISTORY_DATA,
                Gson().toJson(historyList)
            )
489
        }
霍志良 committed
490
        ExpertSearchActivity.startSearch(this, searchWords, "14", relatedWords, isRecommendWords)
upwork.021 committed
491
        finish()
492 493 494 495 496 497 498 499
    }

    override fun onDestroy() {
        super.onDestroy()
        historyCurrentWidth = 0
        expertWidth = 0
    }
}