2022-04-04 14:03:10 +00:00
|
|
|
package com.coldmint.rust.pro
|
|
|
|
|
2024-02-05 03:53:58 +00:00
|
|
|
import android.annotation.SuppressLint
|
|
|
|
import android.content.Context
|
2022-04-04 14:03:10 +00:00
|
|
|
import android.os.Bundle
|
2022-08-14 02:43:53 +00:00
|
|
|
import android.view.LayoutInflater
|
2024-02-05 03:53:58 +00:00
|
|
|
import android.view.Menu
|
|
|
|
import android.view.MenuItem
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.widget.Button
|
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
|
2024-02-05 03:53:58 +00:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
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
|
2024-02-05 03:53:58 +00:00
|
|
|
import com.coldmint.rust.pro.databinding.ItemStringBinding
|
2023-02-21 11:33:06 +00:00
|
|
|
import com.coldmint.rust.pro.ui.StableLinearLayoutManager
|
2024-02-05 03:53:58 +00:00
|
|
|
import com.google.android.flexbox.FlexboxLayoutManager
|
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
2022-04-04 14:03:10 +00:00
|
|
|
|
|
|
|
/**
|
2022-07-09 16:33:10 +00:00
|
|
|
* 搜索界面
|
2022-04-04 14:03:10 +00:00
|
|
|
*/
|
|
|
|
class SearchActivity : BaseActivity<ActivitySearchBinding>() {
|
2024-02-05 03:53:58 +00:00
|
|
|
lateinit var list: MutableList<String>
|
|
|
|
|
|
|
|
@SuppressLint("CommitPrefEdits", "NotifyDataSetChanged")
|
2022-04-04 14:03:10 +00:00
|
|
|
override fun whenCreateActivity(savedInstanceState: Bundle?, canUseView: Boolean) {
|
2022-07-09 16:33:10 +00:00
|
|
|
title = getString(R.string.search)
|
|
|
|
setReturnButton()
|
2023-02-21 11:33:06 +00:00
|
|
|
viewBinding.recyclerView.layoutManager = StableLinearLayoutManager(this)
|
|
|
|
viewBinding.hotSearchView.layoutManager = StableLinearLayoutManager(this)
|
2022-07-12 23:53:18 +00:00
|
|
|
loadSearchView()
|
|
|
|
loadHotSearch()
|
2024-02-05 03:53:58 +00:00
|
|
|
list = getSharedPreferences("lishi", Context.MODE_PRIVATE)
|
|
|
|
.getStringSet("data", mutableSetOf())
|
|
|
|
?.toMutableList() ?: mutableListOf()
|
|
|
|
viewBinding.hotSearchView2.layoutManager = FlexboxLayoutManager(this)
|
|
|
|
viewBinding.hotSearchView2.adapter = adapter
|
|
|
|
viewBinding.deleat.setOnClickListener {
|
|
|
|
MaterialAlertDialogBuilder(this@SearchActivity)
|
|
|
|
.setTitle("清空所有历史记录")
|
2024-02-11 10:03:57 +00:00
|
|
|
.setPositiveButton("确定") { _, _ ->
|
|
|
|
while (list.isNotEmpty()) {
|
|
|
|
val index = 0
|
|
|
|
list.removeAt(index)
|
|
|
|
adapter.notifyItemRemoved(index)
|
|
|
|
}
|
|
|
|
getSharedPreferences("lishi",
|
|
|
|
Context.MODE_PRIVATE).edit().putStringSet(
|
|
|
|
"data", list.toSet()).apply()
|
|
|
|
/* CoroutineScope(Dispatchers.Main).launch {
|
|
|
|
while (list.isNotEmpty()) {
|
|
|
|
list.removeAt(0) // 删除第一个数据
|
|
|
|
adapter.notifyItemRemoved(0) // 刷新 RecyclerView
|
|
|
|
delay(300) // 每隔一秒执行一次删除操作
|
|
|
|
}
|
|
|
|
}*/
|
2024-02-05 03:53:58 +00:00
|
|
|
}
|
2024-02-11 10:03:57 +00:00
|
|
|
.setNegativeButton("取消", null).show()
|
2024-02-05 03:53:58 +00:00
|
|
|
}
|
|
|
|
viewBinding.searchView.onActionViewExpanded()
|
|
|
|
}
|
2024-02-11 10:03:57 +00:00
|
|
|
|
2024-02-05 03:53:58 +00:00
|
|
|
private fun additem(string: String) {
|
2024-02-11 10:03:57 +00:00
|
|
|
val indexOf = list.indexOf(string)
|
|
|
|
if (indexOf != -1) {
|
|
|
|
list.remove(string)
|
|
|
|
adapter.notifyItemRemoved(indexOf)
|
|
|
|
}
|
|
|
|
list.add(0, string)
|
|
|
|
adapter.notifyItemInserted(0)
|
2024-02-05 03:53:58 +00:00
|
|
|
// 限制历史记录数量为10
|
|
|
|
if (list.size > 10) {
|
|
|
|
list.removeAt(list.lastIndex)
|
|
|
|
}
|
|
|
|
val editor = getSharedPreferences("lishi", Context.MODE_PRIVATE).edit()
|
|
|
|
editor.putStringSet("data", list.toSet())
|
|
|
|
editor.apply()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun search(string: String) {
|
2024-02-11 10:03:57 +00:00
|
|
|
/* val intent = Intent(this@SearchActivity, SearchResultActivity::class.java)
|
|
|
|
intent.putExtra("key",string)
|
|
|
|
startActivity(intent)*/
|
2024-02-05 03:53:58 +00:00
|
|
|
additem(string)
|
|
|
|
|
|
|
|
}
|
2024-02-11 10:03:57 +00:00
|
|
|
|
|
|
|
val adapter: RecyclerView.Adapter<VH> = object : RecyclerView.Adapter<VH>() {
|
2024-02-05 03:53:58 +00:00
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
|
|
|
|
return VH(ItemStringBinding.bind(
|
|
|
|
LayoutInflater.from(viewBinding.root.context)
|
|
|
|
.inflate(R.layout.item_string, parent, false)))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: VH, position: Int) {
|
|
|
|
holder.binding.button.text = list[position]
|
2024-02-11 10:03:57 +00:00
|
|
|
holder.binding.button.setOnClickListener {
|
2024-02-05 03:53:58 +00:00
|
|
|
val a: Button = it as Button
|
|
|
|
search(a.text.toString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-11 10:03:57 +00:00
|
|
|
override fun getItemCount(): Int {
|
2024-02-05 03:53:58 +00:00
|
|
|
viewBinding.textview1Text1.isVisible = list.isEmpty()
|
|
|
|
return list.size
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class VH(itemView: ItemStringBinding) : RecyclerView.ViewHolder(itemView.root) {
|
|
|
|
var binding: ItemStringBinding = itemView
|
2022-07-12 23:53:18 +00:00
|
|
|
}
|
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)
|
2024-02-05 03:53:58 +00:00
|
|
|
adapter.setItemEvent { _, itemHotSearchBinding, _, data ->
|
2022-07-12 23:53:18 +00:00
|
|
|
itemHotSearchBinding.root.setOnClickListener {
|
2024-02-05 03:53:58 +00:00
|
|
|
search(data.keyword)
|
2022-07-12 23:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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,
|
2024-02-05 03:53:58 +00:00
|
|
|
android.widget.SearchView.OnQueryTextListener {
|
2022-07-09 16:33:10 +00:00
|
|
|
override fun onQueryTextSubmit(query: String?): Boolean {
|
2024-02-05 03:53:58 +00:00
|
|
|
if (!query.isNullOrBlank()) {
|
|
|
|
search(query)
|
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 {
|
2024-02-05 03:53:58 +00:00
|
|
|
if (!newText.isNullOrBlank()) {
|
|
|
|
// viewBinding.searchSuggestionsView.setText(R.string.search_suggestions_loading)
|
2022-07-09 16:33:10 +00:00
|
|
|
Search.instance.suggestions(newText,
|
2024-02-05 03:53:58 +00:00
|
|
|
object : ApiCallBack<SearchSuggestionsData> {
|
|
|
|
override fun onResponse(t: SearchSuggestionsData) {
|
|
|
|
val dataList = t.data
|
|
|
|
if (dataList.isEmpty()) {
|
|
|
|
viewBinding.recyclerView.isVisible = false
|
|
|
|
// viewBinding.searchSuggestionsView.setText(R.string.search_suggestions_null)
|
|
|
|
} else {
|
|
|
|
val adapter =
|
|
|
|
SearchSuggestionsAdapter(
|
|
|
|
this@SearchActivity,
|
|
|
|
newText,
|
|
|
|
dataList
|
|
|
|
)
|
|
|
|
adapter.setItemEvent { _, itemSearchSuggestionsBinding, _, s ->
|
|
|
|
itemSearchSuggestionsBinding.root.setOnClickListener {
|
|
|
|
search(s)
|
|
|
|
}
|
2022-07-09 16:33:10 +00:00
|
|
|
}
|
2024-02-05 03:53:58 +00:00
|
|
|
viewBinding.recyclerView.adapter = adapter
|
|
|
|
viewBinding.recyclerView.isVisible = true
|
|
|
|
// val s = String.format(getString(R.string.search_suggestions_number),dataList.size)
|
|
|
|
// viewBinding.searchSuggestionsView.text = s
|
2022-07-09 16:33:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-05 03:53:58 +00:00
|
|
|
override fun onFailure(e: Exception) {
|
2022-07-13 05:52:56 +00:00
|
|
|
|
2024-02-05 03:53:58 +00:00
|
|
|
viewBinding.recyclerView.isVisible = false
|
|
|
|
// viewBinding.searchSuggestionsView.setText(R.string.search_suggestions_null)
|
|
|
|
}
|
2022-07-09 16:33:10 +00:00
|
|
|
|
2024-02-05 03:53:58 +00:00
|
|
|
})
|
2022-07-09 16:33:10 +00:00
|
|
|
} else {
|
2024-02-05 03:53:58 +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
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-05 03:53:58 +00:00
|
|
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
|
|
|
val add = menu.add("搜索")
|
|
|
|
add.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)
|
|
|
|
return super.onCreateOptionsMenu(menu)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
if (item.itemId == 0) {
|
|
|
|
if (!viewBinding.searchView.query.isNullOrBlank()) {
|
|
|
|
search(viewBinding.searchView.query.toString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-02-05 03:53:58 +00:00
|
|
|
override fun onBackPressed() {
|
|
|
|
if (viewBinding.recyclerView.isVisible) {
|
|
|
|
viewBinding.recyclerView.isVisible = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
super.onBackPressed()
|
|
|
|
}
|
|
|
|
|
2022-04-04 14:03:10 +00:00
|
|
|
}
|