HotSearchActivity.kt 16.5 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
import com.ydl.ydl_image.module.GlideApp
19 20 21
import com.ydl.ydl_router.manager.YDLRouterParams
import com.ydl.ydlcommon.base.BaseMvpActivity
import com.ydl.ydlcommon.bean.StatusBarOptions
严久程 committed
22
import com.ydl.ydlcommon.modular.ModularServiceManager
23
import com.ydl.ydlcommon.router.IYDLRouterConstant
upwork.021 committed
24
import com.ydl.ydlcommon.utils.*
霍志良 committed
25
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils
26 27 28
import com.ydl.ydlcommon.view.banner.GlideImageLoader
import com.yidianling.common.tools.RxDeviceTool
import com.yidianling.common.tools.RxImageTool
upwork.021 committed
29
import com.yidianling.consultant.adapter.SearchWordsAdapter
严久程 committed
30
import com.yidianling.consultant.api.IConsultantService
upwork.021 committed
31
import com.yidianling.consultant.bean.*
霍志良 committed
32
import com.yidianling.consultant.constants.ConsultBIConstants
33
import com.yidianling.consultant.contract.IHotSearchContract
严久程 committed
34
import com.yidianling.consultant.modular.utils.ConsultAssistantEntryUtils
35
import com.yidianling.consultant.modular.utils.TempH5RouteUtils
36
import com.yidianling.consultant.presenter.HotSearchPresenterImpl
37 38
import kotlinx.android.synthetic.main.consultant_activity_hot_search.*
import kotlinx.android.synthetic.main.consultant_item_expert_hot_search.view.*
39 40

@Route(path = "/consult/hot_search")
upwork.021 committed
41 42 43 44
class HotSearchActivity : BaseMvpActivity<IHotSearchContract.View, IHotSearchContract.Presenter>(), IHotSearchContract.View {
    private lateinit var searchWordsAdapter:SearchWordsAdapter
    private val searchSuggestList: ArrayList<SearchSuggestListBean> = ArrayList()
    private var mSearchContent:String = ""
45 46 47 48
    private val CACHE_CONSULT_SEARCH_HISTORY_DATA = "cache_consult_search_history_data"
    private val HOT_SEARCH_DOCTOR_NAME = "hot_search_doctor_name"
    private var historyList: FixSizeLinkedList<String> = FixSizeLinkedList(15)
    private val bannerList = ArrayList<String>()
严久程 committed
49

50 51
    //历史搜索内容的最大宽度
    private var maxWidth: Int = 0
严久程 committed
52

53 54
    //历史搜索实际宽度
    private var historyCurrentWidth: Int = 0
严久程 committed
55

56 57 58 59 60
    //热门专家搜索实际宽度
    private var expertWidth: Int = 0

    private var dp42: Int = 0

61

62
    override fun layoutResId(): Int {
63
        return R.layout.consultant_activity_hot_search
64 65 66 67 68 69 70 71 72
    }

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

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

74 75 76 77 78 79 80 81
    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
82
        val doctorName = intent.getStringExtra(HOT_SEARCH_DOCTOR_NAME)
83 84 85 86 87 88 89
        if (!TextUtils.isEmpty(doctorName)) {
            etSearch.setText(doctorName)
            iv_delete_icon.visibility = View.VISIBLE
        }
    }

    private fun initViews() {
upwork.021 committed
90 91 92 93
        StatusBarUtils.statusBarLightMode(this)
        maxWidth = (2 * RxDeviceTool.getScreenWidth(this@HotSearchActivity)) - RxImageTool.dip2px(60f)
        dp42 = RxImageTool.dip2px(60f)

94 95 96 97 98
        iv_delete_icon.setOnClickListener {
            etSearch.setText("")
        }
        etSearch.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
99 100
                // 搜索的关联词
                var relatedWords = ""
101
                var isRecommendWords = false
102
                if (searchSuggestList.isNotEmpty()&&searchSuggestList.size>0){
103 104 105 106 107 108 109 110 111 112 113 114 115 116
                    if (searchSuggestList[0].suggest_relations.size>0){
                        relatedWords = searchSuggestList[0].suggest_relations[0]
                        isRecommendWords = true
                    }
                    if (TextUtils.isEmpty(relatedWords)&&!TextUtils.isEmpty(searchSuggestList[0].suggest_content)){
                        relatedWords = searchSuggestList[0].suggest_content
                    }
                    doSearch(etSearch.text.toString(),relatedWords,isRecommendWords)
                }else{
                    val searchWords = etSearch.text.toString()
                    if (TextUtils.isEmpty(searchWords)){
                        doSearch(searchWords,"",isRecommendWords)
                    }else{
                        getSearchWords(etSearch.text.toString(),true)
117 118
                    }
                }
119 120 121 122 123 124 125 126 127 128
            }
            true
        }
        tv_search_cancle.setOnClickListener {
            finish()
        }
        iv_delete_history.setOnClickListener {
            SharedPreferencesEditor.putString(CACHE_CONSULT_SEARCH_HISTORY_DATA, "")
            initHistoryData()
        }
