pull/743/head
gedoor 4 years ago
parent 00ebad810d
commit 543f0aec87
  1. 10
      app/src/main/java/io/legado/app/base/adapter/DiffRecyclerAdapter.kt
  2. 22
      app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt
  3. 58
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt
  4. 44
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt

@ -8,8 +8,6 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import java.util.*
import kotlin.collections.ArrayList
/**
* Created by Invincible on 2017/12/15.
@ -63,14 +61,6 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
}
}
fun swapItem(srcPosition: Int, targetPosition: Int) {
synchronized(lock) {
val list = ArrayList(getItems())
Collections.swap(list, srcPosition, targetPosition)
setItems(list)
}
}
fun updateItem(item: ITEM) =
synchronized(lock) {
val index = asyncListDiffer.currentList.indexOf(item)

@ -9,15 +9,14 @@ import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.Toolbar
import androidx.core.view.isGone
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.BaseDialogFragment
import io.legado.app.base.adapter.DiffRecyclerAdapter
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.RecyclerAdapter
import io.legado.app.data.entities.BookGroup
import io.legado.app.databinding.DialogEditTextBinding
import io.legado.app.databinding.DialogRecyclerViewBinding
@ -137,28 +136,11 @@ class GroupManageDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener
}
private inner class GroupAdapter(context: Context) :
DiffRecyclerAdapter<BookGroup, ItemGroupManageBinding>(context),
RecyclerAdapter<BookGroup, ItemGroupManageBinding>(context),
ItemTouchCallback.Callback {
private var isMoved = false
override val diffItemCallback: DiffUtil.ItemCallback<BookGroup>
get() = object : DiffUtil.ItemCallback<BookGroup>() {
override fun areItemsTheSame(oldItem: BookGroup, newItem: BookGroup): Boolean {
return oldItem.groupId == newItem.groupId
}
override fun areContentsTheSame(
oldItem: BookGroup,
newItem: BookGroup
): Boolean {
return oldItem.groupName == newItem.groupName
&& oldItem.show == newItem.show
}
}
override fun getViewBinding(parent: ViewGroup): ItemGroupManageBinding {
return ItemGroupManageBinding.inflate(inflater, parent, false)
}

@ -8,11 +8,10 @@ import android.view.ViewGroup
import android.widget.ImageView
import android.widget.PopupMenu
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R
import io.legado.app.base.adapter.DiffRecyclerAdapter
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.RecyclerAdapter
import io.legado.app.data.entities.BookSource
import io.legado.app.databinding.ItemBookSourceBinding
import io.legado.app.lib.theme.backgroundColor
@ -23,54 +22,9 @@ import io.legado.app.utils.visible
import org.jetbrains.anko.sdk27.listeners.onClick
class BookSourceAdapter(context: Context, val callBack: CallBack) :
DiffRecyclerAdapter<BookSource, ItemBookSourceBinding>(context),
RecyclerAdapter<BookSource, ItemBookSourceBinding>(context),
Callback {
override val diffItemCallback: DiffUtil.ItemCallback<BookSource>
get() = object : DiffUtil.ItemCallback<BookSource>() {
override fun areItemsTheSame(oldItem: BookSource, newItem: BookSource): Boolean {
return oldItem.bookSourceUrl == newItem.bookSourceUrl
}
override fun areContentsTheSame(oldItem: BookSource, newItem: BookSource): Boolean {
if (oldItem.bookSourceName != newItem.bookSourceName)
return false
if (oldItem.bookSourceGroup != newItem.bookSourceGroup)
return false
if (oldItem.enabled != newItem.enabled)
return false
if (oldItem.enabledExplore != newItem.enabledExplore
|| oldItem.exploreUrl != newItem.exploreUrl
) {
return false
}
return true
}
override fun getChangePayload(oldItem: BookSource, newItem: BookSource): Any? {
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 (oldItem.enabledExplore != newItem.enabledExplore
|| oldItem.exploreUrl != newItem.exploreUrl
) {
payload.putBoolean("showExplore", true)
}
if (payload.isEmpty) {
return null
}
return payload
}
}
private val selected = linkedSetOf<BookSource>()
override fun getViewBinding(parent: ViewGroup): ItemBookSourceBinding {
@ -100,14 +54,6 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
payload.keySet().map {
when (it) {
"selected" -> cbBookSource.isChecked = selected.contains(item)
"name", "group" -> if (item.bookSourceGroup.isNullOrEmpty()) {
cbBookSource.text = item.bookSourceName
} else {
cbBookSource.text =
String.format("%s (%s)", item.bookSourceName, item.bookSourceGroup)
}
"enabled" -> swtEnabled.isChecked = payload.getBoolean(it)
"showExplore" -> upShowExplore(ivExplore, item)
}
}
}

@ -6,11 +6,10 @@ import android.view.View
import android.view.ViewGroup
import android.widget.PopupMenu
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R
import io.legado.app.base.adapter.DiffRecyclerAdapter
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.RecyclerAdapter
import io.legado.app.data.entities.RssSource
import io.legado.app.databinding.ItemRssSourceBinding
import io.legado.app.lib.theme.backgroundColor
@ -19,40 +18,9 @@ import io.legado.app.ui.widget.recycler.ItemTouchCallback
import org.jetbrains.anko.sdk27.listeners.onClick
class RssSourceAdapter(context: Context, val callBack: CallBack) :
DiffRecyclerAdapter<RssSource, ItemRssSourceBinding>(context),
RecyclerAdapter<RssSource, ItemRssSourceBinding>(context),
ItemTouchCallback.Callback {
override val diffItemCallback: DiffUtil.ItemCallback<RssSource>
get() = object : DiffUtil.ItemCallback<RssSource>() {
override fun areItemsTheSame(oldItem: RssSource, newItem: RssSource): Boolean {
return oldItem.sourceUrl == newItem.sourceUrl
}
override fun areContentsTheSame(oldItem: RssSource, newItem: RssSource): Boolean {
return oldItem.sourceName == newItem.sourceName
&& oldItem.sourceGroup == newItem.sourceGroup
&& oldItem.enabled == newItem.enabled
}
override fun getChangePayload(oldItem: RssSource, newItem: RssSource): Any? {
val payload = Bundle()
if (oldItem.sourceName != newItem.sourceName) {
payload.putString("name", newItem.sourceName)
}
if (oldItem.sourceGroup != newItem.sourceGroup) {
payload.putString("group", newItem.sourceGroup)
}
if (oldItem.enabled != newItem.enabled) {
payload.putBoolean("enabled", newItem.enabled)
}
if (payload.isEmpty) {
return null
}
return payload
}
}
private val selected = linkedSetOf<RssSource>()
override fun getViewBinding(parent: ViewGroup): ItemRssSourceBinding {
@ -80,15 +48,7 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) :
} else {
bundle.keySet().map {
when (it) {
"name", "group" ->
if (item.sourceGroup.isNullOrEmpty()) {
cbSource.text = item.sourceName
} else {
cbSource.text =
String.format("%s (%s)", item.sourceName, item.sourceGroup)
}
"selected" -> cbSource.isChecked = selected.contains(item)
"enabled" -> cbSource.isChecked = item.enabled
}
}
}

Loading…
Cancel
Save