From 58c096d8dcd814ecd0d8bf2b48c2215a4ff90067 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 14 Oct 2022 09:37:34 +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 --- .../legado/app/model/webBook/SearchModel.kt | 15 ++----- .../app/ui/book/search/SearchActivity.kt | 40 ++----------------- .../legado/app/ui/book/search/SearchScope.kt | 14 ++++++- .../app/ui/book/search/SearchViewModel.kt | 7 +++- app/src/main/res/menu/book_search.xml | 13 ++++-- 5 files changed, 34 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/webBook/SearchModel.kt b/app/src/main/java/io/legado/app/model/webBook/SearchModel.kt index 33f79d462..768e14b45 100644 --- a/app/src/main/java/io/legado/app/model/webBook/SearchModel.kt +++ b/app/src/main/java/io/legado/app/model/webBook/SearchModel.kt @@ -7,6 +7,7 @@ import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.SearchBook import io.legado.app.help.config.AppConfig import io.legado.app.help.coroutine.CompositeCoroutine +import io.legado.app.ui.book.search.SearchScope import io.legado.app.utils.getPrefBoolean import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExecutorCoroutineDispatcher @@ -50,21 +51,10 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB initSearchPool() mSearchId = searchId searchPage = 1 - val searchGroup = AppConfig.searchGroup bookSourceList.clear() searchBooks.clear() callBack.onSearchSuccess(searchBooks) - 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) - } - } + bookSourceList.addAll(callBack.getSearchScope().getBookSources()) } else { searchPage++ } @@ -203,6 +193,7 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB } interface CallBack { + fun getSearchScope(): SearchScope fun onSearchStart() fun onSearchSuccess(searchBooks: ArrayList) fun onSearchFinish(isEmpty: Boolean) diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt index db70e209b..4854a009f 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt @@ -64,9 +64,7 @@ class SearchActivity : VMBaseActivity() private var isManualStopSearch = false private val searchFinishCallback: (isEmpty: Boolean) -> Unit = searchFinish@{ isEmpty -> val searchGroup = AppConfig.searchGroup @@ -114,8 +112,6 @@ class SearchActivity : VMBaseActivity - val selectedGroup = AppConfig.searchGroup - menu.removeGroup(R.id.source_group) - val allItem = menu.add(R.id.source_group, Menu.NONE, Menu.NONE, R.string.all_source) - var hasSelectedGroup = false - groups.sortedWith { o1, o2 -> - o1.cnCompare(o2) - }.forEach { group -> - menu.add(R.id.source_group, Menu.NONE, Menu.NONE, group)?.let { - if (group == selectedGroup) { - it.isChecked = true - hasSelectedGroup = true - } - } - } - menu.setGroupCheckable(R.id.source_group, true, true) - if (!hasSelectedGroup) { - allItem.isChecked = true - } - } - /** * 更新搜索历史 */ @@ -424,7 +389,8 @@ class SearchActivity : VMBaseActivity) : this(groups.joinToString(",")) @@ -19,6 +19,18 @@ data class SearchScope(var scope: String) { return scope } + fun update(scope: String) { + this.scope = scope + } + + fun update(groups: List) { + scope = groups.joinToString(",") + } + + fun update(source: BookSource) { + scope = "${source.bookSourceName}::${source.bookSourceUrl}" + } + /** * 搜索范围显示 */ diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt index d502022b1..ad26d1ed3 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt @@ -15,7 +15,13 @@ import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.flowOn class SearchViewModel(application: Application) : BaseViewModel(application) { + val searchScope: SearchScope = SearchScope(AppConfig.searchScope) private val searchModel = SearchModel(viewModelScope, object : SearchModel.CallBack { + + override fun getSearchScope(): SearchScope { + return searchScope + } + override fun onSearchStart() { isSearchLiveData.postValue(true) } @@ -35,7 +41,6 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { }) var searchFinishCallback: ((isEmpty: Boolean) -> Unit)? = null var isSearchLiveData = MutableLiveData() - var searchScope: SearchScope = SearchScope(AppConfig.searchScope) var searchKey: String = "" private var searchID = 0L private var searchFlowCallBack: ((searchBooks: ArrayList) -> Unit)? = null diff --git a/app/src/main/res/menu/book_search.xml b/app/src/main/res/menu/book_search.xml index 0a8ce4992..b042b51db 100644 --- a/app/src/main/res/menu/book_search.xml +++ b/app/src/main/res/menu/book_search.xml @@ -1,7 +1,13 @@ + tools:ignore="AlwaysShowAction"> + +