feat: 优化代码

pull/173/head
kunfei 5 years ago
parent 53b596a3e8
commit 26ff281fe9
  1. 3
      app/src/main/java/io/legado/app/data/dao/BookDao.kt
  2. 7
      app/src/main/java/io/legado/app/help/ItemTouchCallback.kt
  3. 1
      app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt
  4. 17
      app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt
  5. 2
      app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt
  6. 2
      app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt
  7. 7
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  8. 1
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  9. 4
      app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt
  10. 2
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt
  11. 1
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt
  12. 2
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt
  13. 2
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt

@ -57,6 +57,9 @@ interface BookDao {
@get:Query("SELECT COUNT(*) FROM books") @get:Query("SELECT COUNT(*) FROM books")
val allBookCount: Int val allBookCount: Int
@get:Query("select max(`order`) from books")
val maxOrder: Int
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg book: Book) fun insert(vararg book: Book)

@ -106,7 +106,7 @@ class ItemTouchCallback : ItemTouchHelper.Callback() {
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
super.clearView(recyclerView, viewHolder) super.clearView(recyclerView, viewHolder)
onItemTouchCallbackListener?.clearView(recyclerView, viewHolder) onItemTouchCallbackListener?.onClearView(recyclerView, viewHolder)
} }
interface OnItemTouchCallbackListener { interface OnItemTouchCallbackListener {
@ -131,7 +131,10 @@ class ItemTouchCallback : ItemTouchHelper.Callback() {
return true return true
} }
fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { /**
* 手指松开
*/
fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
} }

@ -91,6 +91,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
fun changeTo(book1: Book) { fun changeTo(book1: Book) {
execute { execute {
AudioPlay.book?.let { AudioPlay.book?.let {
book1.order = it.order
App.db.bookDao().delete(it) App.db.bookDao().delete(it)
} }
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {

@ -2,6 +2,7 @@ package io.legado.app.ui.book.arrange
import android.content.Context import android.content.Context
import android.view.View import android.view.View
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
@ -10,6 +11,7 @@ import io.legado.app.data.entities.BookGroup
import io.legado.app.help.ItemTouchCallback import io.legado.app.help.ItemTouchCallback
import kotlinx.android.synthetic.main.item_arrange_book.view.* import kotlinx.android.synthetic.main.item_arrange_book.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
import java.util.*
class ArrangeBookAdapter(context: Context, val callBack: CallBack) : class ArrangeBookAdapter(context: Context, val callBack: CallBack) :
SimpleRecyclerAdapter<Book>(context, R.layout.item_arrange_book), SimpleRecyclerAdapter<Book>(context, R.layout.item_arrange_book),
@ -120,6 +122,21 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) :
return groupNames.joinToString(",") 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 { interface CallBack {
val groupList: List<BookGroup> val groupList: List<BookGroup>
fun upSelectCount() fun upSelectCount()

@ -217,7 +217,7 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
return true return true
} }
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
if (isMoved) { if (isMoved) {
for ((index, item) in getItems().withIndex()) { for ((index, item) in getItems().withIndex()) {
item.order = index + 1 item.order = index + 1

@ -203,7 +203,7 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
return true return true
} }
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
if (isMoved) { if (isMoved) {
for ((index, item) in getItems().withIndex()) { for ((index, item) in getItems().withIndex()) {
item.order = index + 1 item.order = index + 1

@ -133,6 +133,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
execute { execute {
if (inBookshelf) { if (inBookshelf) {
bookData.value?.let { bookData.value?.let {
book.order = it.order
App.db.bookDao().delete(it) App.db.bookDao().delete(it)
} }
App.db.bookDao().insert(book) App.db.bookDao().insert(book)
@ -164,6 +165,9 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
fun saveBook(success: (() -> Unit)? = null) { fun saveBook(success: (() -> Unit)? = null) {
execute { execute {
bookData.value?.let { book -> bookData.value?.let { book ->
if (book.order == 0) {
book.order = App.db.bookDao().maxOrder + 1
}
App.db.bookDao().insert(book) App.db.bookDao().insert(book)
} }
}.onSuccess { }.onSuccess {
@ -184,6 +188,9 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
fun addToBookshelf(success: (() -> Unit)?) { fun addToBookshelf(success: (() -> Unit)?) {
execute { execute {
bookData.value?.let { book -> bookData.value?.let { book ->
if (book.order == 0) {
book.order = App.db.bookDao().maxOrder + 1
}
App.db.bookDao().insert(book) App.db.bookDao().insert(book)
} }
chapterListData.value?.let { chapterListData.value?.let {

@ -136,6 +136,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
execute { execute {
ReadBook.upMsg(null) ReadBook.upMsg(null)
ReadBook.book?.let { ReadBook.book?.let {
book1.order = it.order
App.db.bookDao().delete(it) App.db.bookDao().delete(it)
} }
ReadBook.prevTextChapter = null ReadBook.prevTextChapter = null

@ -252,8 +252,8 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
return super.onMove(srcPosition, targetPosition) return super.onMove(srcPosition, targetPosition)
} }
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
super.clearView(recyclerView, viewHolder) super.onClearView(recyclerView, viewHolder)
if (isMoved) { if (isMoved) {
for ((index, item) in getItems().withIndex()) { for ((index, item) in getItems().withIndex()) {
item.serialNumber = index + 1 item.serialNumber = index + 1

@ -185,7 +185,7 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
private val movedItems = hashSetOf<BookSource>() private val movedItems = hashSetOf<BookSource>()
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
if (movedItems.isNotEmpty()) { if (movedItems.isNotEmpty()) {
callBack.update(*movedItems.toTypedArray()) callBack.update(*movedItems.toTypedArray())
movedItems.clear() movedItems.clear()

@ -43,6 +43,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
) )
WebBook(bookSource).getBookInfo(book, this) WebBook(bookSource).getBookInfo(book, this)
.onSuccess(IO) { .onSuccess(IO) {
it.order = App.db.bookDao().maxOrder + 1
App.db.bookDao().insert(it) App.db.bookDao().insert(it)
successCount++ successCount++
}.onError { }.onError {

@ -148,7 +148,7 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
private val movedItems = linkedSetOf<ReplaceRule>() private val movedItems = linkedSetOf<ReplaceRule>()
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
if (movedItems.isNotEmpty()) { if (movedItems.isNotEmpty()) {
callBack.update(*movedItems.toTypedArray()) callBack.update(*movedItems.toTypedArray())
movedItems.clear() movedItems.clear()

@ -139,7 +139,7 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) :
private val movedItems = hashSetOf<RssSource>() private val movedItems = hashSetOf<RssSource>()
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
if (movedItems.isNotEmpty()) { if (movedItems.isNotEmpty()) {
callBack.update(*movedItems.toTypedArray()) callBack.update(*movedItems.toTypedArray())
movedItems.clear() movedItems.clear()

Loading…
Cancel
Save