pull/32/head
parent
d194145c8e
commit
c0fa0821a1
@ -1,107 +1,64 @@ |
|||||||
package io.legado.app.ui.booksource |
package io.legado.app.ui.booksource |
||||||
|
|
||||||
import android.view.LayoutInflater |
import android.content.Context |
||||||
import android.view.View |
|
||||||
import android.view.ViewGroup |
|
||||||
import androidx.paging.PagedList |
|
||||||
import androidx.paging.PagedListAdapter |
|
||||||
import androidx.recyclerview.widget.DiffUtil |
|
||||||
import androidx.recyclerview.widget.RecyclerView |
|
||||||
import io.legado.app.R |
import io.legado.app.R |
||||||
|
import io.legado.app.base.adapter.ItemViewHolder |
||||||
|
import io.legado.app.base.adapter.SimpleRecyclerAdapter |
||||||
import io.legado.app.data.entities.BookSource |
import io.legado.app.data.entities.BookSource |
||||||
import io.legado.app.help.ItemTouchCallback.OnItemTouchCallbackListener |
import io.legado.app.help.ItemTouchCallback.OnItemTouchCallbackListener |
||||||
import io.legado.app.lib.theme.backgroundColor |
import io.legado.app.lib.theme.backgroundColor |
||||||
import kotlinx.android.synthetic.main.item_book_source.view.* |
import kotlinx.android.synthetic.main.item_book_source.view.* |
||||||
import org.jetbrains.anko.sdk27.listeners.onClick |
import org.jetbrains.anko.sdk27.listeners.onClick |
||||||
|
|
||||||
class BookSourceAdapter(val callBack: CallBack) : |
class BookSourceAdapter(context: Context, val callBack: CallBack) : |
||||||
PagedListAdapter<BookSource, BookSourceAdapter.MyViewHolder>(DIFF_CALLBACK), |
SimpleRecyclerAdapter<BookSource>(context, R.layout.item_book_source), |
||||||
OnItemTouchCallbackListener { |
OnItemTouchCallbackListener { |
||||||
|
|
||||||
companion object { |
|
||||||
|
|
||||||
@JvmField |
|
||||||
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<BookSource>() { |
|
||||||
override fun areItemsTheSame(oldItem: BookSource, newItem: BookSource): Boolean = |
|
||||||
oldItem.bookSourceUrl == newItem.bookSourceUrl |
|
||||||
|
|
||||||
override fun areContentsTheSame(oldItem: BookSource, newItem: BookSource): Boolean = |
|
||||||
oldItem.bookSourceUrl == newItem.bookSourceUrl |
|
||||||
&& oldItem.bookSourceName == newItem.bookSourceName |
|
||||||
&& oldItem.bookSourceGroup == newItem.bookSourceGroup |
|
||||||
&& oldItem.enabled == newItem.enabled |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
override fun onSwiped(adapterPosition: Int) { |
override fun onSwiped(adapterPosition: Int) { |
||||||
|
|
||||||
} |
} |
||||||
|
|
||||||
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { |
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { |
||||||
currentList?.let { |
val srcItem = getItem(srcPosition) |
||||||
val srcSource = it[srcPosition] |
val targetItem = getItem(targetPosition) |
||||||
val targetSource = it[targetPosition] |
if (srcItem != null && targetItem != null) { |
||||||
srcSource?.let { a -> |
if (srcItem.customOrder == targetItem.customOrder) { |
||||||
targetSource?.let { b -> |
callBack.upOrder() |
||||||
a.customOrder = targetPosition |
} else { |
||||||
b.customOrder = srcPosition |
val srcOrder = srcItem.customOrder |
||||||
callBack.update(a, b) |
srcItem.customOrder = targetItem.customOrder |
||||||
} |
targetItem.customOrder = srcOrder |
||||||
|
callBack.update(srcItem, targetItem) |
||||||
} |
} |
||||||
} |
} |
||||||
return true |
return true |
||||||
} |
} |
||||||
|
|
||||||
override fun onCurrentListChanged( |
override fun convert(holder: ItemViewHolder, item: BookSource, payloads: MutableList<Any>) { |
||||||
previousList: PagedList<BookSource>?, |
with(holder.itemView) { |
||||||
currentList: PagedList<BookSource>? |
|
||||||
) { |
|
||||||
super.onCurrentListChanged(previousList, currentList) |
|
||||||
callBack.upCount(itemCount) |
|
||||||
} |
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { |
|
||||||
return MyViewHolder( |
|
||||||
LayoutInflater.from(parent.context).inflate( |
|
||||||
R.layout.item_book_source, |
|
||||||
parent, |
|
||||||
false |
|
||||||
) |
|
||||||
) |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: MyViewHolder, position: Int) { |
|
||||||
getItem(position)?.let { holder.bind(it, callBack) } |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { |
|
||||||
|
|
||||||
fun bind(bookSource: BookSource, callBack: CallBack) = with(itemView) { |
|
||||||
this.setBackgroundColor(context.backgroundColor) |
this.setBackgroundColor(context.backgroundColor) |
||||||
if (bookSource.bookSourceGroup.isNullOrEmpty()) { |
if (item.bookSourceGroup.isNullOrEmpty()) { |
||||||
cb_book_source.text = bookSource.bookSourceName |
cb_book_source.text = item.bookSourceName |
||||||
} else { |
} else { |
||||||
cb_book_source.text = |
cb_book_source.text = |
||||||
String.format("%s (%s)", bookSource.bookSourceName, bookSource.bookSourceGroup) |
String.format("%s (%s)", item.bookSourceName, item.bookSourceGroup) |
||||||
} |
} |
||||||
cb_book_source.isChecked = bookSource.enabled |
cb_book_source.isChecked = item.enabled |
||||||
cb_book_source.setOnClickListener { |
cb_book_source.setOnClickListener { |
||||||
bookSource.enabled = cb_book_source.isChecked |
item.enabled = cb_book_source.isChecked |
||||||
callBack.update(bookSource) |
callBack.update(item) |
||||||
} |
} |
||||||
iv_edit_source.onClick { callBack.edit(bookSource) } |
iv_edit_source.onClick { callBack.edit(item) } |
||||||
iv_top_source.onClick { callBack.topSource(bookSource) } |
iv_top_source.onClick { callBack.topSource(item) } |
||||||
iv_del_source.onClick { callBack.del(bookSource) } |
iv_del_source.onClick { callBack.del(item) } |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
interface CallBack { |
interface CallBack { |
||||||
fun upCount(count: Int) |
|
||||||
fun del(bookSource: BookSource) |
fun del(bookSource: BookSource) |
||||||
fun edit(bookSource: BookSource) |
fun edit(bookSource: BookSource) |
||||||
fun update(vararg bookSource: BookSource) |
fun update(vararg bookSource: BookSource) |
||||||
fun topSource(bookSource: BookSource) |
fun topSource(bookSource: BookSource) |
||||||
|
fun upOrder() |
||||||
} |
} |
||||||
} |
} |
@ -0,0 +1,43 @@ |
|||||||
|
package io.legado.app.ui.booksource |
||||||
|
|
||||||
|
import androidx.recyclerview.widget.DiffUtil |
||||||
|
import io.legado.app.data.entities.BookSource |
||||||
|
|
||||||
|
class DiffCallBack( |
||||||
|
private val oldItems: List<BookSource>, |
||||||
|
private val newItems: List<BookSource> |
||||||
|
) : DiffUtil.Callback() { |
||||||
|
|
||||||
|
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { |
||||||
|
val oldItem = oldItems[oldItemPosition] |
||||||
|
val newItem = newItems[newItemPosition] |
||||||
|
return oldItem.bookSourceUrl == newItem.bookSourceUrl |
||||||
|
} |
||||||
|
|
||||||
|
override fun getOldListSize(): Int { |
||||||
|
return oldItems.size |
||||||
|
} |
||||||
|
|
||||||
|
override fun getNewListSize(): Int { |
||||||
|
return newItems.size |
||||||
|
} |
||||||
|
|
||||||
|
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 |
||||||
|
} |
||||||
|
|
||||||
|
override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? { |
||||||
|
val oldItem = oldItems[oldItemPosition] |
||||||
|
val newItem = newItems[newItemPosition] |
||||||
|
return when { |
||||||
|
oldItem.bookSourceName == newItem.bookSourceName |
||||||
|
&& oldItem.bookSourceGroup == newItem.bookSourceGroup |
||||||
|
&& oldItem.enabled != newItem.enabled -> 2 |
||||||
|
else -> null |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue