From 4ddf6599aa551bf876debdaa394abf4598160a12 Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 16 Dec 2020 16:36:33 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/search/SearchAdapter.kt | 44 ++++++- .../app/ui/book/search/SearchViewModel.kt | 119 ++++++------------ 2 files changed, 80 insertions(+), 83 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt index cd2221888..22605747d 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt @@ -3,9 +3,10 @@ package io.legado.app.ui.book.search import android.content.Context import android.os.Bundle import android.view.ViewGroup +import androidx.recyclerview.widget.DiffUtil import io.legado.app.R +import io.legado.app.base.adapter.DiffRecyclerAdapter import io.legado.app.base.adapter.ItemViewHolder -import io.legado.app.base.adapter.RecyclerAdapter import io.legado.app.data.entities.SearchBook import io.legado.app.databinding.ItemSearchBinding import io.legado.app.utils.gone @@ -13,7 +14,46 @@ import io.legado.app.utils.visible import org.jetbrains.anko.sdk27.listeners.onClick class SearchAdapter(context: Context, val callBack: CallBack) : - RecyclerAdapter(context) { + DiffRecyclerAdapter(context) { + + override val diffItemCallback: DiffUtil.ItemCallback + get() = object : DiffUtil.ItemCallback() { + + 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 + } + + } override fun getViewBinding(parent: ViewGroup): ItemSearchBinding { return ItemSearchBinding.inflate(inflater, parent, false) diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt index deb1ab453..431d1ff2c 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt @@ -62,12 +62,8 @@ class SearchViewModel(application: Application) : BaseViewModel(application), } override fun onSearchSuccess(searchBooks: ArrayList) { - if (context.getPrefBoolean(PreferKey.precisionSearch)) { - precisionSearch(this, searchBooks) - } else { - App.db.searchBookDao.insert(*searchBooks.toTypedArray()) - mergeItems(this, searchBooks) - } + val precision = context.getPrefBoolean(PreferKey.precisionSearch) + mergeItems(this, searchBooks, precision) } override fun onSearchFinish() { @@ -80,98 +76,59 @@ class SearchViewModel(application: Application) : BaseViewModel(application), isLoading = false } - /** - * 精确搜索处理 - */ - private fun precisionSearch(scope: CoroutineScope, searchBooks: List) { - val books = arrayListOf() - searchBooks.forEach { searchBook -> - if (searchBook.name.contains(searchKey, true) - || searchBook.author.contains(searchKey, true) - ) books.add(searchBook) - } - App.db.searchBookDao.insert(*books.toTypedArray()) - if (scope.isActive) { - mergeItems(scope, books) - } - } - /** * 合并搜索结果并排序 */ @Synchronized - private fun mergeItems(scope: CoroutineScope, newDataS: List) { + private fun mergeItems(scope: CoroutineScope, newDataS: List, precision: Boolean) { if (newDataS.isNotEmpty()) { - val copyDataS = ArrayList(searchBooks) - val searchBooksAdd = ArrayList() - if (copyDataS.size == 0) { - copyDataS.addAll(newDataS) - } else { - //存在 - newDataS.forEach { item -> + val prevData = ArrayList(searchBooks) + val precisionData = arrayListOf() + prevData.forEach { + if (!scope.isActive) return + if (it.name == searchKey || it.author == searchKey) { + precisionData.add(it) + } + } + repeat(precisionData.size) { + if (!scope.isActive) return + prevData.removeAt(0) + } + newDataS.forEach { nBook -> + if (!scope.isActive) return + if (nBook.name == searchKey || nBook.author == searchKey) { var hasSame = false - for (searchBook in copyDataS) { - if (item.name == searchBook.name - && item.author == searchBook.author - ) { + precisionData.forEach { pBook -> + if (!scope.isActive) return + if (pBook.name == nBook.name && pBook.author == nBook.author) { + pBook.addOrigin(nBook.origin) hasSame = true - searchBook.addOrigin(item.origin) - break } } if (!hasSame) { - searchBooksAdd.add(item) + precisionData.add(nBook) } - } - //添加 - searchBooksAdd.forEach { item -> - if (searchKey == item.name) { - for ((index, searchBook) in copyDataS.withIndex()) { - if (searchKey != searchBook.name) { - copyDataS.add(index, item) - break - } - } - } else if (searchKey == item.author) { - for ((index, searchBook) in copyDataS.withIndex()) { - if (searchKey != searchBook.name && searchKey == searchBook.author) { - copyDataS.add(index, item) - break - } + } else if (!precision) { + var hasSame = false + prevData.forEach { pBook -> + if (!scope.isActive) return + if (pBook.name == nBook.name && pBook.author == nBook.author) { + pBook.addOrigin(nBook.origin) + hasSame = true } - } else { - copyDataS.add(item) } - } - } - if (!scope.isActive) return - searchBooks.sortWith { o1, o2 -> - if (o1.name == searchKey && o2.name != searchKey) { - 1 - } else if (o1.name != searchKey && o2.name == searchKey) { - -1 - } else if (o1.author == searchKey && o2.author != searchKey) { - 1 - } else if (o1.author != searchKey && o2.author == searchKey) { - -1 - } else if (o1.name == o2.name) { - when { - o1.origins.size > o2.origins.size -> { - 1 - } - o1.origins.size < o2.origins.size -> { - -1 - } - else -> { - 0 - } + if (!hasSame) { + prevData.add(nBook) } - } else { - 0 } } if (!scope.isActive) return - searchBooks = copyDataS + precisionData.sortByDescending { it.origins.size } + if (!scope.isActive) return + if (!precision) { + precisionData.addAll(prevData) + } + searchBooks = precisionData upAdapter() } } From 883fa652631e3c55dc3316dee82dbdf95c995470 Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 16 Dec 2020 16:38:56 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/ui/book/search/SearchAdapter.kt | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt index 22605747d..0f92e549e 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt @@ -40,15 +40,16 @@ class SearchAdapter(context: Context, val callBack: CallBack) : 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.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 (oldItem.intro != newItem.intro) + payload.putString("intro", newItem.intro) if (payload.isEmpty) return null return payload } @@ -106,9 +107,6 @@ class SearchAdapter(context: Context, val callBack: CallBack) : with(binding) { bundle.keySet().map { when (it) { - "name" -> tvName.text = searchBook.name - "author" -> tvAuthor.text = - context.getString(R.string.author_show, searchBook.author) "origins" -> bvOriginCount.setBadgeCount(searchBook.origins.size) "last" -> upLasted(binding, searchBook.latestChapterTitle) "intro" -> { From 65facf0706ff8eb1d844aca8de40c5ce6675966e Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 16 Dec 2020 16:48:04 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/ui/book/search/SearchAdapter.kt | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt index 0f92e549e..5057b6389 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt @@ -28,20 +28,12 @@ class SearchAdapter(context: Context, val callBack: CallBack) : } 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 - } + return false } override fun getChangePayload(oldItem: SearchBook, newItem: SearchBook): Any? { val payload = Bundle() - if (oldItem.origins.size != newItem.origins.size) - payload.putInt("origins", newItem.origins.size) + payload.putInt("origins", newItem.origins.size) if (oldItem.coverUrl != newItem.coverUrl) payload.putString("cover", newItem.coverUrl) if (oldItem.kind != newItem.kind) @@ -50,7 +42,6 @@ class SearchAdapter(context: Context, val callBack: CallBack) : payload.putString("last", newItem.latestChapterTitle) if (oldItem.intro != newItem.intro) payload.putString("intro", newItem.intro) - if (payload.isEmpty) return null return payload } From 68f1146c6c55aeac9f0672eed5f41353e76b341a Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 16 Dec 2020 16:48:54 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt index 5057b6389..493454f40 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt @@ -31,7 +31,7 @@ class SearchAdapter(context: Context, val callBack: CallBack) : return false } - override fun getChangePayload(oldItem: SearchBook, newItem: SearchBook): Any? { + override fun getChangePayload(oldItem: SearchBook, newItem: SearchBook): Any { val payload = Bundle() payload.putInt("origins", newItem.origins.size) if (oldItem.coverUrl != newItem.coverUrl) From a61dc18b02cf596f916962473bdb50a3f96c907a Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 16 Dec 2020 16:49:51 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/updateLog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index b1b192255..cea85f283 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -5,6 +5,7 @@ **2020/12/16** * 修复上版本因更新组件引起的一些bug +* 搜索一点都不闪了 **2020/12/15** * 修复一些引起崩溃的bug