严久程 committed
129

霍志良 committed
130
        val data = ModularServiceManager.provide(IConsultantService::class.java).getGuideImage(5)
严久程 committed
131 132 133
        if (data?.size ?: 0 > 0) {
            val imageUrl= data?.get(0)?.pic
            iv_daoyi_image.visibility=View.VISIBLE
严久程 committed
134 135 136 137 138

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

严久程 committed
139 140
            iv_daoyi_image.setOnClickListener {
                LogUtil.e("跳转导医:location=${data?.get(0)!!.location},title=${data[0].title}")
upwork.021 committed
141
                ConsultAssistantEntryUtils.jumpConsultAssistant(this, data[0].location.toInt(),"learning")
严久程 committed
142 143 144
            }
        }

upwork.021 committed
145 146 147 148
        searchWordsAdapter = SearchWordsAdapter(searchSuggestList)
        rv_search_words.layoutManager = LinearLayoutManager(this)
        rv_search_words.adapter = searchWordsAdapter
        searchWordsAdapter.setOnItemClickListener { adapter, view, position ->
149 150
            // 搜索的关联词
            var relatedWords = ""
151
            var isRecommendWords = false
152
            if (searchSuggestList.isNotEmpty()&&searchSuggestList.size>0){
153 154 155 156 157 158
                if (searchSuggestList[position].suggest_relations.size>0){
                    relatedWords = searchSuggestList[position].suggest_relations[0]
                    isRecommendWords = true
                }
                if (TextUtils.isEmpty(relatedWords)&&!TextUtils.isEmpty(searchSuggestList[position].suggest_content)){
                    relatedWords = searchSuggestList[position].suggest_content
159 160
                }
            }
161 162 163 164
            doSearch(searchSuggestList[position].suggest_content,relatedWords,isRecommendWords)

            // 埋点
            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
165
        }
166 167 168 169
        etSearch.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                if (TextUtils.isEmpty(s)) {
                    iv_delete_icon.visibility = View.INVISIBLE
upwork.021 committed
170
                    rv_search_words.visibility = View.GONE
171
                    getSearchWords("",false)
172 173
                } else {
                    iv_delete_icon.visibility = View.VISIBLE
174
                    getSearchWords(s.toString(),false)
175
                }
upwork.021 committed
176

177 178 179 180 181 182 183 184 185
            }

            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
186 187
    }

188

upwork.021 committed
189 190
    /**
     * 获取搜索联想词
191
     * @param isClickWords 是否是点击历史搜索、热门搜索等进行搜索
upwork.021 committed
192
     */
193
    private fun getSearchWords(searchContent:String,isClickWords:Boolean){
upwork.021 committed
194 195 196 197
        if (!TextUtils.isEmpty(searchContent)){
            mSearchContent = searchContent
            val map = HashMap<String, Any>()
            map["content"] = searchContent
198
            mPresenter.getSearchWords(map,searchContent,isClickWords)
upwork.021 committed
199 200 201 202 203
        }else{
            rv_search_words.visibility = View.GONE
        }
    }

204 205 206 207
    override fun getSearchWordsSuccess(searchWordsBean: SearchWordsBean,searchContent:String,isClickWords:Boolean) {
        if (isClickWords){
            // 搜索的关联词
            var relatedWords = ""
208
            var isRecommendWords = false
209
            if (!searchWordsBean.search_suggests.isNullOrEmpty()&&searchWordsBean.search_suggests.size>0){
210 211 212 213 214 215
                if (searchWordsBean.search_suggests[0].suggest_relations.size>0){
                    relatedWords = searchWordsBean.search_suggests[0].suggest_relations[0]
                    isRecommendWords = true
                }
                if (TextUtils.isEmpty(relatedWords)&&!TextUtils.isEmpty(searchWordsBean.search_suggests[0].suggest_content)){
                    relatedWords = searchWordsBean.search_suggests[0].suggest_content
216 217
                }
            }
218
            doSearch(searchContent,relatedWords,isRecommendWords)
upwork.021 committed
219
        }else{
220 221 222 223 224
            searchSuggestList.clear()
            if (!searchWordsBean.search_suggests.isNullOrEmpty()){
                rv_search_words.visibility = View.VISIBLE
                searchSuggestList.addAll(searchWordsBean.search_suggests)
                searchWordsAdapter.notifyDataAndSetSearchWord(mSearchContent)
225 226 227 228

                // 埋点
                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")
229 230 231
            }else{
                rv_search_words.visibility = View.GONE
            }
upwork.021 committed
232
        }
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    }

    private fun initData() {
        initHistoryData()
        mPresenter.localData(this)

        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)
    }

    override fun requestFail() {
        refreshBanner(null)
    }

