TestSearchActivity.kt 10.9 KB
Newer Older
konghaorui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package com.yidianling.tests.search

import android.content.Context
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.LinearLayout
import android.widget.TextView
import cn.lankton.flowlayout.FlowLayout
konghaorui committed
18 19 20 21 22
import com.ydl.ydlcommon.adapter.MyBaseAdapter
import com.ydl.ydlcommon.base.BaseMvpActivity
import com.ydl.ydlcommon.bean.StatusBarOptions
import com.ydl.ydlcommon.utils.SharedPreferencesEditor
import com.ydl.ydlcommon.view.listener.EndlessRecyclerViewScrollListener
konghaorui committed
23 24 25 26 27 28 29
import com.yidianling.common.tools.ToastUtil
import com.yidianling.tests.R
import com.yidianling.tests.home.event.UpdateCouponMoneyEvent
import com.yidianling.tests.home.utils.TestHomeUtils
import com.yidianling.tests.list.model.bean.RecommendSearchItemBean
import com.yidianling.tests.list.model.bean.Test
import com.yidianling.tests.list.view.adapter.TestListRecyclerAdapter
30
import com.yidianling.tests.router.TestsIn
konghaorui committed
31
import de.greenrobot.event.EventBus
32 33
import kotlinx.android.synthetic.main.tests_activity_test_search.*
import kotlinx.android.synthetic.main.tests_item_hot_search.view.*
konghaorui committed
34 35 36 37 38


class TestSearchActivity : BaseMvpActivity<TestSearchView, TestSearchPresenter>(), TestSearchView, View.OnClickListener {
    private var isFirstLoad = true

konghaorui committed
39 40
    override fun getStatusViewOptions(): StatusBarOptions {
        return StatusBarOptions(true, statusBarDarkMode = true)
konghaorui committed
41 42 43
    }

    override fun layoutResId(): Int {
44
        return R.layout.tests_activity_test_search
konghaorui committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    }

    override fun initDataAndEvent() {
        init()
    }

    companion object {
        fun start(context: Context) {
            context.startActivity(Intent(context, TestSearchActivity::class.java))
        }
    }

    private var keyword: String? = null
    private val searchedTestList: MutableList<Test> = ArrayList()
    private val hotSearchedTestList: MutableList<Test> = ArrayList()

    private var searchAdapter: TestListRecyclerAdapter? = null
    private var hotSearchAdapter: TestListRecyclerAdapter? = null
    private var isSearch = false

    private var searchListCurrentPage = 1

    private var onRecommendSearchListResponseFinish = false
    private var onHotRecommendResponseFinish = false

    private fun init() {
        EventBus.getDefault().register(this)
        setupListeners()
        initRecyclerView()
        initData()
    }

    fun initData() {
        var lastCouponMoney = SharedPreferencesEditor.getString(TestHomeUtils.TEST_MAX_COUPON_MONEY_SP_KEY)
        if (!TextUtils.isEmpty(lastCouponMoney)) {
            searchAdapter?.couponMoney = lastCouponMoney
            hotSearchAdapter?.couponMoney = lastCouponMoney
        }
konghaorui committed
83 84
        mPresenter.fetchHotTests()
        mPresenter.hotRecommendRequest()
konghaorui committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    }

    private lateinit var onScrollListener: EndlessRecyclerViewScrollListener

    private var headerHotSearch: LinearLayout? = null
    private var searchHeader: View? = null

    private var flHotSearch: FlowLayout? = null
    private var tvSearchHint: TextView? = null

