From e62f842635917835170180456df41e5b36a2a612 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 6 Feb 2020 21:34:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../book/source/manage/BookSourceAdapter.kt | 22 ++++++++++---- .../app/ui/book/source/manage/DiffCallBack.kt | 29 +++++++++++++++++-- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt index 8f5c3c9bc..c5660e07d 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt @@ -1,8 +1,10 @@ package io.legado.app.ui.book.source.manage import android.content.Context +import android.os.Bundle import android.view.Menu import android.widget.PopupMenu +import androidx.core.os.bundleOf import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter @@ -22,7 +24,7 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : getItems().forEach { selected.add(it) } - notifyItemRangeChanged(0, itemCount, 1) + notifyItemRangeChanged(0, itemCount, bundleOf(Pair("selected", null))) } fun revertSelection() { @@ -33,7 +35,7 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : selected.add(it) } } - notifyItemRangeChanged(0, itemCount, 1) + notifyItemRangeChanged(0, itemCount, bundleOf(Pair("selected", null))) } fun getSelection(): LinkedHashSet { @@ -68,7 +70,8 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : override fun convert(holder: ItemViewHolder, item: BookSource, payloads: MutableList) { with(holder.itemView) { - if (payloads.isEmpty()) { + val payload = payloads.getOrNull(0) as? Bundle + if (payload == null) { this.setBackgroundColor(context.backgroundColor) if (item.bookSourceGroup.isNullOrEmpty()) { cb_book_source.text = item.bookSourceName @@ -104,8 +107,17 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : popupMenu.show() } } else { - when (payloads[0]) { - 1 -> cb_book_source.isChecked = selected.contains(item) + payload.keySet().map { + when (it) { + "select" -> cb_book_source.isChecked = selected.contains(item) + "name", "group" -> if (item.bookSourceGroup.isNullOrEmpty()) { + cb_book_source.text = item.bookSourceName + } else { + cb_book_source.text = + String.format("%s (%s)", item.bookSourceName, item.bookSourceGroup) + } + "enabled" -> swt_enabled.isChecked = payload.getBoolean(it) + } } } } diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/DiffCallBack.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/DiffCallBack.kt index 6eb02037a..da0d1292e 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/DiffCallBack.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/DiffCallBack.kt @@ -1,5 +1,6 @@ package io.legado.app.ui.book.source.manage +import android.os.Bundle import androidx.recyclerview.widget.DiffUtil import io.legado.app.data.entities.BookSource @@ -25,9 +26,31 @@ class DiffCallBack( override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { val oldItem = oldItems[oldItemPosition] val newItem = newItems[newItemPosition] - return oldItem.bookSourceName == newItem.bookSourceName - && oldItem.bookSourceGroup == newItem.bookSourceGroup - && oldItem.enabled == newItem.enabled + if (oldItem.bookSourceName != newItem.bookSourceName) + return false + if (oldItem.bookSourceGroup != newItem.bookSourceGroup) + return false + if (oldItem.enabled != newItem.enabled) + return false + return true } + override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? { + val oldItem = oldItems[oldItemPosition] + val newItem = newItems[newItemPosition] + val payload = Bundle() + if (oldItem.bookSourceName != newItem.bookSourceName) { + payload.putString("name", newItem.bookSourceName) + } + if (oldItem.bookSourceGroup != newItem.bookSourceGroup) { + payload.putString("group", newItem.bookSourceGroup) + } + if (oldItem.enabled != newItem.enabled) { + payload.putBoolean("enabled", newItem.enabled) + } + if (payload.isEmpty) { + return null + } + return payload + } } \ No newline at end of file