pull/737/head
Robot 4 years ago
commit f71c679287
  1. 42
      app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt
  2. 45
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  3. 11
      app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt

@ -1,42 +0,0 @@
package io.legado.app.ui.book.search
import android.os.Bundle
import androidx.recyclerview.widget.DiffUtil
import io.legado.app.data.entities.SearchBook
class DiffCallBack : DiffUtil.ItemCallback<SearchBook>() {
override fun areItemsTheSame(oldItem: SearchBook, newItem: SearchBook): Boolean {
return when {
oldItem.name != newItem.name -> false
oldItem.author != newItem.author -> false
else -> true
}
}
override fun areContentsTheSame(oldItem: SearchBook, newItem: SearchBook): Boolean {
return when {
oldItem.origins.size != newItem.origins.size -> false
oldItem.coverUrl != newItem.coverUrl -> false
oldItem.kind != newItem.kind -> false
oldItem.latestChapterTitle != newItem.latestChapterTitle -> false
oldItem.intro != newItem.intro -> false
else -> true
}
}
override fun getChangePayload(oldItem: SearchBook, newItem: SearchBook): Any? {
val payload = Bundle()
if (oldItem.name != newItem.name) payload.putString("name", newItem.name)
if (oldItem.author != newItem.author) payload.putString("author", newItem.author)
if (oldItem.origins.size != newItem.origins.size)
payload.putInt("origins", newItem.origins.size)
if (oldItem.coverUrl != newItem.coverUrl) payload.putString("cover", newItem.coverUrl)
if (oldItem.kind != newItem.kind) payload.putString("kind", newItem.kind)
if (oldItem.latestChapterTitle != newItem.latestChapterTitle)
payload.putString("last", newItem.latestChapterTitle)
if (oldItem.intro != newItem.intro) payload.putString("intro", newItem.intro)
if (payload.isEmpty) return null
return payload
}
}

