优化代码

pull/737/head
gedoor 4 years ago
parent 9c50359568
commit b5a274040d
  1. 13
      app/src/main/java/io/legado/app/base/adapter/DiffRecyclerAdapter.kt
  2. 25
      app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt
  3. 1
      app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt
  4. 4
      app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt
  5. 4
      app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt
  6. 9
      app/src/main/java/io/legado/app/ui/book/local/ImportBookAdapter.kt
  7. 5
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt
  8. 1
      app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt
  9. 4
      app/src/main/java/io/legado/app/ui/replace/ReplaceRuleAdapter.kt
  10. 5
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt

@ -14,15 +14,17 @@ import kotlin.collections.ArrayList
/**
* Created by Invincible on 2017/12/15.
*/
@Suppress("unused")
@Suppress("unused", "MemberVisibilityCanBePrivate")
abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Context) :
RecyclerView.Adapter<ItemViewHolder>(), AsyncListDiffer.ListListener<ITEM> {
RecyclerView.Adapter<ItemViewHolder>() {
val inflater: LayoutInflater = LayoutInflater.from(context)
private val asyncListDiffer: AsyncListDiffer<ITEM> by lazy {
AsyncListDiffer(this, diffItemCallback).apply {
addListListener(this@DiffRecyclerAdapter)
addListListener { _, _ ->
onCurrentListChanged()
}
}
}
@ -146,10 +148,7 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
final override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {}
override fun onCurrentListChanged(
previousList: MutableList<ITEM>,
currentList: MutableList<ITEM>
) {
open fun onCurrentListChanged() {
}

@ -88,6 +88,7 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
this.items.addAll(items)
}
notifyDataSetChanged()
onCurrentListChanged()
}
}
@ -100,6 +101,7 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
this.items.addAll(items)
}
diffResult.dispatchUpdatesTo(this)
onCurrentListChanged()
}
}
@ -110,6 +112,7 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
this.items[position] = item
notifyItemChanged(position + getHeaderCount())
}
onCurrentListChanged()
}
}
@ -119,6 +122,7 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
if (this.items.add(item)) {
notifyItemInserted(oldSize + getHeaderCount())
}
onCurrentListChanged()
}
}
@ -127,6 +131,7 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
if (this.items.addAll(position, newItems)) {
notifyItemRangeInserted(position + getHeaderCount(), newItems.size)
}
onCurrentListChanged()
}
}
@ -140,6 +145,7 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
notifyItemRangeInserted(oldSize + getHeaderCount(), newItems.size)
}
}
onCurrentListChanged()
}
}
@ -148,6 +154,7 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
if (this.items.removeAt(position) != null) {
notifyItemRemoved(position + getHeaderCount())
}
onCurrentListChanged()
}
}
@ -156,6 +163,7 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
if (this.items.remove(item)) {
notifyItemRemoved(this.items.indexOf(item) + getHeaderCount())
}
onCurrentListChanged()
}
}
@ -164,6 +172,7 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
if (this.items.removeAll(items)) {
notifyDataSetChanged()
}
onCurrentListChanged()
}
}
@ -176,6 +185,7 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
Collections.swap(this.items, srcPosition, targetPosition)
notifyItemMoved(srcPosition, targetPosition)
}
onCurrentListChanged()
}
}
@ -186,6 +196,7 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
this.items[index] = item
notifyItemChanged(index)
}
onCurrentListChanged()
}
fun updateItem(position: Int, payload: Any) =
@ -208,11 +219,11 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
}
}
fun clearItems() =
synchronized(lock) {
this.items.clear()
notifyDataSetChanged()
}
fun clearItems() = synchronized(lock) {
this.items.clear()
notifyDataSetChanged()
onCurrentListChanged()
}
fun isEmpty() = items.isEmpty()
@ -252,6 +263,10 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
} ?: 0
}
open fun onCurrentListChanged() {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = when {
viewType < TYPE_HEADER_VIEW + getHeaderCount() -> {
ItemViewHolder(headerItems.get(viewType).invoke(parent))

@ -131,7 +131,6 @@ class ArrangeBookActivity : VMBaseActivity<ActivityArrangeBookBinding, ArrangeBo
else -> list.sortedByDescending { it.durChapterTime }
}
adapter.setItems(books)
upSelectCount()
})
}

@ -29,6 +29,10 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) :
return ItemArrangeBookBinding.inflate(inflater, parent, false)
}
override fun onCurrentListChanged() {
callBack.upSelectCount()
}
override fun convert(
holder: ItemViewHolder,
binding: ItemArrangeBookBinding,

@ -196,7 +196,7 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV
}
docList.sortWith(compareBy({ !it.isDir }, { it.name }))
withContext(Main) {
adapter.setData(docList)
adapter.setItems(docList)
}
}
}
@ -232,7 +232,7 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV
}
}
docList.sortWith(compareBy({ !it.isDir }, { it.name }))
adapter.setData(docList)
adapter.setItems(docList)
}
/**

@ -22,6 +22,10 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) :
return ItemImportBookBinding.inflate(inflater, parent, false)
}
override fun onCurrentListChanged() {
upCheckableCount()
}
override fun convert(
holder: ItemViewHolder,
binding: ItemImportBookBinding,
@ -86,11 +90,6 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) :
upCheckableCount()
}
fun setData(data: List<DocItem>) {
setItems(data)
upCheckableCount()
}
private fun upCheckableCount() {
checkableCount = 0
getItems().forEach {

@ -147,10 +147,7 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
}
}
override fun onCurrentListChanged(
previousList: MutableList<BookSource>,
currentList: MutableList<BookSource>
) {
override fun onCurrentListChanged() {
callBack.upCountView()
}

@ -154,7 +154,6 @@ class ReplaceRuleActivity : VMBaseActivity<ActivityReplaceRuleBinding, ReplaceRu
}
adapter.setItems(it)
dataInit = true
upCountView()
})
}

@ -59,6 +59,10 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
return ItemReplaceRuleBinding.inflate(inflater, parent, false)
}
override fun onCurrentListChanged() {
callBack.upCountView()
}
override fun convert(
holder: ItemViewHolder,
binding: ItemReplaceRuleBinding,

@ -128,10 +128,7 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) :
}
}
override fun onCurrentListChanged(
previousList: MutableList<RssSource>,
currentList: MutableList<RssSource>
) {
override fun onCurrentListChanged() {
callBack.upCountView()
}

Loading…
Cancel
Save