pull/36/head
kunfei 5 years ago
parent 6953feef0d
commit f1e9345c47
  1. 5
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  2. 41
      app/src/main/java/io/legado/app/ui/rss/source/manage/DiffCallBack.kt
  3. 18
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt

@ -136,10 +136,7 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
bookSourceLiveDate?.observe(this, Observer {
search_view.queryHint = getString(R.string.search_book_source_num, it.size)
val diffResult = DiffUtil.calculateDiff(
DiffCallBack(
adapter.getItems(),
it
)
DiffCallBack(adapter.getItems(), it)
)
adapter.setItemsNoNotify(it)
diffResult.dispatchUpdatesTo(adapter)

@ -0,0 +1,41 @@
package io.legado.app.ui.rss.source.manage
import androidx.recyclerview.widget.DiffUtil
import io.legado.app.data.entities.RssSource
class DiffCallBack(
private val oldItems: List<RssSource>,
private val newItems: List<RssSource>
) : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition]
return oldItem.sourceUrl == newItem.sourceUrl
}
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.sourceName == newItem.sourceName
&& oldItem.enabled == newItem.enabled
}
override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition]
return when {
oldItem.sourceName == newItem.sourceName
&& oldItem.enabled != newItem.enabled -> 2
else -> null
}
}
}

@ -5,9 +5,13 @@ import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.widget.SearchView
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.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.VMBaseActivity
import io.legado.app.data.entities.RssSource
@ -29,11 +33,13 @@ class RssSourceActivity : VMBaseActivity<RssSourceViewModel>(R.layout.activity_r
get() = getViewModel(RssSourceViewModel::class.java)
private lateinit var adapter: RssSourceAdapter
private var sourceLiveData: LiveData<List<RssSource>>? = null
override fun onActivityCreated(savedInstanceState: Bundle?) {
setSupportActionBar(toolbar)
initRecyclerView()
initSearchView()
initData()
}
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
@ -81,6 +87,18 @@ class RssSourceActivity : VMBaseActivity<RssSourceViewModel>(R.layout.activity_r
})
}
private fun initData() {
sourceLiveData?.removeObservers(this)
sourceLiveData = App.db.rssSourceDao().liveAll()
sourceLiveData?.observe(this, Observer {
val diffResult = DiffUtil.calculateDiff(
DiffCallBack(adapter.getItems(), it)
)
adapter.setItemsNoNotify(it)
diffResult.dispatchUpdatesTo(adapter)
})
}
override fun del(source: RssSource) {
}

Loading…
Cancel
Save