优化代码

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. * Created by Invincible on 2017/12/15.
*/ */
@Suppress("unused") @Suppress("unused", "MemberVisibilityCanBePrivate")
abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Context) : 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) val inflater: LayoutInflater = LayoutInflater.from(context)
private val asyncListDiffer: AsyncListDiffer<ITEM> by lazy { private val asyncListDiffer: AsyncListDiffer<ITEM> by lazy {
AsyncListDiffer(this, diffItemCallback).apply { 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) {} final override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {}
override fun onCurrentListChanged( open fun onCurrentListChanged() {
previousList: MutableList<ITEM>,
currentList: MutableList<ITEM>
) {
} }

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

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

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

@ -196,7 +196,7 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV
} }
docList.sortWith(compareBy({ !it.isDir }, { it.name })) docList.sortWith(compareBy({ !it.isDir }, { it.name }))
withContext(Main) { withContext(Main) {
adapter.setData(docList) adapter.setItems(docList)
} }
} }
} }
@ -232,7 +232,7 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV
} }
} }
docList.sortWith(compareBy({ !it.isDir }, { it.name })) 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) return ItemImportBookBinding.inflate(inflater, parent, false)
} }
override fun onCurrentListChanged() {
upCheckableCount()
}
override fun convert( override fun convert(
holder: ItemViewHolder, holder: ItemViewHolder,
binding: ItemImportBookBinding, binding: ItemImportBookBinding,
@ -86,11 +90,6 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) :
upCheckableCount() upCheckableCount()
} }
fun setData(data: List<DocItem>) {
setItems(data)
upCheckableCount()
}
private fun upCheckableCount() { private fun upCheckableCount() {
checkableCount = 0 checkableCount = 0
getItems().forEach { getItems().forEach {

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

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

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

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

Loading…
Cancel
Save