HotSearchActivity.kt 12.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
严久程 committed
17 18 19
import com.ydl.ydl_image.config.ISimpleImageOpConfig
import com.ydl.ydl_image.config.SimpleImageOpConfiger
import com.ydl.ydl_image.manager.YDLImageCacheManager
严久程 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 26
import com.ydl.ydlcommon.router.IYDLRouterConstant
import com.ydl.ydlcommon.utils.FixSizeLinkedList
严久程 committed
27
import com.ydl.ydlcommon.utils.LogUtil
28 29
import com.ydl.ydlcommon.utils.SharedPreferencesEditor
import com.ydl.ydlcommon.utils.StatusBarUtils
霍志良 committed
30
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils
31 32 33
import com.ydl.ydlcommon.view.banner.GlideImageLoader
import com.yidianling.common.tools.RxDeviceTool
import com.yidianling.common.tools.RxImageTool
严久程 committed
34
import com.yidianling.consultant.api.IConsultantService
35 36 37 38
import com.yidianling.consultant.bean.HotSearchBean
import com.yidianling.consultant.bean.HotSearchFocusItemBean
import com.yidianling.consultant.bean.HotSearchKeyWordDataBean
import com.yidianling.consultant.bean.HotSearchPopularDoctorBean
霍志良 committed
39
import com.yidianling.consultant.constants.ConsultBIConstants
40
import com.yidianling.consultant.contract.IHotSearchContract
严久程 committed
41
import com.yidianling.consultant.modular.utils.ConsultAssistantEntryUtils
42
import com.yidianling.consultant.modular.utils.TempH5RouteUtils
43
import com.yidianling.consultant.presenter.HotSearchPresenterImpl
44 45
import kotlinx.android.synthetic.main.consultant_activity_hot_search.*
import kotlinx.android.synthetic.main.consultant_item_expert_hot_search.view.*
46 47

@Route(path = "/consult/hot_search")
严久程 committed
48 49
class HotSearchActivity : BaseMvpActivity<IHotSearchContract.View, IHotSearchContract.Presenter>(),
    IHotSearchContract.View {
50 51 52 53 54

    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
55

56 57
    //历史搜索内容的最大宽度
    private var maxWidth: Int = 0
严久程 committed
58

59 60
    //历史搜索实际宽度
    private var historyCurrentWidth: Int = 0
严久程 committed
61

62 63 64 65 66 67
    //热门专家搜索实际宽度
    private var expertWidth: Int = 0

    private var dp42: Int = 0

    override fun layoutResId(): Int {
68
        return R.layout.consultant_activity_hot_search
69 70 71 72 73 74 75 76 77
    }

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

    override fun getStatusViewOptions(): StatusBarOptions {
        return StatusBarOptions(isAddStatusView = true)
    }
严久程 committed
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
    override fun initDataAndEvent() {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE or WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
        getDataFromIntent()
        initViews()
        initData()
    }

    private fun getDataFromIntent() {
        var doctorName = intent.getStringExtra(HOT_SEARCH_DOCTOR_NAME)
        if (!TextUtils.isEmpty(doctorName)) {
            etSearch.setText(doctorName)
            iv_delete_icon.visibility = View.VISIBLE
        }
    }

    private fun initViews() {
        iv_delete_icon.setOnClickListener {
            etSearch.setText("")
        }
        etSearch.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                doSearch()
            }
            true
        }
        etSearch.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                doSearch()
            }
            true
        }
        tv_search_cancle.setOnClickListener {
            finish()
        }
        iv_delete_history.setOnClickListener {
            SharedPreferencesEditor.putString(CACHE_CONSULT_SEARCH_HISTORY_DATA, "")
            initHistoryData()
        }
严久程 committed
117

霍志良 committed
118
        val data = ModularServiceManager.provide(IConsultantService::class.java).getGuideImage(5)
严久程 committed
119 120 121
        if (data?.size ?: 0 > 0) {
            val imageUrl= data?.get(0)?.pic
            iv_daoyi_image.visibility=View.VISIBLE
严久程 committed
122 123 124 125 126

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

严久程 committed
127 128
            iv_daoyi_image.setOnClickListener {
                LogUtil.e("跳转导医:location=${data?.get(0)!!.location},title=${data[0].title}")
霍志良 committed
129
                ConsultAssistantEntryUtils.jumpConsultAssistant(this,data?.get(0)!!.location.toInt(),"learning")
严久程 committed
130 131 132 133
            }
        }


134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        etSearch.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                if (TextUtils.isEmpty(s)) {
                    iv_delete_icon.visibility = View.INVISIBLE
                } else {
                    iv_delete_icon.visibility = View.VISIBLE
                }
            }

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

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

            }

        })
        StatusBarUtils.statusBarLightMode(this)
