diff --git a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt index b6204e90b..ea61cd1d3 100644 --- a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt @@ -14,6 +14,7 @@ import io.legado.app.data.entities.BookGroup import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.okButton import io.legado.app.lib.theme.ATH +import io.legado.app.ui.book.group.GroupSelectDialog import io.legado.app.utils.applyTint import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_arrange_book.* @@ -22,10 +23,11 @@ import org.jetbrains.anko.toast class ArrangeBookActivity : VMBaseActivity(R.layout.activity_arrange_book), - ArrangeBookAdapter.CallBack { + ArrangeBookAdapter.CallBack, GroupSelectDialog.CallBack { override val viewModel: ArrangeBookViewModel get() = getViewModel(ArrangeBookViewModel::class.java) override val groupList: ArrayList = arrayListOf() + private val groupRequestCode = 22 private lateinit var adapter: ArrangeBookAdapter private var groupLiveData: LiveData>? = null private var booksLiveData: LiveData>? = null @@ -71,7 +73,7 @@ class ArrangeBookActivity : VMBaseActivity(R.layout.activi toast(R.string.non_select) return@onClick } - selectGroup() + selectGroup(groupRequestCode) } } @@ -91,8 +93,25 @@ class ArrangeBookActivity : VMBaseActivity(R.layout.activi }) } - override fun selectGroup() { + override fun selectGroup(requestCode: Int) { + GroupSelectDialog.show(supportFragmentManager, requestCode) + } + override fun upGroup(requestCode: Int, group: BookGroup) { + when (requestCode) { + groupRequestCode -> { + val books = arrayListOf() + adapter.selectedBooks.forEach { + books.add(it.copy(group = group.groupId)) + } + viewModel.updateBook(*books.toTypedArray()) + } + adapter.groupRequestCode -> { + adapter.actionItem?.let { + viewModel.updateBook(it.copy(group = group.groupId)) + } + } + } } override fun upSelectCount() { diff --git a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt index dab74dc4b..21a3516eb 100644 --- a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt @@ -12,8 +12,9 @@ import org.jetbrains.anko.sdk27.listeners.onClick class ArrangeBookAdapter(context: Context, val callBack: CallBack) : SimpleRecyclerAdapter(context, R.layout.item_arrange_book) { - + val groupRequestCode = 12 val selectedBooks: HashSet = hashSetOf() + var actionItem: Book? = null fun isSelectAll(): Boolean { return if (selectedBooks.isEmpty()) { @@ -64,7 +65,8 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) : callBack.deleteBook(item) } tv_group.onClick { - callBack.selectGroup() + actionItem = item + callBack.selectGroup(groupRequestCode) } } } @@ -82,6 +84,6 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) : val groupList: List fun upSelectCount() fun deleteBook(book: Book) - fun selectGroup() + fun selectGroup(requestCode: Int) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookViewModel.kt index 173678b15..4f4972de9 100644 --- a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookViewModel.kt @@ -8,6 +8,11 @@ import io.legado.app.data.entities.Book class ArrangeBookViewModel(application: Application) : BaseViewModel(application) { + fun updateBook(vararg book: Book) { + execute { + App.db.bookDao().update(*book) + } + } fun deleteBook(vararg book: Book) { execute {