@ -44,7 +44,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
private lateinit var bookAdapter: BookAdapter private lateinit var bookAdapter: BookAdapter
private lateinit var historyKeyAdapter: HistoryKeyAdapter private lateinit var historyKeyAdapter: HistoryKeyAdapter
private lateinit var loadMoreView: LoadMoreView private lateinit var loadMoreView: LoadMoreView
private lateinit var serchView: SearchView private lateinit var searchView: SearchView
private var historyData: LiveData<List<SearchKeyword>>? = null private var historyData: LiveData<List<SearchKeyword>>? = null
private var bookData: LiveData<List<Book>>? = null private var bookData: LiveData<List<Book>>? = null
private var menu: Menu? = null private var menu: Menu? = null
@ -57,7 +57,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
binding.llHistory.setBackgroundColor(backgroundColor) binding.llHistory.setBackgroundColor(backgroundColor)
serchView = binding.titleBar.findViewById(R.id.search_view) searchView = binding.titleBar.findViewById(R.id.search_view)
initRecyclerView() initRecyclerView()
initSearchView() initSearchView()
initOtherView() initOtherView()
@ -87,8 +87,8 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
!getPrefBoolean(PreferKey.precisionSearch) !getPrefBoolean(PreferKey.precisionSearch)
) )
precisionSearchMenuItem?.isChecked = getPrefBoolean(PreferKey.precisionSearch) precisionSearchMenuItem?.isChecked = getPrefBoolean(PreferKey.precisionSearch)
serchView.query?.toString()?.trim()?.let { searchView.query?.toString()?.trim()?.let {
serchView.setQuery(it, true) searchView.setQuery(it, true)
} }
} }
R.id.menu_source_manage -> startActivity<BookSourceActivity>() R.id.menu_source_manage -> startActivity<BookSourceActivity>()
@ -99,8 +99,8 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
} else { } else {
putPrefString("searchGroup", item.title.toString()) putPrefString("searchGroup", item.title.toString())
} }
serchView.query?.toString()?.trim()?.let { searchView.query?.toString()?.trim()?.let {
serchView.setQuery(it, true) searchView.setQuery(it, true)
} }
} }
} }
@ -108,14 +108,14 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
} }
private fun initSearchView() { private fun initSearchView() {
ATH.setTint(serchView, primaryTextColor) ATH.setTint(searchView, primaryTextColor)
serchView.onActionViewExpanded() searchView.onActionViewExpanded()
serchView.isSubmitButtonEnabled = true searchView.isSubmitButtonEnabled = true
serchView.queryHint = getString(R.string.search_book_key) searchView.queryHint = getString(R.string.search_book_key)
serchView.clearFocus() searchView.clearFocus()
serchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean { override fun onQueryTextSubmit(query: String?): Boolean {
serchView.clearFocus() searchView.clearFocus()
query?.let { query?.let {
viewModel.saveSearchKey(query) viewModel.saveSearchKey(query)
viewModel.search(it) viewModel.search(it)
@ -130,8 +130,8 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
return false return false
} }
}) })
serchView.setOnQueryTextFocusChangeListener { _, hasFocus -> searchView.setOnQueryTextFocusChangeListener { _, hasFocus ->
if (!hasFocus && serchView.query.toString().trim().isEmpty()) { if (!hasFocus && searchView.query.toString().trim().isEmpty()) {
finish() finish()
} else { } else {
openOrCloseHistory(hasFocus) openOrCloseHistory(hasFocus)
@ -207,9 +207,9 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
private fun receiptIntent(intent: Intent? = null) { private fun receiptIntent(intent: Intent? = null) {
intent?.getStringExtra("key")?.let { intent?.getStringExtra("key")?.let {
serchView.setQuery(it, true) searchView.setQuery(it, true)
} ?: let { } ?: let {
serchView.requestFocus() searchView.requestFocus()
} }
} }
@ -227,7 +227,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
*/ */
private fun openOrCloseHistory(open: Boolean) { private fun openOrCloseHistory(open: Boolean) {
if (open) { if (open) {
upHistory(serchView.query.toString()) upHistory(searchView.query.toString())
binding.llHistory.visibility = VISIBLE binding.llHistory.visibility = VISIBLE
} else { } else {
binding.llHistory.visibility = GONE binding.llHistory.visibility = GONE
@ -299,7 +299,6 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
/** /**
* 更新搜索结果 * 更新搜索结果
*/ */
@Synchronized
private fun upSearchItems(items: List<SearchBook>) { private fun upSearchItems(items: List<SearchBook>) {
adapter.setItems(items) adapter.setItems(items)
} }
@ -351,14 +350,14 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
override fun searchHistory(key: String) { override fun searchHistory(key: String) {
launch { launch {
when { when {
serchView.query.toString() == key -> { searchView.query.toString() == key -> {
serchView.setQuery(key, true) searchView.setQuery(key, true)
} }
withContext(IO) { App.db.bookDao.findByName(key).isEmpty() } -> { withContext(IO) { App.db.bookDao.findByName(key).isEmpty() } -> {
serchView.setQuery(key, true) searchView.setQuery(key, true)
} }
else -> { else -> {
serchView.setQuery(key, false) searchView.setQuery(key, false)
} }
} }
} }

@ -17,7 +17,16 @@ class SearchAdapter(context: Context, val callBack: CallBack) :
SimpleRecyclerAdapter<SearchBook, ItemSearchBinding>(context) { SimpleRecyclerAdapter<SearchBook, ItemSearchBinding>(context) {
override val diffItemCallback: DiffUtil.ItemCallback<SearchBook> override val diffItemCallback: DiffUtil.ItemCallback<SearchBook>
get() = DiffCallBack() get() = object : DiffUtil.ItemCallback<SearchBook>() {
override fun areContentsTheSame(oldItem: SearchBook, newItem: SearchBook): Boolean {
return true
}
override fun areItemsTheSame(oldItem: SearchBook, newItem: SearchBook): Boolean {
return false
}
}
override fun getViewBinding(parent: ViewGroup): ItemSearchBinding { override fun getViewBinding(parent: ViewGroup): ItemSearchBinding {
return ItemSearchBinding.inflate(inflater, parent, false) return ItemSearchBinding.inflate(inflater, parent, false)

Loading…
Cancel
Save