    private fun initRecyclerView() {
        swipe_refresh_layout.isEnabled = false
        searchAdapter = TestListRecyclerAdapter(this, searchedTestList, TestListRecyclerAdapter.PAGE_TYPE_SEARCH)
        hotSearchAdapter = TestListRecyclerAdapter(this, hotSearchedTestList, TestListRecyclerAdapter.PAGE_TYPE_SEARCH)

        searchAdapter?.onItemClickListener = MyBaseAdapter.OnItemClickListener { _, _, data ->
101
            TestsIn.getTestsImpl().testDetailH5(data.id.toString())
konghaorui committed
102 103 104
        }

        hotSearchAdapter?.onItemClickListener = MyBaseAdapter.OnItemClickListener { _, _, data ->
105
            TestsIn.getTestsImpl().testDetailH5(data.id.toString())
konghaorui committed
106 107
        }

108 109
        headerHotSearch = View.inflate(this, R.layout.tests_header_hot_search, null) as LinearLayout?;
        searchHeader = View.inflate(this, R.layout.tests_header_search_list, null);
konghaorui committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

        tvSearchHint = searchHeader?.findViewById(R.id.tv_search_hint);
        flHotSearch = headerHotSearch?.findViewById(R.id.flHotSearch)

        searchHeader?.let { searchAdapter?.setHeaderView(it) }
        headerHotSearch?.let { hotSearchAdapter?.setHeaderView(it) }

        rv_search_list.adapter = searchAdapter
        rv_hot_list.adapter = hotSearchAdapter

        val layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        val hotLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        rv_search_list.layoutManager = layoutManager
        rv_hot_list.layoutManager = hotLayoutManager


        onScrollListener = object : EndlessRecyclerViewScrollListener(layoutManager) {
            override fun onLoadMore(page: Int, totalItemsCount: Int, view: RecyclerView?) {
                if (isSearch) {
konghaorui committed
129
                    mPresenter.searchTests(keyword, searchListCurrentPage)
konghaorui committed
130 131 132 133 134
                }
            }
        }
        rv_hot_list.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
135
                if (!isFirstLoad && dx!=0 && dx!=0) {
konghaorui committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 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
                    hideSoftInput()
                }
            }
        })
        rv_search_list.addOnScrollListener(onScrollListener)
    }

    private fun setupListeners() {
        tvBack.setOnClickListener(this)
        etSearch.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }

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

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (TextUtils.isEmpty(s.toString())) {
                    setSearchListVisibility(false)
                    isSearch = false
                    hideNoResultView()
                }
            }
        })
        etSearch.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                doSearch()
            }
            true
        }

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tvBack -> {
                finish()
            }
        }
    }

    private fun doSearch() {
        keyword = etSearch.text.toString()
        if (TextUtils.isEmpty(keyword)) {
180
            ToastUtil.toastShort(getString(R.string.tests_no_search_keyword_hint))
konghaorui committed
181 182 183 184 185 186
            return
        }
        hideSoftInput()
        isSearch = true
        searchListCurrentPage = 1
        onScrollListener.resetState()
konghaorui committed
187
        mPresenter.searchTests(keyword, searchListCurrentPage)
konghaorui committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    }

    fun hideSoftInput() {
        val view = this.currentFocus
        if (view != null) {
            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(view.windowToken, 0)
        }
    }

    /**
     * 热门推荐
     */
    override fun onHotRecommendResponse(datalist: List<Test>) {
        rv_hot_list.adapter = hotSearchAdapter
        hotSearchedTestList.clear()

        onHotRecommendResponseFinish = true
        hideRefreshView()
        setSearchListVisibility(false)
        if (datalist.isNotEmpty()) {
            hotSearchAdapter?.hasMore = false
            hotSearchedTestList.addAll(datalist)
            hotSearchAdapter?.notifyDataSetChanged()
        }

        rv_hot_list.postDelayed({
            isFirstLoad = false
        }, 500)
    }

    /**
     * 热门搜索
     */
    override fun onRecommendSearchListResponse(keywordData: List<RecommendSearchItemBean>) {
        if (keywordData.isEmpty()) {
            headerHotSearch?.visibility = View.GONE
            return
        }

        onRecommendSearchListResponseFinish = true
        hideRefreshView()
        setSearchListVisibility(false)

        headerHotSearch?.visibility = View.VISIBLE
        flHotSearch?.removeAllViews()
        for (index in keywordData.indices) {
235
            val view = LayoutInflater.from(this).inflate(R.layout.tests_item_recommend_hot_search, flHotSearch, false)
konghaorui committed
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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 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 323 324 325 326 327 328
            view.tvHotSearch.text = keywordData[index].keyWord
            view.setOnClickListener {
                etSearch.setText(keywordData[index].keyWord)
                doSearch()
            }

            flHotSearch?.addView(view)
        }
    }

    /**
     * 搜索结果
     */
    override fun showSearchResultView(testList: List<Test>?, page: Int) {
        if (page == 1) {
            rv_search_list.adapter = searchAdapter
            onScrollListener.resetState()
            searchedTestList.clear()
        }
        if (testList != null && testList.isNotEmpty()) {
            searchedTestList.addAll(testList)
            searchAdapter?.notifyDataSetChanged()
        } else {
            searchAdapter?.hasMore = false
            searchAdapter?.notifyDataSetChanged()
        }
        tvSearchHint?.text = String.format("为您找到以下跟“%s”有关的内容", keyword)
        setSearchListVisibility(true)
        searchListCurrentPage++
    }


    fun setSearchListVisibility(visibility: Boolean) {
        rv_search_list.visibility = if (visibility) View.VISIBLE else View.GONE
        rv_hot_list.visibility = if (visibility) View.GONE else View.VISIBLE
    }

    override fun createPresenter(): TestSearchPresenter = TestSearchPresenter()

    override fun hideLoadingView() {
        swipe_refresh_layout.isRefreshing = false
    }


    override fun hideNoResultView() {
        llEmpty.visibility = View.GONE
    }


    override fun showLoadingView() {
        swipe_refresh_layout.isRefreshing = true
    }


    override fun showError(msg: String) {
        ToastUtil.toastShort(msg)
    }

    override fun showNoResultView() {
        llEmpty.visibility = View.VISIBLE
        iv_empty.visibility = View.VISIBLE
        tv_search_empty.text = "没有搜到相关信息,换个关键词看看吧"
        tv_search_empty.gravity = Gravity.CENTER_HORIZONTAL
    }

    override fun onResume() {
        super.onResume()
        //每次可见,更新优惠券信息
        TestHomeUtils.updateCouponMoney()
    }

    /**
     * 更新优惠券信息
     * 刷新列表
     */
    fun onEvent(updateCouponMoneyEvent: UpdateCouponMoneyEvent) {
        searchAdapter?.couponMoney = updateCouponMoneyEvent.money
        searchAdapter?.notifyDataSetChanged()
        hotSearchAdapter?.couponMoney = updateCouponMoneyEvent.money
        hotSearchAdapter?.notifyDataSetChanged()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    private fun hideRefreshView() {
        if (onRecommendSearchListResponseFinish && onHotRecommendResponseFinish) {
            hideLoadingView()
        }
    }
}