Squashed commit of the following:

commit 4ebd12b631
Author: Jason Yao <h11129@gmail.com>
Date:   Mon Aug 2 00:35:58 2021 -0400

    top source and bottom source
pull/1251/head
Jason Yao 3 years ago
parent 732138522c
commit 72dd5b2f09
  1. 10
      app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt
  2. 13
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceAdapter.kt
  3. 29
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt
  4. 45
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt
  5. 12
      app/src/main/res/menu/change_source_item.xml

@ -1,9 +1,6 @@
package io.legado.app.data.dao
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.*
import io.legado.app.data.entities.SearchBook
@Dao
@ -65,4 +62,9 @@ interface SearchBookDao {
@Query("delete from searchBooks where time < :time")
fun clearExpired(time: Long)
@Update
fun update(vararg searchBook: SearchBook)
@Delete
fun delete(vararg searchBook: SearchBook)
}

@ -88,6 +88,16 @@ class ChangeSourceAdapter(
R.id.menu_disable_book_source -> {
callBack.disableSource(searchBook)
}
R.id.menu_top_source -> {
callBack.topSource(searchBook)
}
R.id.menu_bottom_source -> {
callBack.bottomSource(searchBook)
}
R.id.menu_delete_book_source -> {
callBack.deleteSource(searchBook)
updateItems(0, itemCount, listOf<Int>())
}
}
true
}
@ -98,5 +108,8 @@ class ChangeSourceAdapter(
val bookUrl: String?
fun changeTo(searchBook: SearchBook)
fun disableSource(searchBook: SearchBook)
fun topSource(searchBook: SearchBook)
fun bottomSource(searchBook: SearchBook)
fun deleteSource(searchBook: SearchBook)
}
}

@ -198,9 +198,7 @@ class ChangeSourceDialog : BaseDialogFragment(),
}
override fun changeTo(searchBook: SearchBook) {
val book = searchBook.toBook()
book.upInfoFromOld(callBack?.oldBook)
callBack?.changeTo(book)
changeSource(searchBook)
dismissAllowingStateLoss()
}
@ -211,6 +209,31 @@ class ChangeSourceDialog : BaseDialogFragment(),
viewModel.disableSource(searchBook)
}
override fun topSource(searchBook: SearchBook) {
viewModel.topSource(searchBook)
}
override fun bottomSource(searchBook: SearchBook) {
viewModel.bottomSource(searchBook)
}
override fun deleteSource(searchBook: SearchBook) {
if (bookUrl == searchBook.bookUrl) {
viewModel.firstSourceOrNull(searchBook)?.let {
changeSource(it)
}
}
viewModel.del(searchBook)
}
private fun changeSource(searchBook: SearchBook) {
val book = searchBook.toBook()
book.upInfoFromOld(callBack?.oldBook)
callBack?.changeTo(book)
searchBook.time = System.currentTimeMillis()
viewModel.updateSource(searchBook)
}
/**
* 更新分组菜单
*/

@ -242,4 +242,49 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
}
}
fun topSource(searchBook: SearchBook) {
execute {
appDb.bookSourceDao.getBookSource(searchBook.origin)?.let { source ->
val minOrder = appDb.bookSourceDao.minOrder - 1
source.customOrder = minOrder
searchBook.originOrder = source.customOrder
appDb.bookSourceDao.update(source)
updateSource(searchBook)
}
upAdapter()
}
}
fun bottomSource(searchBook: SearchBook) {
execute {
appDb.bookSourceDao.getBookSource(searchBook.origin)?.let { source ->
val maxOrder = appDb.bookSourceDao.maxOrder + 1
source.customOrder = maxOrder
searchBook.originOrder = source.customOrder
appDb.bookSourceDao.update(source)
updateSource(searchBook)
}
upAdapter()
}
}
fun updateSource(searchBook: SearchBook) {
appDb.searchBookDao.update(searchBook)
}
fun del(searchBook: SearchBook) {
execute {
appDb.bookSourceDao.getBookSource(searchBook.origin)?.let { source ->
appDb.bookSourceDao.delete(source)
appDb.searchBookDao.delete(searchBook)
}
}
searchBooks.remove(searchBook)
upAdapter()
}
fun firstSourceOrNull(searchBook: SearchBook): SearchBook? {
return searchBooks.firstOrNull { it.bookUrl != searchBook.bookUrl }
}
}

@ -1,8 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_top_source"
android:title="@string/to_top" />
<item
android:id="@+id/menu_bottom_source"
android:title="@string/to_bottom" />
<item
android:id="@+id/menu_disable_book_source"
android:title="@string/disable_book_source" />
<item
android:id="@+id/menu_delete_book_source"
android:title="@string/delete" />
</menu>
Loading…
Cancel
Save