pull/94/head
kunfei 5 years ago
parent d8c518281b
commit 73f3af1606
  1. 9
      app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksDiffCallBack.kt
  2. 9
      app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt
  3. 34
      app/src/main/java/io/legado/app/ui/main/explore/ExploreDiffCallBack.kt
  4. 14
      app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt

@ -7,11 +7,6 @@ import io.legado.app.data.entities.Book
class BooksDiffCallBack(private val oldItems: List<Book>, private val newItems: List<Book>) : class BooksDiffCallBack(private val oldItems: List<Book>, private val newItems: List<Book>) :
DiffUtil.Callback() { DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return oldItems[oldItemPosition].bookUrl == newItems[newItemPosition].bookUrl
}
override fun getOldListSize(): Int { override fun getOldListSize(): Int {
return oldItems.size return oldItems.size
} }
@ -20,6 +15,10 @@ class BooksDiffCallBack(private val oldItems: List<Book>, private val newItems:
return newItems.size return newItems.size
} }
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return oldItems[oldItemPosition].bookUrl == newItems[newItemPosition].bookUrl
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldItems[oldItemPosition] val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition] val newItem = newItems[newItemPosition]

@ -104,13 +104,8 @@ class BooksFragment : BaseFragment(R.layout.fragment_books),
else -> App.db.bookDao().observeByGroup(groupId) else -> App.db.bookDao().observeByGroup(groupId)
} }
bookshelfLiveData?.observe(this, Observer { bookshelfLiveData?.observe(this, Observer {
val diffResult = val diffResult = DiffUtil
DiffUtil.calculateDiff( .calculateDiff(BooksDiffCallBack(ArrayList(booksAdapter.getItems()), it))
BooksDiffCallBack(
booksAdapter.getItems(),
it
)
)
booksAdapter.setItems(it, false) booksAdapter.setItems(it, false)
diffResult.dispatchUpdatesTo(booksAdapter) diffResult.dispatchUpdatesTo(booksAdapter)
}) })

@ -0,0 +1,34 @@
package io.legado.app.ui.main.explore
import androidx.recyclerview.widget.DiffUtil
import io.legado.app.data.entities.BookSource
class ExploreDiffCallBack(
private val oldItems: List<BookSource>,
private val newItems: List<BookSource>
) :
DiffUtil.Callback() {
override fun getOldListSize(): Int {
return oldItems.size
}
override fun getNewListSize(): Int {
return newItems.size
}
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return true
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition]
if (oldItem.bookSourceName != newItem.bookSourceName) {
return false
}
return true
}
}

@ -5,7 +5,9 @@ import android.view.View
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseFragment import io.legado.app.base.VMBaseFragment
@ -61,6 +63,15 @@ class ExploreFragment : VMBaseFragment<ExploreViewModel>(R.layout.fragment_find_
rv_find.layoutManager = linearLayoutManager rv_find.layoutManager = linearLayoutManager
adapter = ExploreAdapter(requireContext(), this, this) adapter = ExploreAdapter(requireContext(), this, this)
rv_find.adapter = adapter rv_find.adapter = adapter
adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
super.onItemRangeInserted(positionStart, itemCount)
if (positionStart == 0) {
rv_find.scrollToPosition(0)
}
}
})
} }
private fun initData(key: String? = null) { private fun initData(key: String? = null) {
@ -71,7 +82,10 @@ class ExploreFragment : VMBaseFragment<ExploreViewModel>(R.layout.fragment_find_
App.db.bookSourceDao().liveExplore("%$key%") App.db.bookSourceDao().liveExplore("%$key%")
} }
liveExplore?.observe(viewLifecycleOwner, Observer { liveExplore?.observe(viewLifecycleOwner, Observer {
val diffResult = DiffUtil
.calculateDiff(ExploreDiffCallBack(ArrayList(adapter.getItems()), it))
adapter.setItems(it) adapter.setItems(it)
diffResult.dispatchUpdatesTo(adapter)
}) })
} }

Loading…
Cancel
Save