diff --git a/app/src/main/java/io/legado/app/data/entities/BaseBook.kt b/app/src/main/java/io/legado/app/data/entities/BaseBook.kt index bd5c28db3..acf795430 100644 --- a/app/src/main/java/io/legado/app/data/entities/BaseBook.kt +++ b/app/src/main/java/io/legado/app/data/entities/BaseBook.kt @@ -10,7 +10,7 @@ interface BaseBook { var infoHtml: String? var tocHtml: String? - fun putVariable(key: String, value: String) + fun putVariable(key: String, value: String) {} fun getKindList(): List { val kindList = arrayListOf() diff --git a/app/src/main/java/io/legado/app/data/entities/SearchBook.kt b/app/src/main/java/io/legado/app/data/entities/SearchBook.kt index 7457c9b29..0f92b5eae 100644 --- a/app/src/main/java/io/legado/app/data/entities/SearchBook.kt +++ b/app/src/main/java/io/legado/app/data/entities/SearchBook.kt @@ -64,6 +64,10 @@ data class SearchBook( variable = GSON.toJson(variableMap) } + @Ignore + @IgnoredOnParcel + val origins: LinkedHashSet? = null + fun toBook(): Book { return Book( name = name, diff --git a/app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt b/app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt index 000404970..37158ea5b 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt @@ -1,24 +1,25 @@ package io.legado.app.ui.book.search import androidx.recyclerview.widget.DiffUtil +import io.legado.app.data.entities.SearchBook -class DiffCallBack : DiffUtil.ItemCallback() { - override fun areItemsTheSame(oldItem: SearchEntity, newItem: SearchEntity): Boolean { +class DiffCallBack : DiffUtil.ItemCallback() { + override fun areItemsTheSame(oldItem: SearchBook, newItem: SearchBook): Boolean { return oldItem.name == newItem.name && oldItem.author == newItem.author } - override fun areContentsTheSame(oldItem: SearchEntity, newItem: SearchEntity): Boolean { - return oldItem.originCount == newItem.originCount + override fun areContentsTheSame(oldItem: SearchBook, newItem: SearchBook): Boolean { + return oldItem.origins?.size == newItem.origins?.size && (oldItem.coverUrl == newItem.coverUrl || !oldItem.coverUrl.isNullOrEmpty()) && (oldItem.kind == newItem.kind || !oldItem.kind.isNullOrEmpty()) && (oldItem.latestChapterTitle == newItem.latestChapterTitle || !oldItem.kind.isNullOrEmpty()) && oldItem.intro?.length ?: 0 > newItem.intro?.length ?: 0 } - override fun getChangePayload(oldItem: SearchEntity, newItem: SearchEntity): Any? { + override fun getChangePayload(oldItem: SearchBook, newItem: SearchBook): Any? { return when { - oldItem.originCount != newItem.originCount -> 1 + oldItem.origins?.size != newItem.origins?.size -> 1 oldItem.coverUrl != newItem.coverUrl -> 2 oldItem.kind != newItem.kind -> 3 oldItem.latestChapterTitle != newItem.latestChapterTitle -> 4 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 e967cd9ee..7339ba392 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 @@ -5,6 +5,7 @@ import android.view.View import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter +import io.legado.app.data.entities.SearchBook import io.legado.app.help.ImageLoader import io.legado.app.utils.gone import io.legado.app.utils.visible @@ -14,9 +15,9 @@ import kotlinx.android.synthetic.main.item_search.view.* import org.jetbrains.anko.sdk27.listeners.onClick class SearchAdapter(context: Context, val callBack: CallBack) : - SimpleRecyclerAdapter(context, R.layout.item_search) { + SimpleRecyclerAdapter(context, R.layout.item_search) { - override fun convert(holder: ItemViewHolder, item: SearchEntity, payloads: MutableList) { + override fun convert(holder: ItemViewHolder, item: SearchBook, payloads: MutableList) { if (payloads.isEmpty()) { bind(holder.itemView, item) } else { @@ -24,11 +25,11 @@ class SearchAdapter(context: Context, val callBack: CallBack) : } } - private fun bind(itemView: View, searchBook: SearchEntity) { + private fun bind(itemView: View, searchBook: SearchBook) { with(itemView) { tv_name.text = searchBook.name tv_author.text = context.getString(R.string.author_show, searchBook.author) - bv_originCount.setBadgeCount(searchBook.originCount) + bv_originCount.setBadgeCount(searchBook.origins?.size ?: 1) if (searchBook.latestChapterTitle.isNullOrEmpty()) { tv_lasted.gone() } else { @@ -79,10 +80,10 @@ class SearchAdapter(context: Context, val callBack: CallBack) : } } - private fun bindChange(itemView: View, searchBook: SearchEntity, payloads: MutableList) { + private fun bindChange(itemView: View, searchBook: SearchBook, payloads: MutableList) { with(itemView) { when (payloads[0]) { - 1 -> bv_originCount.setBadgeCount(searchBook.originCount) + 1 -> bv_originCount.setBadgeCount(searchBook.origins?.size ?: 1) 2 -> searchBook.coverUrl.let { ImageLoader.load(context, it)//Glide自动识别http://和file:// .placeholder(R.drawable.image_cover_default) diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchEntity.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchEntity.kt deleted file mode 100644 index 14fe95f14..000000000 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchEntity.kt +++ /dev/null @@ -1,29 +0,0 @@ -package io.legado.app.ui.book.search - -import io.legado.app.utils.splitNotBlank - -data class SearchEntity( - var name: String = "", - var author: String = "", - var kind: String? = null, - var coverUrl: String? = null, - var intro: String? = null, - var wordCount: String? = null, - var latestChapterTitle: String? = null, - var time: Long = 0L, - var originCount: Int = 0 -) { - - fun getKindList(): List { - val kindList = arrayListOf() - wordCount?.let { - if (it.isNotBlank()) kindList.add(it) - } - kind?.let { - val kinds = it.splitNotBlank(",", "\n") - kindList.addAll(kinds) - } - return kindList - } - -} \ No newline at end of file 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 1b16c0c3d..6ef9e9639 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 @@ -22,7 +22,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { var startTime: Long = 0 var searchPage = 0 var isLoading = false - private val booksShow = arrayListOf() + private val searchBooks = arrayListOf() fun search( key: String, @@ -34,7 +34,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { searchPage++ } else { searchKey = key - booksShow.clear() + searchBooks.clear() } if (searchKey.isEmpty()) return startTime = System.currentTimeMillis() @@ -81,8 +81,10 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { } } - private fun addToAdapter(searchBooks: List) { - callBack?.adapter.let { adapter -> + @Synchronized + private fun addToAdapter(books: List) { + if (books.isNotEmpty()) { + val copyDataS = ArrayList(searchBooks) } }