From 2a53b9475429ee5ad6a293a8f839c61158c6a676 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 15 Oct 2019 13:33:12 +0800 Subject: [PATCH] up --- .../ui/main/bookshelf/BooksDiffCallBack.kt | 33 +++++++++++++++++++ .../app/ui/main/bookshelf/BooksFragment.kt | 4 +++ 2 files changed, 37 insertions(+) create mode 100644 app/src/main/java/io/legado/app/ui/main/bookshelf/BooksDiffCallBack.kt diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksDiffCallBack.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksDiffCallBack.kt new file mode 100644 index 000000000..c5b7d7c03 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksDiffCallBack.kt @@ -0,0 +1,33 @@ +package io.legado.app.ui.main.bookshelf + +import androidx.recyclerview.widget.DiffUtil +import io.legado.app.data.entities.Book + +class BooksDiffCallBack(private val oldItems: List, private val newItems: List) : + DiffUtil.Callback() { + + + override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { + return oldItems[oldItemPosition].bookUrl == newItems[newItemPosition].bookUrl + } + + override fun getOldListSize(): Int { + return oldItems.size + } + + override fun getNewListSize(): Int { + return newItems.size + } + + override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { + val oldItem = oldItems[oldItemPosition] + val newItem = newItems[newItemPosition] + return oldItem.name == newItem.name + && oldItem.durChapterTitle == newItem.durChapterTitle + && oldItem.latestChapterTitle == newItem.latestChapterTitle + && oldItem.getDisplayCover() == newItem.getDisplayCover() + && oldItem.getUnreadChapterNum() == newItem.getUnreadChapterNum() + && oldItem.lastCheckCount == newItem.lastCheckCount + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksFragment.kt index 9c20f526b..f65337b52 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksFragment.kt @@ -5,6 +5,7 @@ import android.view.View import androidx.core.content.ContextCompat import androidx.lifecycle.LiveData import androidx.lifecycle.Observer +import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.App @@ -86,7 +87,10 @@ class BooksFragment : VMBaseFragment(R.layout.fragment_books), bookshelfLiveData?.observe( this, Observer { + val diffResult = + DiffUtil.calculateDiff(BooksDiffCallBack(booksAdapter.getItems(), it)) booksAdapter.setItems(it) + diffResult.dispatchUpdatesTo(booksAdapter) }) }