From 4ebd12b63182542cd04ea7befb2423544f981b63 Mon Sep 17 00:00:00 2001 From: Jason Yao Date: Mon, 2 Aug 2021 00:35:58 -0400 Subject: [PATCH] top source and bottom source --- .../io/legado/app/data/dao/SearchBookDao.kt | 10 +++-- .../book/changesource/ChangeSourceAdapter.kt | 13 ++++++ .../book/changesource/ChangeSourceDialog.kt | 29 ++++++++++-- .../changesource/ChangeSourceViewModel.kt | 45 +++++++++++++++++++ app/src/main/res/menu/change_source_item.xml | 12 +++++ 5 files changed, 102 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt b/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt index 8f53d0170..fdaad66bf 100644 --- a/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt @@ -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 @@ -62,4 +59,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) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceAdapter.kt index b5298d4b7..d34f7a441 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceAdapter.kt @@ -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()) + } } 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) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt index e83118039..a6980e049 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt @@ -194,9 +194,7 @@ class ChangeSourceDialog : BaseDialogFragment(), } override fun changeTo(searchBook: SearchBook) { - val book = searchBook.toBook() - book.upInfoFromOld(callBack?.oldBook) - callBack?.changeTo(book) + changeSource(searchBook) dismissAllowingStateLoss() } @@ -207,6 +205,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) + } + /** * 更新分组菜单 */ diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt index 6197725bd..ed14c5e7d 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt @@ -240,4 +240,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 } + } + } \ No newline at end of file diff --git a/app/src/main/res/menu/change_source_item.xml b/app/src/main/res/menu/change_source_item.xml index 49c724fee..2ba189ad5 100644 --- a/app/src/main/res/menu/change_source_item.xml +++ b/app/src/main/res/menu/change_source_item.xml @@ -1,8 +1,20 @@ + + + + + + \ No newline at end of file