feat: 优化代码

pull/103/head
kunfei 5 years ago
parent b2033c0e59
commit 70b7b4b1dc
  1. 32
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt

@ -198,12 +198,18 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
} }
} }
/**
* 滚动到底部事件
*/
private fun scrollToBottom() { private fun scrollToBottom() {
if (!viewModel.isLoading && viewModel.searchKey.isNotEmpty() && loadMoreView.hasMore) { if (!viewModel.isLoading && viewModel.searchKey.isNotEmpty() && loadMoreView.hasMore) {
viewModel.search("") viewModel.search("")
} }
} }
/**
* 打开关闭历史界面
*/
private fun openOrCloseHistory(open: Boolean) { private fun openOrCloseHistory(open: Boolean) {
if (open) { if (open) {
upHistory(search_view.query.toString()) upHistory(search_view.query.toString())
@ -213,6 +219,9 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
} }
} }
/**
* 更新分组菜单
*/
private fun upGroupMenu() { private fun upGroupMenu() {
val selectedGroup = getPrefString("searchGroup") ?: "" val selectedGroup = getPrefString("searchGroup") ?: ""
menu?.removeGroup(R.id.source_group) menu?.removeGroup(R.id.source_group)
@ -229,6 +238,9 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
menu?.setGroupCheckable(R.id.source_group, true, true) menu?.setGroupCheckable(R.id.source_group, true, true)
} }
/**
* 更新搜索历史
*/
private fun upHistory(key: String? = null) { private fun upHistory(key: String? = null) {
bookData?.removeObservers(this) bookData?.removeObservers(this)
if (key.isNullOrBlank()) { if (key.isNullOrBlank()) {
@ -263,6 +275,9 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
}) })
} }
/**
* 更新搜索结果
*/
@Synchronized @Synchronized
private fun upSearchItems(items: List<SearchBook>, isMandatoryUpdate: Boolean) { private fun upSearchItems(items: List<SearchBook>, isMandatoryUpdate: Boolean) {
if (!isMandatoryUpdate && System.currentTimeMillis() - refreshTime < 1000) { if (!isMandatoryUpdate && System.currentTimeMillis() - refreshTime < 1000) {
@ -274,17 +289,26 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
diffResult.dispatchUpdatesTo(adapter) diffResult.dispatchUpdatesTo(adapter)
} }
/**
* 开始搜索
*/
private fun startSearch() { private fun startSearch() {
refresh_progress_bar.isAutoLoading = true refresh_progress_bar.isAutoLoading = true
fb_stop.visible() fb_stop.visible()
} }
/**
* 搜索结束
*/
private fun searchFinally() { private fun searchFinally() {
refresh_progress_bar.isAutoLoading = false refresh_progress_bar.isAutoLoading = false
loadMoreView.startLoad() loadMoreView.startLoad()
fb_stop.invisible() 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 {
@ -293,16 +317,22 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
} }
} }
/**
* 显示书籍详情
*/
override fun showBookInfo(book: Book) { override fun showBookInfo(book: Book) {
startActivity<BookInfoActivity>( startActivity<BookInfoActivity>(
Pair("bookUrl", book.bookUrl) Pair("bookUrl", book.bookUrl)
) )
} }
/**
* 点击历史关键字
*/
override fun searchHistory(key: String) { override fun searchHistory(key: String) {
launch { launch {
when { when {
search_view.query == key -> { search_view.query.toString() == key -> {
search_view.setQuery(key, true) search_view.setQuery(key, true)
} }
withContext(IO) { App.db.bookDao().findByName(key).isEmpty() } -> { withContext(IO) { App.db.bookDao().findByName(key).isEmpty() } -> {

Loading…
Cancel
Save