From cca3920c76a0f01987d0815f56439e09f977c6a2 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 28 Sep 2019 22:19:58 +0800 Subject: [PATCH] up --- .../ui/main/bookshelf/BookshelfViewModel.kt | 8 +++--- .../ui/main/bookshelf/GroupManageDialog.kt | 25 ++++++++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt index f4664fa22..0499e5e5e 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt @@ -20,15 +20,15 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) } } - fun upGroup(bookGroup: BookGroup) { + fun upGroup(vararg bookGroup: BookGroup) { execute { - App.db.bookGroupDao().update(bookGroup) + App.db.bookGroupDao().update(*bookGroup) } } - fun delGroup(bookGroup: BookGroup) { + fun delGroup(vararg bookGroup: BookGroup) { execute { - App.db.bookGroupDao().delete(bookGroup) + App.db.bookGroupDao().delete(*bookGroup) } } } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt index 87d43bc1b..f6151edaf 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt @@ -13,6 +13,7 @@ import androidx.appcompat.widget.Toolbar import androidx.fragment.app.DialogFragment import androidx.lifecycle.Observer import androidx.recyclerview.widget.DividerItemDecoration +import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView 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.SimpleRecyclerAdapter 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.customView import io.legado.app.lib.dialogs.noButton @@ -75,6 +77,10 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { App.db.bookGroupDao().liveDataAll().observe(viewLifecycleOwner, Observer { adapter.setItems(it) }) + val itemTouchCallback = ItemTouchCallback() + itemTouchCallback.onItemTouchCallbackListener = adapter + itemTouchCallback.isCanDrag = true + ItemTouchHelper(itemTouchCallback).attachToRecyclerView(recycler_view) } override fun onMenuItemClick(item: MenuItem?): Boolean { @@ -136,7 +142,8 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { } private inner class GroupAdapter(context: Context) : - SimpleRecyclerAdapter(context, R.layout.item_group_manage) { + SimpleRecyclerAdapter(context, R.layout.item_group_manage), + ItemTouchCallback.OnItemTouchCallbackListener { override fun convert(holder: ItemViewHolder, item: BookGroup, payloads: MutableList) { with(holder.itemView) { @@ -145,6 +152,22 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { 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 {