upwork.021 committed
261

262 263 264 265 266 267 268 269 270
    //刷新 热门搜索
    private fun refreshHotSearchData(keywordData: MutableList<HotSearchKeyWordDataBean>?) {
        if (null == keywordData || keywordData.isEmpty()) {
            llHotSearch.visibility = View.GONE
            return
        }
        llHotSearch.visibility = View.VISIBLE
        flHotSearch.removeAllViews()
        for (index in keywordData.indices) {
严久程 committed
271 272
            val view = LayoutInflater.from(this)
                .inflate(R.layout.consultant_item_expert_hot_search, flHotSearch, false)
273 274
            view.tvHotSearch.text = keywordData[index].keyword
            view.setOnClickListener {
275
                getSearchWords(keywordData[index].keyword!!,true)
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
            }
            flHotSearch.addView(view)
        }
    }

    //刷新 本周热门专家
    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
291 292
            val view = LayoutInflater.from(this)
                .inflate(R.layout.consultant_item_expert_hot_search, flHotSearch, false)
293 294
            view.tvHotSearch.text = hotSearchExpert[index].name
            if (!TextUtils.isEmpty(hotSearchExpert[index].name)) {
严久程 committed
295 296 297 298 299 300 301
                expertWidth += getTextContentWidth(
                    view.findViewById(R.id.tvHotSearch),
                    if (hotSearchExpert[index].name!!.length > 7) hotSearchExpert[index].name!!.substring(
                        0,
                        8
                    ) else hotSearchExpert[index].name!!
                ) + dp42
302 303 304 305 306
                if (expertWidth > maxWidth) {
                    break
                }
            }
            view.setOnClickListener {
307
                getSearchWords(etSearch.text.toString(),true)
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
            }
            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()) {
327
                TempH5RouteUtils.tempH5Route(
328
                    IYDLRouterConstant.ROUTER_H5_H5,
严久程 committed
329 330 331 332 333
                    YDLRouterParams().putExtra(
                        IYDLRouterConstant.EXTRA_URL,
                        "https://h2.yidianling.com/ct/list"
                    ), ""
                )
334
            } else {
335
                TempH5RouteUtils.tempH5Route(focusList[it].linkUrl)
336 337
            }
        }
严久程 committed
338 339
        banner.setImageLoader(GlideImageLoader(R.drawable.consultant_expert_banner_default))
            .setImages(bannerList).start()
340 341 342 343 344 345 346 347 348
        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
349 350 351 352 353
            val list = Gson().fromJson<MutableList<String>>(
                cacheHomeData,
                object : TypeToken<MutableList<String>>() {
                }.type
            )
354 355 356 357
            historyList.addAll(list)
            if (historyList.size > 0) {
                for (historyStr in historyList.reversed()) {

严久程 committed
358 359
                    val view = LayoutInflater.from(this)
                        .inflate(R.layout.consultant_item_expert_hot_search, flHotSearch, false)
360
                    view.tvHotSearch.text = historyStr
严久程 committed
361 362 363 364 365
                    historyCurrentWidth += getTextContentWidth(
                        view.findViewById(R.id.tvHotSearch),
                        if (historyStr.length > 7) historyStr.substring(0, 8) else historyStr
                    ) + RxImageTool.dp2px(50f)
                    if (historyCurrentWidth > maxWidth) {
366 367 368
                        break
                    }
                    view.setOnClickListener {
369
                        getSearchWords(historyStr,true)
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
                    }
                    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
385 386
    /**
     *  搜索
387
     *  @param isRecommendWords 是否是推荐词
upwork.021 committed
388
     */
389
    private fun doSearch(searchWords:String,relatedWords:String,isRecommendWords:Boolean) {
390 391 392 393 394
        val view = this.currentFocus
        if (view != null) {
            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(view.windowToken, 0)
        }
395

upwork.021 committed
396 397 398 399
        ActionCountUtils.count(ConsultBIConstants.UserMainEvent.YDL_USER_SEARCH_CLICK,searchWords)
        if (!TextUtils.isEmpty(searchWords)) {
            historyList.remove(searchWords)
            historyList.add(searchWords)
严久程 committed
400 401 402 403
            SharedPreferencesEditor.putString(
                CACHE_CONSULT_SEARCH_HISTORY_DATA,
                Gson().toJson(historyList)
            )
404
        }
405
        ExpertSearchActivity.startSearch(this, searchWords,"14",relatedWords,isRecommendWords)
upwork.021 committed
406
        finish()
407 408 409 410 411 412 413 414
    }

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