pull/2425/head
kunfei 2 years ago
parent 91111c9b76
commit 58c096d8dc
  1. 15
      app/src/main/java/io/legado/app/model/webBook/SearchModel.kt
  2. 40
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  3. 14
      app/src/main/java/io/legado/app/ui/book/search/SearchScope.kt
  4. 7
      app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt
  5. 13
      app/src/main/res/menu/book_search.xml

@ -7,6 +7,7 @@ import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.ui.book.search.SearchScope
import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefBoolean
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExecutorCoroutineDispatcher import kotlinx.coroutines.ExecutorCoroutineDispatcher
@ -50,21 +51,10 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB
initSearchPool() initSearchPool()
mSearchId = searchId mSearchId = searchId
searchPage = 1 searchPage = 1
val searchGroup = AppConfig.searchGroup
bookSourceList.clear() bookSourceList.clear()
searchBooks.clear() searchBooks.clear()
callBack.onSearchSuccess(searchBooks) callBack.onSearchSuccess(searchBooks)
if (searchGroup.isBlank()) { bookSourceList.addAll(callBack.getSearchScope().getBookSources())
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)
}
}
} else { } else {
searchPage++ searchPage++
} }
@ -203,6 +193,7 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB
} }
interface CallBack { interface CallBack {
fun getSearchScope(): SearchScope
fun onSearchStart() fun onSearchStart()
fun onSearchSuccess(searchBooks: ArrayList<SearchBook>) fun onSearchSuccess(searchBooks: ArrayList<SearchBook>)
fun onSearchFinish(isEmpty: Boolean) fun onSearchFinish(isEmpty: Boolean)

@ -64,9 +64,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
} }
private var historyFlowJob: Job? = null private var historyFlowJob: Job? = null
private var booksFlowJob: Job? = null private var booksFlowJob: Job? = null
private var menu: Menu? = null
private var precisionSearchMenuItem: MenuItem? = null private var precisionSearchMenuItem: MenuItem? = null
private var groups = linkedSetOf<String>()
private var isManualStopSearch = false private var isManualStopSearch = false
private val searchFinishCallback: (isEmpty: Boolean) -> Unit = searchFinish@{ isEmpty -> private val searchFinishCallback: (isEmpty: Boolean) -> Unit = searchFinish@{ isEmpty ->
val searchGroup = AppConfig.searchGroup val searchGroup = AppConfig.searchGroup
@ -114,8 +112,6 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
menuInflater.inflate(R.menu.book_search, menu) menuInflater.inflate(R.menu.book_search, menu)
precisionSearchMenuItem = menu.findItem(R.id.menu_precision_search) precisionSearchMenuItem = menu.findItem(R.id.menu_precision_search)
precisionSearchMenuItem?.isChecked = getPrefBoolean(PreferKey.precisionSearch) precisionSearchMenuItem?.isChecked = getPrefBoolean(PreferKey.precisionSearch)
this.menu = menu
upGroupMenu()
return super.onCompatCreateOptionsMenu(menu) return super.onCompatCreateOptionsMenu(menu)
} }
@ -249,19 +245,12 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
delay(1000) delay(1000)
} }
} }
launch {
appDb.bookSourceDao.flowEnabledGroups().conflate().collect {
groups.clear()
groups.addAll(it)
upGroupMenu()
}
}
} }
private fun receiptIntent(intent: Intent? = null) { private fun receiptIntent(intent: Intent? = null) {
val searchScope = intent?.getStringExtra("searchScope") val searchScope = intent?.getStringExtra("searchScope")
searchScope?.let { searchScope?.let {
viewModel.searchScope.scope = searchScope viewModel.searchScope.update(searchScope)
searchScopeAdapter.setItems(viewModel.searchScope.getShowNames()) searchScopeAdapter.setItems(viewModel.searchScope.getShowNames())
} }
val key = intent?.getStringExtra("key") val key = intent?.getStringExtra("key")
@ -300,30 +289,6 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
} }
} }
/**
* 更新分组菜单
*/
private fun upGroupMenu() = menu?.let { menu ->
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<ActivityBookSearchBinding, SearchViewModel
override fun onSearchScopeOk(searchScope: SearchScope) { override fun onSearchScopeOk(searchScope: SearchScope) {
viewModel.searchScope = searchScope searchScope.save()
viewModel.searchScope.update(searchScope.toString())
searchScopeAdapter.setItems(searchScope.getShowNames()) searchScopeAdapter.setItems(searchScope.getShowNames())
} }

@ -9,7 +9,7 @@ import io.legado.app.utils.splitNotBlank
* 搜索范围 * 搜索范围
*/ */
@Suppress("unused") @Suppress("unused")
data class SearchScope(var scope: String) { data class SearchScope(private var scope: String) {
constructor(groups: List<String>) : this(groups.joinToString(",")) constructor(groups: List<String>) : this(groups.joinToString(","))
@ -19,6 +19,18 @@ data class SearchScope(var scope: String) {
return scope return scope
} }
fun update(scope: String) {
this.scope = scope
}
fun update(groups: List<String>) {
scope = groups.joinToString(",")
}
fun update(source: BookSource) {
scope = "${source.bookSourceName}::${source.bookSourceUrl}"
}
/** /**
* 搜索范围显示 * 搜索范围显示
*/ */

@ -15,7 +15,13 @@ import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
class SearchViewModel(application: Application) : BaseViewModel(application) { class SearchViewModel(application: Application) : BaseViewModel(application) {
val searchScope: SearchScope = SearchScope(AppConfig.searchScope)
private val searchModel = SearchModel(viewModelScope, object : SearchModel.CallBack { private val searchModel = SearchModel(viewModelScope, object : SearchModel.CallBack {
override fun getSearchScope(): SearchScope {
return searchScope
}
override fun onSearchStart() { override fun onSearchStart() {
isSearchLiveData.postValue(true) isSearchLiveData.postValue(true)
} }
@ -35,7 +41,6 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
}) })
var searchFinishCallback: ((isEmpty: Boolean) -> Unit)? = null var searchFinishCallback: ((isEmpty: Boolean) -> Unit)? = null
var isSearchLiveData = MutableLiveData<Boolean>() var isSearchLiveData = MutableLiveData<Boolean>()
var searchScope: SearchScope = SearchScope(AppConfig.searchScope)
var searchKey: String = "" var searchKey: String = ""
private var searchID = 0L private var searchID = 0L
private var searchFlowCallBack: ((searchBooks: ArrayList<SearchBook>) -> Unit)? = null private var searchFlowCallBack: ((searchBooks: ArrayList<SearchBook>) -> Unit)? = null

@ -1,7 +1,13 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
tools:context=".view.activity.MainActivity"> tools:ignore="AlwaysShowAction">
<item
android:id="@+id/menu_precision_search"
android:checkable="true"
android:title="@string/precision_search"
app:showAsAction="never" />
<item <item
android:id="@+id/menu_source_manage" android:id="@+id/menu_source_manage"
@ -9,9 +15,8 @@
app:showAsAction="never" /> app:showAsAction="never" />
<item <item
android:id="@+id/menu_precision_search" android:id="@+id/menu_log"
android:title="@string/precision_search" android:title="@string/log"
android:checkable="true"
app:showAsAction="never" /> app:showAsAction="never" />
</menu> </menu>

Loading…
Cancel
Save