pull/94/head
kunfei 5 years ago
parent 153830e143
commit 413f602809
  1. 19
      app/src/main/java/io/legado/app/help/ItemTouchCallback.kt
  2. 26
      app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt
  3. 25
      app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt
  4. 51
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt
  5. 14
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt
  6. 2
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt
  7. 21
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt

@ -104,13 +104,21 @@ class ItemTouchCallback : ItemTouchHelper.Callback() {
viewPager?.requestDisallowInterceptTouchEvent(swiping) viewPager?.requestDisallowInterceptTouchEvent(swiping)
} }
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
super.clearView(recyclerView, viewHolder)
onItemTouchCallbackListener?.clearView(recyclerView, viewHolder)
}
interface OnItemTouchCallbackListener { interface OnItemTouchCallbackListener {
/** /**
* 当某个Item被滑动删除的时候 * 当某个Item被滑动删除的时候
* *
* @param adapterPosition item的position * @param adapterPosition item的position
*/ */
fun onSwiped(adapterPosition: Int) fun onSwiped(adapterPosition: Int) {
}
/** /**
* 当两个Item位置互换的时候被回调 * 当两个Item位置互换的时候被回调
@ -119,6 +127,13 @@ class ItemTouchCallback : ItemTouchHelper.Callback() {
* @param targetPosition 目的地的Item的position * @param targetPosition 目的地的Item的position
* @return 开发者处理了操作应该返回true开发者没有处理就返回false * @return 开发者处理了操作应该返回true开发者没有处理就返回false
*/ */
fun onMove(srcPosition: Int, targetPosition: Int): Boolean fun onMove(srcPosition: Int, targetPosition: Int): Boolean {
return false
}
fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
}
} }
} }

@ -15,6 +15,7 @@ import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
@ -35,6 +36,8 @@ import kotlinx.android.synthetic.main.dialog_edit_text.view.*
import kotlinx.android.synthetic.main.dialog_recycler_view.* import kotlinx.android.synthetic.main.dialog_recycler_view.*
import kotlinx.android.synthetic.main.item_group_manage.view.* import kotlinx.android.synthetic.main.item_group_manage.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
import java.util.*
import kotlin.collections.ArrayList
class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
private lateinit var viewModel: GroupViewModel private lateinit var viewModel: GroupViewModel
@ -175,6 +178,8 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
SimpleRecyclerAdapter<BookGroup>(context, R.layout.item_group_manage), SimpleRecyclerAdapter<BookGroup>(context, R.layout.item_group_manage),
ItemTouchCallback.OnItemTouchCallbackListener { ItemTouchCallback.OnItemTouchCallbackListener {
private var isMoved = false
override fun convert(holder: ItemViewHolder, item: BookGroup, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: BookGroup, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {
tv_group.text = item.groupName tv_group.text = item.groupName
@ -184,19 +189,20 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
} }
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { override fun onMove(srcPosition: Int, targetPosition: Int): Boolean {
val srcItem = getItem(srcPosition) Collections.swap(getItems(), srcPosition, targetPosition)
val targetItem = getItem(targetPosition) notifyItemMoved(srcPosition, targetPosition)
if (srcItem != null && targetItem != null) { isMoved = true
val order = srcItem.order
srcItem.order = targetItem.order
targetItem.order = order
viewModel.upGroup(srcItem, targetItem)
}
return true return true
} }
override fun onSwiped(adapterPosition: Int) { override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
if (isMoved) {
for ((index, item) in getItems().withIndex()) {
item.order = index + 1
}
viewModel.upGroup(*getItems().toTypedArray())
}
isMoved = false
} }
} }

@ -15,6 +15,7 @@ import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
@ -34,6 +35,7 @@ import kotlinx.android.synthetic.main.dialog_edit_text.view.*
import kotlinx.android.synthetic.main.dialog_recycler_view.* import kotlinx.android.synthetic.main.dialog_recycler_view.*
import kotlinx.android.synthetic.main.item_group_manage.view.* import kotlinx.android.synthetic.main.item_group_manage.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
import java.util.*
class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
@ -152,6 +154,8 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
SimpleRecyclerAdapter<BookGroup>(context, R.layout.item_group_manage), SimpleRecyclerAdapter<BookGroup>(context, R.layout.item_group_manage),
ItemTouchCallback.OnItemTouchCallbackListener { ItemTouchCallback.OnItemTouchCallbackListener {
private var isMoved: Boolean = false
override fun convert(holder: ItemViewHolder, item: BookGroup, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: BookGroup, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {
tv_group.text = item.groupName tv_group.text = item.groupName
@ -165,19 +169,20 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
} }
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { override fun onMove(srcPosition: Int, targetPosition: Int): Boolean {
val srcItem = getItem(srcPosition) Collections.swap(getItems(), srcPosition, targetPosition)
val targetItem = getItem(targetPosition) notifyItemMoved(srcPosition, targetPosition)
if (srcItem != null && targetItem != null) { isMoved = true
val order = srcItem.order
srcItem.order = targetItem.order
targetItem.order = order
viewModel.upGroup(srcItem, targetItem)
}
return true return true
} }
override fun onSwiped(adapterPosition: Int) { override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
if (isMoved) {
for ((index, item) in getItems().withIndex()) {
item.order = index + 1
}
viewModel.upGroup(*getItems().toTypedArray())
}
isMoved = false
} }
} }

