From 2489c96b6bbaf910810ca78a490a3226d7feb205 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Apr 2022 10:20:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/dao/BookSourceDao.kt | 3 +++ .../io/legado/app/help/config/AppConfig.kt | 6 +++++ .../changesource/ChangeBookSourceDialog.kt | 10 ++++---- .../changesource/ChangeBookSourceViewModel.kt | 23 +++++++++++++------ .../changesource/ChangeChapterSourceDialog.kt | 10 ++++---- 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt index 1399eee93..521d79b51 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt @@ -85,6 +85,9 @@ interface BookSourceDao { @Query("select * from book_sources where enabled = 1 and bookSourceGroup like '%' || :group || '%'") fun getEnabledByGroup(group: String): List + @Query("select * from book_sources where enabled = 1 and bookSourceType = :type") + fun getEnabledByType(type: Int): List + @get:Query("select * from book_sources where trim(bookUrlPattern) <> '' order by enabled desc, customOrder") val hasBookUrlPattern: List diff --git a/app/src/main/java/io/legado/app/help/config/AppConfig.kt b/app/src/main/java/io/legado/app/help/config/AppConfig.kt index 25525406f..a851ce134 100644 --- a/app/src/main/java/io/legado/app/help/config/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/config/AppConfig.kt @@ -301,6 +301,12 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { val doublePageHorizontal: Boolean get() = appCtx.getPrefBoolean(PreferKey.doublePageHorizontal, true) + var searchGroup: String + get() = appCtx.getPrefString("searchGroup") ?: "" + set(value) { + appCtx.putPrefString("searchGroup", value) + } + private fun getPrefUserAgent(): String { val ua = appCtx.getPrefString(PreferKey.userAgent) if (ua.isNullOrBlank()) { diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt index 147dcc954..d7bb6354b 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt @@ -60,14 +60,14 @@ class ChangeBookSourceDialog() : BaseDialogFragment(R.layout.dialog_book_change_ } private val searchFinishCallback: (isEmpty: Boolean) -> Unit = { if (it) { - val searchGroup = getPrefString("searchGroup") - if (!searchGroup.isNullOrEmpty()) { + val searchGroup = AppConfig.searchGroup + if (searchGroup.isNotEmpty()) { launch { alert("搜索结果为空") { setMessage("${searchGroup}分组搜索结果为空,是否切换到全部分组") cancelButton() okButton { - putPrefString("searchGroup", "") + AppConfig.searchGroup = "" viewModel.startSearch() } } @@ -221,9 +221,9 @@ class ChangeBookSourceDialog() : BaseDialogFragment(R.layout.dialog_book_change_ if (!item.isChecked) { item.isChecked = true if (item.title.toString() == getString(R.string.all_source)) { - putPrefString("searchGroup", "") + AppConfig.searchGroup = "" } else { - putPrefString("searchGroup", item.title.toString()) + AppConfig.searchGroup = item.title.toString() } viewModel.startOrStopSearch() viewModel.refresh() diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt index 4cb536198..fdbc7e161 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt @@ -20,7 +20,6 @@ import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.webBook.WebBook import io.legado.app.utils.getPrefBoolean -import io.legado.app.utils.getPrefString import io.legado.app.utils.toastOnUi import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers.IO @@ -31,7 +30,6 @@ import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map import kotlinx.coroutines.withContext -import splitties.init.appCtx import java.util.* import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.Executors @@ -50,7 +48,6 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a private var bookSourceList = arrayListOf() private val searchBooks = Collections.synchronizedList(arrayListOf()) private val tocMap = ConcurrentHashMap>() - private val searchGroup get() = appCtx.getPrefString("searchGroup") ?: "" private var searchCallback: SourceCallback? = null val searchDataFlow = callbackFlow { @@ -129,11 +126,13 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a appDb.searchBookDao.clear(name, author) searchBooks.clear() bookSourceList.clear() + val searchGroup = AppConfig.searchGroup if (searchGroup.isBlank()) { bookSourceList.addAll(appDb.bookSourceDao.allEnabled) } else { val sources = appDb.bookSourceDao.getEnabledByGroup(searchGroup) if (sources.isEmpty()) { + AppConfig.searchGroup = "" bookSourceList.addAll(appDb.bookSourceDao.allEnabled) } else { bookSourceList.addAll(sources) @@ -224,15 +223,25 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a private fun getDbSearchBooks(): List { return if (screenKey.isEmpty()) { if (AppConfig.changeSourceCheckAuthor) { - appDb.searchBookDao.getChangeSourceSearch(name, author, searchGroup) + appDb.searchBookDao.getChangeSourceSearch(name, author, AppConfig.searchGroup) } else { - appDb.searchBookDao.getChangeSourceSearch(name, "", searchGroup) + appDb.searchBookDao.getChangeSourceSearch(name, "", AppConfig.searchGroup) } } else { if (AppConfig.changeSourceCheckAuthor) { - appDb.searchBookDao.getChangeSourceSearch(name, author, screenKey, searchGroup) + appDb.searchBookDao.getChangeSourceSearch( + name, + author, + screenKey, + AppConfig.searchGroup + ) } else { - appDb.searchBookDao.getChangeSourceSearch(name, "", screenKey, searchGroup) + appDb.searchBookDao.getChangeSourceSearch( + name, + "", + screenKey, + AppConfig.searchGroup + ) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceDialog.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceDialog.kt index f2de2c707..4ddf26683 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceDialog.kt @@ -75,14 +75,14 @@ class ChangeChapterSourceDialog() : BaseDialogFragment(R.layout.dialog_chapter_c private var searchBook: SearchBook? = null private val searchFinishCallback: (isEmpty: Boolean) -> Unit = { if (it) { - val searchGroup = getPrefString("searchGroup") - if (!searchGroup.isNullOrEmpty()) { + val searchGroup = AppConfig.searchGroup + if (searchGroup.isNotEmpty()) { launch { alert("搜索结果为空") { setMessage("${searchGroup}分组搜索结果为空,是否切换到全部分组") cancelButton() okButton { - putPrefString("searchGroup", "") + AppConfig.searchGroup = "" viewModel.startSearch() } } @@ -245,9 +245,9 @@ class ChangeChapterSourceDialog() : BaseDialogFragment(R.layout.dialog_chapter_c if (!item.isChecked) { item.isChecked = true if (item.title.toString() == getString(R.string.all_source)) { - putPrefString("searchGroup", "") + AppConfig.searchGroup = "" } else { - putPrefString("searchGroup", item.title.toString()) + AppConfig.searchGroup = item.title.toString() } viewModel.startOrStopSearch() viewModel.refresh()