pull/43/head^2
kunfei 5 years ago
parent d640d941dc
commit 22779b2150
  1. 20
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  2. 12
      app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt

@ -99,14 +99,7 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
search_view.clearFocus() search_view.clearFocus()
query?.let { query?.let {
viewModel.saveSearchKey(query) viewModel.saveSearchKey(query)
viewModel.search(it, { viewModel.search(it)
refresh_progress_bar.isAutoLoading = true
initData()
fb_stop.visible()
}, {
refresh_progress_bar.isAutoLoading = false
fb_stop.invisible()
})
} }
return true return true
} }
@ -227,6 +220,17 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
historyData?.observe(this, Observer { historyKeyAdapter.setItems(it) }) historyData?.observe(this, Observer { historyKeyAdapter.setItems(it) })
} }
override fun startSearch() {
refresh_progress_bar.isAutoLoading = true
initData()
fb_stop.visible()
}
override fun searchFinally() {
refresh_progress_bar.isAutoLoading = false
fb_stop.invisible()
}
override fun showBookInfo(name: String, author: String) { override fun showBookInfo(name: String, author: String) {
viewModel.getSearchBook(name, author) { searchBook -> viewModel.getSearchBook(name, author) { searchBook ->
searchBook?.let { searchBook?.let {

@ -25,11 +25,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
var isLoading = false var isLoading = false
private var searchBooks = arrayListOf<SearchBook>() private var searchBooks = arrayListOf<SearchBook>()
fun search( fun search(key: String) {
key: String,
start: (() -> Unit)? = null,
finally: (() -> Unit)? = null
) {
task?.cancel() task?.cancel()
if (key.isEmpty()) { if (key.isEmpty()) {
searchPage++ searchPage++
@ -39,7 +35,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
} }
if (searchKey.isEmpty()) return if (searchKey.isEmpty()) return
startTime = System.currentTimeMillis() startTime = System.currentTimeMillis()
start?.invoke() callBack?.startSearch()
task = execute { task = execute {
isLoading = true isLoading = true
val searchGroup = context.getPrefString("searchGroup") ?: "" val searchGroup = context.getPrefString("searchGroup") ?: ""
@ -66,7 +62,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
} }
task?.invokeOnCompletion { task?.invokeOnCompletion {
finally?.invoke() callBack?.searchFinally()
isLoading = false isLoading = false
} }
} }
@ -176,5 +172,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
interface CallBack { interface CallBack {
var adapter: SearchAdapter var adapter: SearchAdapter
fun startSearch()
fun searchFinally()
} }
} }

Loading…
Cancel
Save