严久程 committed
151 152
        maxWidth =
            (2 * RxDeviceTool.getScreenWidth(this@HotSearchActivity)) - RxImageTool.dip2px(60f)
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
        dp42 = RxImageTool.dip2px(60f)
    }

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

    //刷新 热门搜索
    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
191 192
            val view = LayoutInflater.from(this)
                .inflate(R.layout.consultant_item_expert_hot_search, flHotSearch, false)
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
            view.tvHotSearch.text = keywordData[index].keyword
            view.setOnClickListener {
                etSearch.setText(keywordData[index].keyword)
                doSearch()
            }
            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
212 213
            val view = LayoutInflater.from(this)
                .inflate(R.layout.consultant_item_expert_hot_search, flHotSearch, false)
214 215
            view.tvHotSearch.text = hotSearchExpert[index].name
            if (!TextUtils.isEmpty(hotSearchExpert[index].name)) {
严久程 committed
216 217 218 219 220 221 222
                expertWidth += getTextContentWidth(
                    view.findViewById(R.id.tvHotSearch),
                    if (hotSearchExpert[index].name!!.length > 7) hotSearchExpert[index].name!!.substring(
                        0,
                        8
                    ) else hotSearchExpert[index].name!!
                ) + dp42
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
                if (expertWidth > maxWidth) {
                    break
                }
            }
            view.setOnClickListener {
                etSearch.setText(hotSearchExpert[index].name)
                doSearch()
            }
            flHotExpert.addView(view)
        }
    }

    //刷新 banner
    private fun refreshBanner(focusList: MutableList<HotSearchFocusItemBean>?) {
        if (null == focusList) {
//            if (bannerList.isEmpty()) {
//                bannerList.add("https://h2.yidianling.com/ct/list")
//            }
            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()) {
252
                TempH5RouteUtils.tempH5Route(
253
                    IYDLRouterConstant.ROUTER_H5_H5,
严久程 committed
254 255 256 257 258
                    YDLRouterParams().putExtra(
                        IYDLRouterConstant.EXTRA_URL,
                        "https://h2.yidianling.com/ct/list"
                    ), ""
                )
259
            } else {
260
                TempH5RouteUtils.tempH5Route(focusList[it].linkUrl)
261 262
            }
        }
严久程 committed
263 264
        banner.setImageLoader(GlideImageLoader(R.drawable.consultant_expert_banner_default))
            .setImages(bannerList).start()
265 266 267 268 269 270 271 272 273
        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
274 275 276 277 278
            val list = Gson().fromJson<MutableList<String>>(
                cacheHomeData,
                object : TypeToken<MutableList<String>>() {
                }.type
            )
279 280 281 282
            historyList.addAll(list)
            if (historyList.size > 0) {
                for (historyStr in historyList.reversed()) {

严久程 committed
283 284
                    val view = LayoutInflater.from(this)
                        .inflate(R.layout.consultant_item_expert_hot_search, flHotSearch, false)
285
                    view.tvHotSearch.text = historyStr
严久程 committed
286 287 288 289 290
                    historyCurrentWidth += getTextContentWidth(
                        view.findViewById(R.id.tvHotSearch),
                        if (historyStr.length > 7) historyStr.substring(0, 8) else historyStr
                    ) + RxImageTool.dp2px(50f)
                    if (historyCurrentWidth > maxWidth) {
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
                        break
                    }
                    view.setOnClickListener {
                        etSearch.setText(historyStr)
                        doSearch()
                    }
                    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()//文字宽
    }

    //执行搜索
    private fun doSearch() {
        val view = this.currentFocus
        if (view != null) {
            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(view.windowToken, 0)
        }
        val keyWord = etSearch.text.toString()
//        if (TextUtils.isEmpty(keyWord.trim())) {
//            ToastUtil.toastShort("请输入搜索内容")
//            return
//        }
霍志良 committed
323
        ActionCountUtils.count(ConsultBIConstants.UserMainEvent.YDL_USER_SEARCH_CLICK,keyWord)
324 325 326
        if (!TextUtils.isEmpty(keyWord)) {
            historyList.remove(keyWord)
            historyList.add(keyWord)
严久程 committed
327 328 329 330
            SharedPreferencesEditor.putString(
                CACHE_CONSULT_SEARCH_HISTORY_DATA,
                Gson().toJson(historyList)
            )
331 332 333 334 335 336 337 338 339 340 341 342 343 344
        }


        ExpertSearchActivity.start(this, keyWord)

        this.finish()
    }

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