From 26ff281fe9b80848f455eafd45bdd135cebc7d5e Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 22 Mar 2020 17:30:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/data/dao/BookDao.kt | 3 +++ .../io/legado/app/help/ItemTouchCallback.kt | 7 +++++-- .../legado/app/ui/audio/AudioPlayViewModel.kt | 1 + .../app/ui/book/arrange/ArrangeBookAdapter.kt | 17 +++++++++++++++++ .../app/ui/book/group/GroupManageDialog.kt | 2 +- .../app/ui/book/group/GroupSelectDialog.kt | 2 +- .../app/ui/book/info/BookInfoViewModel.kt | 7 +++++++ .../app/ui/book/read/ReadBookViewModel.kt | 1 + .../app/ui/book/read/config/TocRegexDialog.kt | 4 ++-- .../ui/book/source/manage/BookSourceAdapter.kt | 2 +- .../app/ui/main/bookshelf/BookshelfViewModel.kt | 1 + .../app/ui/replacerule/ReplaceRuleAdapter.kt | 2 +- .../ui/rss/source/manage/RssSourceAdapter.kt | 2 +- 13 files changed, 42 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/BookDao.kt b/app/src/main/java/io/legado/app/data/dao/BookDao.kt index b323a2711..915a9dbb9 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookDao.kt @@ -57,6 +57,9 @@ interface BookDao { @get:Query("SELECT COUNT(*) FROM books") val allBookCount: Int + @get:Query("select max(`order`) from books") + val maxOrder: Int + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg book: Book) diff --git a/app/src/main/java/io/legado/app/help/ItemTouchCallback.kt b/app/src/main/java/io/legado/app/help/ItemTouchCallback.kt index 99ba74ef1..9c1a0bbf2 100644 --- a/app/src/main/java/io/legado/app/help/ItemTouchCallback.kt +++ b/app/src/main/java/io/legado/app/help/ItemTouchCallback.kt @@ -106,7 +106,7 @@ class ItemTouchCallback : ItemTouchHelper.Callback() { override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { super.clearView(recyclerView, viewHolder) - onItemTouchCallbackListener?.clearView(recyclerView, viewHolder) + onItemTouchCallbackListener?.onClearView(recyclerView, viewHolder) } interface OnItemTouchCallbackListener { @@ -131,7 +131,10 @@ class ItemTouchCallback : ItemTouchHelper.Callback() { return true } - fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + /** + * 手指松开 + */ + fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { } diff --git a/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt b/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt index 892a99f41..8f0cd9fa4 100644 --- a/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt @@ -91,6 +91,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) fun changeTo(book1: Book) { execute { AudioPlay.book?.let { + book1.order = it.order App.db.bookDao().delete(it) } withContext(Dispatchers.Main) { 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 abc72b905..d585e8c62 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 @@ -2,6 +2,7 @@ package io.legado.app.ui.book.arrange import android.content.Context import android.view.View +import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter @@ -10,6 +11,7 @@ import io.legado.app.data.entities.BookGroup import io.legado.app.help.ItemTouchCallback import kotlinx.android.synthetic.main.item_arrange_book.view.* import org.jetbrains.anko.sdk27.listeners.onClick +import java.util.* class ArrangeBookAdapter(context: Context, val callBack: CallBack) : SimpleRecyclerAdapter(context, R.layout.item_arrange_book), @@ -120,6 +122,21 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) : return groupNames.joinToString(",") } + private var isMoved = false + + override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { + Collections.swap(getItems(), srcPosition, targetPosition) + notifyItemMoved(srcPosition, targetPosition) + isMoved = true + return true + } + + override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + if (isMoved) { + + } + } + interface CallBack { val groupList: List fun upSelectCount() diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt index 8be9ed34f..60010f46d 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt @@ -217,7 +217,7 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { return true } - override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { if (isMoved) { for ((index, item) in getItems().withIndex()) { item.order = index + 1 diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt index b290bc5df..29df624e7 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt @@ -203,7 +203,7 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { return true } - override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { if (isMoved) { for ((index, item) in getItems().withIndex()) { item.order = index + 1 diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt index a8d8f59ef..7dc4e16dd 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt @@ -133,6 +133,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { execute { if (inBookshelf) { bookData.value?.let { + book.order = it.order App.db.bookDao().delete(it) } App.db.bookDao().insert(book) @@ -164,6 +165,9 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { fun saveBook(success: (() -> Unit)? = null) { execute { bookData.value?.let { book -> + if (book.order == 0) { + book.order = App.db.bookDao().maxOrder + 1 + } App.db.bookDao().insert(book) } }.onSuccess { @@ -184,6 +188,9 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { fun addToBookshelf(success: (() -> Unit)?) { execute { bookData.value?.let { book -> + if (book.order == 0) { + book.order = App.db.bookDao().maxOrder + 1 + } App.db.bookDao().insert(book) } chapterListData.value?.let { diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 231f83e11..987555a9b 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -136,6 +136,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { execute { ReadBook.upMsg(null) ReadBook.book?.let { + book1.order = it.order App.db.bookDao().delete(it) } ReadBook.prevTextChapter = null diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt index aa816f93f..d4a6711bc 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt @@ -252,8 +252,8 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { return super.onMove(srcPosition, targetPosition) } - override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { - super.clearView(recyclerView, viewHolder) + override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + super.onClearView(recyclerView, viewHolder) if (isMoved) { for ((index, item) in getItems().withIndex()) { item.serialNumber = index + 1 diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt index 5d5872638..eb37cb868 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt @@ -185,7 +185,7 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : private val movedItems = hashSetOf() - override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { if (movedItems.isNotEmpty()) { callBack.update(*movedItems.toTypedArray()) movedItems.clear() 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 2415db1be..dda342015 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 @@ -43,6 +43,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) ) WebBook(bookSource).getBookInfo(book, this) .onSuccess(IO) { + it.order = App.db.bookDao().maxOrder + 1 App.db.bookDao().insert(it) successCount++ }.onError { diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt index dd51d31d0..4dd09980c 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt @@ -148,7 +148,7 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) : private val movedItems = linkedSetOf() - override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { if (movedItems.isNotEmpty()) { callBack.update(*movedItems.toTypedArray()) movedItems.clear() diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt index 165d9bd0c..72de1e250 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt @@ -139,7 +139,7 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) : private val movedItems = hashSetOf() - override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { if (movedItems.isNotEmpty()) { callBack.update(*movedItems.toTypedArray()) movedItems.clear()