From bdde8833e2839237c7fcefcc76ed34d6011142c9 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 1 Dec 2022 17:23:43 +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/ui/book/search/SearchActivity.kt | 14 -------------- .../legado/app/ui/book/search/SearchViewModel.kt | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 14 deletions(-) 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 6bb262baa..d48ffc8ad 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 @@ -17,7 +17,6 @@ import androidx.recyclerview.widget.RecyclerView import com.google.android.flexbox.FlexboxLayoutManager import io.legado.app.R import io.legado.app.base.VMBaseActivity -import io.legado.app.constant.AppLog import io.legado.app.constant.PreferKey import io.legado.app.data.appDb 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.Job import kotlinx.coroutines.delay -import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.conflate -import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import splitties.init.appCtx @@ -291,17 +288,6 @@ class SearchActivity : VMBaseActivity - books.map { "${it.name}-${it.author}" } - }.catch { e -> - AppLog.put("加载书架数据失败", e) - }.collect { - viewModel.bookshelf.clear() - viewModel.bookshelf.addAll(it) - viewModel.upAdapterLiveData.postValue("isInBookshelf") - } - } } /** 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 cb8330bb2..a75582147 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 @@ -10,10 +10,13 @@ import io.legado.app.data.entities.SearchKeyword import io.legado.app.help.config.AppConfig import io.legado.app.model.webBook.SearchModel import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.flowOn +import kotlinx.coroutines.flow.mapLatest +@OptIn(ExperimentalCoroutinesApi::class) class SearchViewModel(application: Application) : BaseViewModel(application) { val bookshelf = hashSetOf() val upAdapterLiveData = MutableLiveData() @@ -57,6 +60,19 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { } }.flowOn(IO) + + init { + execute { + appDb.bookDao.flowAll().mapLatest { books -> + books.map { "${it.name}-${it.author}" } + }.collect { + bookshelf.clear() + bookshelf.addAll(it) + upAdapterLiveData.postValue("isInBookshelf") + } + } + } + /** * 开始搜索 */