优化搜索界面逻辑

pull/61/head
Administrator 5 years ago
parent a99cedd114
commit 18a524266b
  1. 28
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt

@ -3,6 +3,8 @@ package io.legado.app.ui.book.search
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View.GONE
import android.view.View.VISIBLE
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
@ -28,6 +30,7 @@ import kotlinx.coroutines.withContext
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
import org.jetbrains.anko.startActivity 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, SearchViewModel.CallBack,
BookAdapter.CallBack, BookAdapter.CallBack,
@ -110,10 +113,10 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
} }
}) })
search_view.setOnQueryTextFocusChangeListener { _, hasFocus -> search_view.setOnQueryTextFocusChangeListener { _, hasFocus ->
if (hasFocus) { if (!hasFocus && search_view.query.toString().trim().isEmpty()) {
ll_history.visible() finish()
} else { } else {
ll_history.invisible() openOrCloseHistory(hasFocus)
} }
} }
} }
@ -175,6 +178,15 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
} }
} }
private fun openOrCloseHistory(open: Boolean) {
if (open) {
upHistory("")
ll_history.visibility = VISIBLE
} else {
ll_history.visibility = GONE
}
}
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)
@ -216,7 +228,13 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
} else { } else {
App.db.searchKeywordDao().liveDataSearch(key) App.db.searchKeywordDao().liveDataSearch(key)
} }
historyData?.observe(this, Observer { historyKeyAdapter.setItems(it) }) historyData?.observe(this, Observer { historyKeyAdapter.setItems(it)
if (it.isEmpty()) {
tv_clear_history.invisible()
} else {
tv_clear_history.visible()
}
})
} }
override fun startSearch() { override fun startSearch() {
@ -254,4 +272,4 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
} }
} }
} }
} }
Loading…
Cancel
Save