RustAssistant/app/src/main/java/com/coldmint/rust/pro/SearchActivity.kt

121 lines
5.4 KiB
Kotlin
Raw Normal View History

2022-04-04 14:03:10 +00:00
package com.coldmint.rust.pro
2022-07-09 16:33:10 +00:00
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
2022-04-04 14:03:10 +00:00
import android.os.Bundle
2022-08-14 02:43:53 +00:00
import android.view.LayoutInflater
2022-07-09 16:33:10 +00:00
import androidx.appcompat.widget.SearchView
2022-04-04 14:03:10 +00:00
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
2022-07-12 23:53:18 +00:00
import com.coldmint.rust.core.dataBean.HotSearchData
2022-07-09 16:33:10 +00:00
import com.coldmint.rust.core.dataBean.SearchSuggestionsData
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.core.interfaces.ApiCallBack
import com.coldmint.rust.core.web.Search
2022-07-12 23:53:18 +00:00
import com.coldmint.rust.pro.adapters.HotSearchAdapter
2022-07-09 16:33:10 +00:00
import com.coldmint.rust.pro.adapters.SearchSuggestionsAdapter
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.pro.base.BaseActivity
import com.coldmint.rust.pro.databinding.ActivitySearchBinding
/**
2022-07-09 16:33:10 +00:00
* 搜索界面
2022-04-04 14:03:10 +00:00
*/
class SearchActivity : BaseActivity<ActivitySearchBinding>() {
override fun whenCreateActivity(savedInstanceState: Bundle?, canUseView: Boolean) {
2022-07-09 16:33:10 +00:00
title = getString(R.string.search)
setReturnButton()
viewBinding.recyclerView.layoutManager = LinearLayoutManager(this)
2022-07-12 23:53:18 +00:00
viewBinding.hotSearchView.layoutManager = LinearLayoutManager(this)
loadSearchView()
loadHotSearch()
}
2022-04-04 14:03:10 +00:00
2022-07-12 23:53:18 +00:00
private fun loadHotSearch() {
Search.instance.hotSearch(object : ApiCallBack<HotSearchData> {
override fun onResponse(t: HotSearchData) {
val adapter = HotSearchAdapter(this@SearchActivity, t.data)
adapter.setItemEvent { i, itemHotSearchBinding, viewHolder, data ->
itemHotSearchBinding.root.setOnClickListener {
val intent = Intent(this@SearchActivity, SearchResultActivity::class.java)
intent.putExtra("key", data.keyword)
startActivity(intent)
}
}
viewBinding.hotSearchView.adapter = adapter
}
override fun onFailure(e: Exception) {
}
})
}
private fun loadSearchView() {
2022-07-09 16:33:10 +00:00
viewBinding.searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener,
android.widget.SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
if (query != null && query.isNotBlank()) {
val intent = Intent(this@SearchActivity, SearchResultActivity::class.java)
intent.putExtra("key", query)
startActivity(intent)
2022-04-04 14:03:10 +00:00
}
2022-07-09 16:33:10 +00:00
return true
2022-04-04 14:03:10 +00:00
}
2022-07-09 16:33:10 +00:00
override fun onQueryTextChange(newText: String?): Boolean {
if (newText != null && newText.isNotBlank()) {
2022-07-16 14:41:37 +00:00
// viewBinding.searchSuggestionsView.setText(R.string.search_suggestions_loading)
2022-07-09 16:33:10 +00:00
Search.instance.suggestions(newText,
object : ApiCallBack<SearchSuggestionsData> {
override fun onResponse(t: SearchSuggestionsData) {
val dataList = t.data
if (dataList.isNullOrEmpty()) {
viewBinding.recyclerView.isVisible = false
2022-07-16 14:41:37 +00:00
// viewBinding.searchSuggestionsView.setText(R.string.search_suggestions_null)
2022-07-09 16:33:10 +00:00
} else {
val adapter =
SearchSuggestionsAdapter(
this@SearchActivity,
newText,
dataList
)
adapter.setItemEvent { i, itemSearchSuggestionsBinding, viewHolder, s ->
itemSearchSuggestionsBinding.root.setOnClickListener {
val intent = Intent(
this@SearchActivity,
SearchResultActivity::class.java
)
intent.putExtra("key", s)
startActivity(intent)
}
}
viewBinding.recyclerView.adapter = adapter
viewBinding.recyclerView.isVisible = true
2022-07-16 14:41:37 +00:00
// val s = String.format(getString(R.string.search_suggestions_number),dataList.size)
// viewBinding.searchSuggestionsView.text = s
2022-07-09 16:33:10 +00:00
}
}
override fun onFailure(e: Exception) {
2022-07-13 05:52:56 +00:00
2022-07-09 16:33:10 +00:00
viewBinding.recyclerView.isVisible = false
2022-07-16 14:41:37 +00:00
// viewBinding.searchSuggestionsView.setText(R.string.search_suggestions_null)
2022-07-09 16:33:10 +00:00
}
})
} else {
2022-07-16 14:41:37 +00:00
// viewBinding.searchSuggestionsView.setText(R.string.search_suggestions_null)
2022-07-09 16:33:10 +00:00
viewBinding.recyclerView.isVisible = false
}
return true
2022-04-04 14:03:10 +00:00
}
})
}
2022-08-14 02:43:53 +00:00
override fun getViewBindingObject(layoutInflater: LayoutInflater): ActivitySearchBinding {
2022-04-04 14:03:10 +00:00
return ActivitySearchBinding.inflate(layoutInflater)
}
2022-07-09 16:33:10 +00:00
2022-04-04 14:03:10 +00:00
}