feat: 优化代码

pull/95/head
kunfei 5 years ago
parent 18ef671211
commit 853a43272d
  1. 16
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  2. 10
      app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt

@ -34,7 +34,6 @@ import org.jetbrains.anko.startActivity
class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_search), class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_search),
SearchViewModel.CallBack,
BookAdapter.CallBack, BookAdapter.CallBack,
HistoryKeyAdapter.CallBack, HistoryKeyAdapter.CallBack,
SearchAdapter.CallBack { SearchAdapter.CallBack {
@ -53,7 +52,6 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
private var groups = hashSetOf<String>() private var groups = hashSetOf<String>()
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
viewModel.callBack = this
initRecyclerView() initRecyclerView()
initSearchView() initSearchView()
initOtherView() initOtherView()
@ -181,6 +179,13 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
viewModel.searchBookLiveData.observe(this, Observer { viewModel.searchBookLiveData.observe(this, Observer {
setSearchItems(it) setSearchItems(it)
}) })
viewModel.isSearchLiveData.observe(this, Observer {
if (it) {
startSearch()
} else {
searchFinally()
}
})
} }
private fun initIntent() { private fun initIntent() {
@ -256,18 +261,19 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
}) })
} }
@Synchronized
private fun setSearchItems(items: List<SearchBook>) { private fun setSearchItems(items: List<SearchBook>) {
val diffResult = DiffUtil.calculateDiff(DiffCallBack(adapter.getItems(), items)) val diffResult = DiffUtil.calculateDiff(DiffCallBack(ArrayList(adapter.getItems()), items))
adapter.setItems(items, false) adapter.setItems(items, false)
diffResult.dispatchUpdatesTo(adapter) diffResult.dispatchUpdatesTo(adapter)
} }
override fun startSearch() { fun startSearch() {
refresh_progress_bar.isAutoLoading = true refresh_progress_bar.isAutoLoading = true
fb_stop.visible() fb_stop.visible()
} }
override fun searchFinally() { fun searchFinally() {
refresh_progress_bar.isAutoLoading = false refresh_progress_bar.isAutoLoading = false
loadMoreView.startLoad() loadMoreView.startLoad()
fb_stop.invisible() fb_stop.invisible()

@ -21,7 +21,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
private var searchPool = private var searchPool =
Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher()
private var task: Coroutine<*>? = null private var task: Coroutine<*>? = null
var callBack: CallBack? = null var isSearchLiveData = MutableLiveData<Boolean>()
var searchBookLiveData = MutableLiveData<List<SearchBook>>() var searchBookLiveData = MutableLiveData<List<SearchBook>>()
var searchKey: String = "" var searchKey: String = ""
var searchPage = 1 var searchPage = 1
@ -41,7 +41,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
searchKey = key searchKey = key
searchBooks.clear() searchBooks.clear()
} }
callBack?.startSearch() isSearchLiveData.postValue(true)
task = execute { task = execute {
val searchGroup = context.getPrefString("searchGroup") ?: "" val searchGroup = context.getPrefString("searchGroup") ?: ""
val bookSourceList = if (searchGroup.isBlank()) { val bookSourceList = if (searchGroup.isBlank()) {
@ -67,7 +67,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
} }
task?.invokeOnCompletion { task?.invokeOnCompletion {
callBack?.searchFinally() isSearchLiveData.postValue(false)
isLoading = false isLoading = false
} }
} }
@ -169,8 +169,4 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
searchPool.close() searchPool.close()
} }
interface CallBack {
fun startSearch()
fun searchFinally()
}
} }

Loading…
Cancel
Save