Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Sep 11, 2023
1 parent 1d23acf commit 9b45ce0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB
onSuccess(searchId, it)
}
.onFinally {
ensureActive()
onFinally(searchId)
}
tasks.add(task)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import io.legado.app.databinding.ActivityAllBookmarkBinding
import io.legado.app.ui.file.HandleFileContract
import io.legado.app.utils.showDialogFragment
import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

class AllBookmarkActivity : VMBaseActivity<ActivityAllBookmarkBinding, AllBookmarkViewModel>(),
Expand All @@ -35,7 +37,7 @@ class AllBookmarkActivity : VMBaseActivity<ActivityAllBookmarkBinding, AllBookma
override fun onActivityCreated(savedInstanceState: Bundle?) {
initView()
lifecycleScope.launch {
appDb.bookmarkDao.flowAll().collect {
appDb.bookmarkDao.flowAll().flowOn(IO).collect {
adapter.setItems(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
private var searchPool: ExecutorCoroutineDispatcher? = null
private val tasks = CompositeCoroutine()
private var searchSuccess: ((SearchBook) -> Unit)? = null
private var upAdapter: (() -> Unit)? = null
private var bookSourceList = arrayListOf<BookSource>()
private val defaultCover by lazy {
listOf(
Expand All @@ -42,8 +43,8 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
val searchStateData = MutableLiveData<Boolean>()
var name: String = ""
var author: String = ""
val dataFlow = callbackFlow<List<SearchBook>> {
val searchBooks = Collections.synchronizedList(arrayListOf<SearchBook>())
val searchBooks: MutableList<SearchBook> = Collections.synchronizedList(arrayListOf())
val dataFlow = callbackFlow {

searchSuccess = { searchBook ->
if (!searchBooks.contains(searchBook)) {
Expand All @@ -52,6 +53,10 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
}
}

upAdapter = {
trySend(defaultCover + searchBooks.sortedBy { it.originOrder })
}

appDb.searchBookDao.getEnableHasCover(name, author).let {
searchBooks.addAll(it)
trySend(defaultCover + searchBooks.toList())
Expand All @@ -64,6 +69,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
awaitClose {
searchBooks.clear()
searchSuccess = null
upAdapter = null
}
}.flowOn(IO)

Expand All @@ -90,6 +96,8 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
private fun startSearch() {
execute {
stopSearch()
searchBooks.clear()
upAdapter?.invoke()
bookSourceList.clear()
bookSourceList.addAll(appDb.bookSourceDao.allEnabled)
searchStateData.postValue(true)
Expand All @@ -112,7 +120,13 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
return
}
val task = WebBook
.searchBook(viewModelScope, source, name, context = searchPool!!)
.searchBook(
viewModelScope,
source,
name,
context = searchPool!!,
executeContext = searchPool!!
)
.timeout(60000L)
.onSuccess(IO) {
it.firstOrNull()?.let { searchBook ->
Expand All @@ -124,7 +138,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
}
}
}
.onFinally(searchPool) {
.onFinally {
searchNext()
}
tasks.add(task)
Expand Down

0 comments on commit 9b45ce0

Please sign in to comment.