pull/36/head
kunfei 5 years ago
parent 6bed8c39c2
commit cca3920c76
  1. 8
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt
  2. 25
      app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt

@ -20,15 +20,15 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
} }
} }
fun upGroup(bookGroup: BookGroup) { fun upGroup(vararg bookGroup: BookGroup) {
execute { execute {
App.db.bookGroupDao().update(bookGroup) App.db.bookGroupDao().update(*bookGroup)
} }
} }
fun delGroup(bookGroup: BookGroup) { fun delGroup(vararg bookGroup: BookGroup) {
execute { execute {
App.db.bookGroupDao().delete(bookGroup) App.db.bookGroupDao().delete(*bookGroup)
} }
} }
} }

@ -13,6 +13,7 @@ import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import io.legado.app.App import io.legado.app.App
@ -20,6 +21,7 @@ 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
import io.legado.app.data.entities.BookGroup import io.legado.app.data.entities.BookGroup
import io.legado.app.help.ItemTouchCallback
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.customView import io.legado.app.lib.dialogs.customView
import io.legado.app.lib.dialogs.noButton import io.legado.app.lib.dialogs.noButton
@ -75,6 +77,10 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
App.db.bookGroupDao().liveDataAll().observe(viewLifecycleOwner, Observer { App.db.bookGroupDao().liveDataAll().observe(viewLifecycleOwner, Observer {
adapter.setItems(it) adapter.setItems(it)
}) })
val itemTouchCallback = ItemTouchCallback()
itemTouchCallback.onItemTouchCallbackListener = adapter
itemTouchCallback.isCanDrag = true
ItemTouchHelper(itemTouchCallback).attachToRecyclerView(recycler_view)
} }
override fun onMenuItemClick(item: MenuItem?): Boolean { override fun onMenuItemClick(item: MenuItem?): Boolean {
@ -136,7 +142,8 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
} }
private inner class GroupAdapter(context: Context) : private inner class GroupAdapter(context: Context) :
SimpleRecyclerAdapter<BookGroup>(context, R.layout.item_group_manage) { SimpleRecyclerAdapter<BookGroup>(context, R.layout.item_group_manage),
ItemTouchCallback.OnItemTouchCallbackListener {
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) {
@ -145,6 +152,22 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
tv_del.onClick { viewModel.delGroup(item) } tv_del.onClick { viewModel.delGroup(item) }
} }
} }
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean {
val srcItem = getItem(srcPosition)
val targetItem = getItem(targetPosition)
if (srcItem != null && targetItem != null) {
val order = srcItem.order
srcItem.order = targetItem.order
targetItem.order = order
viewModel.upGroup(srcItem, targetItem)
}
return true
}
override fun onSwiped(adapterPosition: Int) {
}
} }
interface CallBack { interface CallBack {

Loading…
Cancel
Save