From 72dd5b2f091e6c10e2c7d99973a46764b7bbea64 Mon Sep 17 00:00:00 2001 From: Jason Yao Date: Mon, 2 Aug 2021 00:38:23 -0400 Subject: [PATCH] Squashed commit of the following: commit 4ebd12b63182542cd04ea7befb2423544f981b63 Author: Jason Yao Date: Mon Aug 2 00:35:58 2021 -0400 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 5454fd700..0c5a6183f 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 @@ -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) } \ 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 c03c92513..acd00f18d 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 @@ -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) + } + /** * 更新分组菜单 */ 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 e7b73f7fc..9c998e1a8 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 @@ -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 } + } + } \ 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