优化
This commit is contained in:
@@ -17,8 +17,10 @@ import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.asCoroutineDispatcher
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.onCompletion
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
@@ -39,6 +41,7 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB
|
||||
private var bookSourceParts = emptyList<BookSourcePart>()
|
||||
private var searchBooks = arrayListOf<SearchBook>()
|
||||
private var searchJob: Job? = null
|
||||
private var workingState = MutableStateFlow(true)
|
||||
|
||||
|
||||
private fun initSearchPool() {
|
||||
@@ -80,6 +83,7 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB
|
||||
bs.getBookSource()?.let {
|
||||
emit(it)
|
||||
}
|
||||
workingState.first { it }
|
||||
}
|
||||
}.onStart {
|
||||
callBack.onSearchStart()
|
||||
@@ -176,6 +180,14 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB
|
||||
}
|
||||
}
|
||||
|
||||
fun pause() {
|
||||
workingState.value = false
|
||||
}
|
||||
|
||||
fun resume() {
|
||||
workingState.value = true
|
||||
}
|
||||
|
||||
fun cancelSearch() {
|
||||
close()
|
||||
callBack.onSearchCancel()
|
||||
|
||||
@@ -12,7 +12,9 @@ import androidx.activity.viewModels
|
||||
import androidx.appcompat.widget.SearchView
|
||||
import androidx.core.os.bundleOf
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.google.android.flexbox.FlexboxLayoutManager
|
||||
@@ -49,6 +51,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||
import io.legado.app.utils.visible
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.awaitCancellation
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.conflate
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
@@ -304,6 +307,16 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
|
||||
groups = it
|
||||
}
|
||||
}
|
||||
lifecycleScope.launch {
|
||||
repeatOnLifecycle(Lifecycle.State.RESUMED) {
|
||||
viewModel.resume()
|
||||
try {
|
||||
awaitCancellation()
|
||||
} finally {
|
||||
viewModel.pause()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -117,6 +117,14 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
|
||||
searchModel.cancelSearch()
|
||||
}
|
||||
|
||||
fun pause() {
|
||||
searchModel.pause()
|
||||
}
|
||||
|
||||
fun resume() {
|
||||
searchModel.resume()
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存搜索关键字
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user