pull/2610/head
kunfei 2 years ago
parent 5723ed16b8
commit bdde8833e2
  1. 14
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  2. 16
      app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt

@ -17,7 +17,6 @@ import androidx.recyclerview.widget.RecyclerView
import com.google.android.flexbox.FlexboxLayoutManager import com.google.android.flexbox.FlexboxLayoutManager
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.AppLog
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
@ -33,9 +32,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import splitties.init.appCtx import splitties.init.appCtx
@ -291,17 +288,6 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
groups = it groups = it
} }
} }
launch {
appDb.bookDao.flowAll().conflate().map { books ->
books.map { "${it.name}-${it.author}" }
}.catch { e ->
AppLog.put("加载书架数据失败", e)
}.collect {
viewModel.bookshelf.clear()
viewModel.bookshelf.addAll(it)
viewModel.upAdapterLiveData.postValue("isInBookshelf")
}
}
} }
/** /**

@ -10,10 +10,13 @@ import io.legado.app.data.entities.SearchKeyword
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.model.webBook.SearchModel import io.legado.app.model.webBook.SearchModel
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.mapLatest
@OptIn(ExperimentalCoroutinesApi::class)
class SearchViewModel(application: Application) : BaseViewModel(application) { class SearchViewModel(application: Application) : BaseViewModel(application) {
val bookshelf = hashSetOf<String>() val bookshelf = hashSetOf<String>()
val upAdapterLiveData = MutableLiveData<String>() val upAdapterLiveData = MutableLiveData<String>()
@ -57,6 +60,19 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
} }
}.flowOn(IO) }.flowOn(IO)
init {
execute {
appDb.bookDao.flowAll().mapLatest { books ->
books.map { "${it.name}-${it.author}" }
}.collect {
bookshelf.clear()
bookshelf.addAll(it)
upAdapterLiveData.postValue("isInBookshelf")
}
}
}
/** /**
* 开始搜索 * 开始搜索
*/ */

Loading…
Cancel
Save