@ -5,6 +5,7 @@ import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.widget.PopupMenu import android.widget.PopupMenu
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
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.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.base.adapter.SimpleRecyclerAdapter
@ -13,6 +14,7 @@ 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
import java.util.*
class BookSourceAdapter(context: Context, val callBack: CallBack) : class BookSourceAdapter(context: Context, val callBack: CallBack) :
SimpleRecyclerAdapter<BookSource>(context, R.layout.item_book_source), SimpleRecyclerAdapter<BookSource>(context, R.layout.item_book_source),
@ -48,26 +50,6 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
return selection return selection
} }
override fun onSwiped(adapterPosition: Int) {
}
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean {
val srcItem = getItem(srcPosition)
val targetItem = getItem(targetPosition)
if (srcItem != null && targetItem != null) {
if (srcItem.customOrder == targetItem.customOrder) {
callBack.upOrder()
} else {
val srcOrder = srcItem.customOrder
srcItem.customOrder = targetItem.customOrder
targetItem.customOrder = srcOrder
callBack.update(srcItem, targetItem)
}
}
return true
}
override fun convert(holder: ItemViewHolder, item: BookSource, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: BookSource, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {
val payload = payloads.getOrNull(0) as? Bundle val payload = payloads.getOrNull(0) as? Bundle
@ -121,6 +103,35 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
} }
} }
} }
}
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean {
val srcItem = getItem(srcPosition)
val targetItem = getItem(targetPosition)
if (srcItem != null && targetItem != null) {
if (srcItem.customOrder == targetItem.customOrder) {
callBack.upOrder()
} else {
val srcOrder = srcItem.customOrder
srcItem.customOrder = targetItem.customOrder
targetItem.customOrder = srcOrder
movedItems.add(srcItem)
movedItems.add(targetItem)
}
}
Collections.swap(getItems(), srcPosition, targetPosition)
notifyItemMoved(srcPosition, targetPosition)
return true
}
private val movedItems = hashSetOf<BookSource>()
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
if (movedItems.isNotEmpty()) {
callBack.update(*movedItems.toTypedArray())
movedItems.clear()
}
} }
interface CallBack { interface CallBack {

@ -5,6 +5,7 @@ import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.widget.PopupMenu import android.widget.PopupMenu
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
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.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.base.adapter.SimpleRecyclerAdapter
@ -13,6 +14,7 @@ import io.legado.app.help.ItemTouchCallback
import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.backgroundColor
import kotlinx.android.synthetic.main.item_replace_rule.view.* import kotlinx.android.synthetic.main.item_replace_rule.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
import java.util.*
class ReplaceRuleAdapter(context: Context, var callBack: CallBack) : class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
@ -117,14 +119,22 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
val srcOrder = srcItem.order val srcOrder = srcItem.order
srcItem.order = targetItem.order srcItem.order = targetItem.order
targetItem.order = srcOrder targetItem.order = srcOrder
callBack.update(srcItem, targetItem) movedItems.add(srcItem)
movedItems.add(targetItem)
} }
} }
Collections.swap(getItems(), srcPosition, targetPosition)
notifyItemMoved(srcPosition, targetPosition)
return true return true
} }
override fun onSwiped(adapterPosition: Int) { private val movedItems = linkedSetOf<ReplaceRule>()
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
if (movedItems.isNotEmpty()) {
callBack.update(*movedItems.toTypedArray())
movedItems.clear()
}
} }
interface CallBack { interface CallBack {

@ -56,7 +56,7 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application
fun upOrder() { fun upOrder() {
execute { execute {
val rules = App.db.replaceRuleDao().all val rules = App.db.replaceRuleDao().all
for ((index: Int, rule: ReplaceRule) in rules.withIndex()) { for ((index, rule) in rules.withIndex()) {
rule.order = index + 1 rule.order = index + 1
} }
App.db.replaceRuleDao().update(*rules.toTypedArray()) App.db.replaceRuleDao().update(*rules.toTypedArray())

@ -3,6 +3,7 @@ package io.legado.app.ui.rss.source.manage
import android.content.Context import android.content.Context
import android.view.Menu import android.view.Menu
import android.widget.PopupMenu import android.widget.PopupMenu
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.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.base.adapter.SimpleRecyclerAdapter
@ -11,6 +12,7 @@ import io.legado.app.help.ItemTouchCallback
import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.backgroundColor
import kotlinx.android.synthetic.main.item_rss_source.view.* import kotlinx.android.synthetic.main.item_rss_source.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
import java.util.*
class RssSourceAdapter(context: Context, val callBack: CallBack) : class RssSourceAdapter(context: Context, val callBack: CallBack) :
SimpleRecyclerAdapter<RssSource>(context, R.layout.item_rss_source), SimpleRecyclerAdapter<RssSource>(context, R.layout.item_rss_source),
@ -92,11 +94,6 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) :
} }
} }
override fun onSwiped(adapterPosition: Int) {
}
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { override fun onMove(srcPosition: Int, targetPosition: Int): Boolean {
val srcItem = getItem(srcPosition) val srcItem = getItem(srcPosition)
val targetItem = getItem(targetPosition) val targetItem = getItem(targetPosition)
@ -107,12 +104,24 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) :
val srcOrder = srcItem.customOrder val srcOrder = srcItem.customOrder
srcItem.customOrder = targetItem.customOrder srcItem.customOrder = targetItem.customOrder
targetItem.customOrder = srcOrder targetItem.customOrder = srcOrder
callBack.update(srcItem, targetItem) movedItems.add(srcItem)
movedItems.add(targetItem)
} }
} }
Collections.swap(getItems(), srcPosition, targetPosition)
notifyItemMoved(srcPosition, targetPosition)
return true return true
} }
private val movedItems = hashSetOf<RssSource>()
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
if (movedItems.isNotEmpty()) {
callBack.update(*movedItems.toTypedArray())
movedItems.clear()
}
}
interface CallBack { interface CallBack {
fun del(source: RssSource) fun del(source: RssSource)
fun edit(source: RssSource) fun edit(source: RssSource)

Loading…
Cancel
Save