pull/94/head
kunfei 5 years ago
parent e62f842635
commit 30d61c61c9
  1. 2
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt
  3. 32
      app/src/main/java/io/legado/app/ui/replacerule/DiffCallBack.kt
  4. 3
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt
  5. 24
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt

@ -151,7 +151,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))
.calculateDiff(DiffCallBack(ArrayList(adapter.getItems()), it))
adapter.setItems(it, false)
diffResult.dispatchUpdatesTo(adapter)
})

@ -109,7 +109,7 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
} else {
payload.keySet().map {
when (it) {
"select" -> cb_book_source.isChecked = selected.contains(item)
"selected" -> cb_book_source.isChecked = selected.contains(item)
"name", "group" -> if (item.bookSourceGroup.isNullOrEmpty()) {
cb_book_source.text = item.bookSourceName
} else {

@ -1,5 +1,6 @@
package io.legado.app.ui.replacerule
import android.os.Bundle
import androidx.recyclerview.widget.DiffUtil
import io.legado.app.data.entities.ReplaceRule
@ -24,19 +25,34 @@ class DiffCallBack(
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition]
return oldItem.name == newItem.name
&& oldItem.group == newItem.group
&& oldItem.isEnabled == newItem.isEnabled
if (oldItem.name != newItem.name) {
return false
}
if (oldItem.group != newItem.group) {
return false
}
if (oldItem.isEnabled != newItem.isEnabled) {
return false
}
return true
}
override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition]
return when {
oldItem.name == newItem.name
&& oldItem.group == newItem.group
&& oldItem.isEnabled != newItem.isEnabled -> 2
else -> null
val payload = Bundle()
if (oldItem.name != newItem.name) {
payload.putString("name", newItem.name)
}
if (oldItem.group != newItem.group) {
payload.putString("group", newItem.group)
}
if (oldItem.isEnabled != newItem.isEnabled) {
payload.putBoolean("enabled", newItem.isEnabled)
}
if (payload.isEmpty) {
return null
}
return payload
}
}

@ -127,7 +127,8 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
if (dataInit) {
setResult(Activity.RESULT_OK)
}
val diffResult = DiffUtil.calculateDiff(DiffCallBack(adapter.getItems(), it))
val diffResult =
DiffUtil.calculateDiff(DiffCallBack(ArrayList(adapter.getItems()), it))
adapter.setItems(it, false)
diffResult.dispatchUpdatesTo(adapter)
dataInit = true

@ -1,8 +1,10 @@
package io.legado.app.ui.replacerule
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
@ -23,7 +25,7 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
getItems().forEach {
selectedIds.add(it.id)
}
notifyItemRangeChanged(0, itemCount, 1)
notifyItemRangeChanged(0, itemCount, bundleOf(Pair("selected", null)))
}
fun revertSelection() {
@ -34,7 +36,7 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
selectedIds.add(it.id)
}
}
notifyItemRangeChanged(0, itemCount, 1)
notifyItemRangeChanged(0, itemCount, bundleOf(Pair("selected", null)))
}
fun getSelectionIds(): LinkedHashSet<Long> {
@ -49,7 +51,8 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
override fun convert(holder: ItemViewHolder, item: ReplaceRule, payloads: MutableList<Any>) {
with(holder.itemView) {
if (payloads.isEmpty()) {
val bundle = payloads.getOrNull(0) as? Bundle
if (bundle == null) {
this.setBackgroundColor(context.backgroundColor)
if (item.group.isNullOrEmpty()) {
cb_name.text = item.name
@ -87,9 +90,18 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
popupMenu.show()
}
} else {
when (payloads[0]) {
1 -> cb_name.isChecked = selectedIds.contains(item.id)
2 -> swt_enabled.isChecked = item.isEnabled
bundle.keySet().map {
when (it) {
"name", "group" ->
if (item.group.isNullOrEmpty()) {
cb_name.text = item.name
} else {
cb_name.text =
String.format("%s (%s)", item.name, item.group)
}
"enabled" -> swt_enabled.isChecked = item.isEnabled
"selected" -> cb_name.isChecked = selectedIds.contains(item.id)
}
}
}
}

Loading…
Cancel
Save