|
|
@@ -12,9 +12,11 @@ import androidx.constraintlayout.widget.ConstraintLayout
|
|
|
import androidx.core.view.WindowInsetsCompat
|
|
|
import androidx.core.view.updateLayoutParams
|
|
|
import androidx.core.view.updatePadding
|
|
|
+import androidx.lifecycle.MutableLiveData
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
import androidx.recyclerview.widget.OrientationHelper
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
+import com.adealink.frame.base.Rlt
|
|
|
import com.adealink.frame.base.fastLazy
|
|
|
import com.adealink.frame.mvvm.view.viewBinding
|
|
|
import com.adealink.frame.router.Router
|
|
|
@@ -28,6 +30,7 @@ import com.adealink.weparty.commonui.ext.gone
|
|
|
import com.adealink.weparty.commonui.ext.onWindowInsets
|
|
|
import com.adealink.weparty.commonui.ext.show
|
|
|
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
|
|
|
+import com.adealink.weparty.commonui.recycleview.itemdecoration.VerticalSpaceItemDecoration
|
|
|
import com.adealink.weparty.commonui.recycleview.layoutmanager.FlowLayoutManager
|
|
|
import com.adealink.weparty.module.profile.Profile
|
|
|
import com.adealink.weparty.profile.databinding.ActivityUserSearchBinding
|
|
|
@@ -39,6 +42,7 @@ import com.adealink.weparty.profile.search.data.SearchResultItemData
|
|
|
import com.adealink.weparty.profile.search.viewmodel.SearchViewModel
|
|
|
import com.qmuiteam.qmui.widget.util.QMUIKeyboardHelper
|
|
|
import com.qmuiteam.qmui.widget.util.QMUIStatusBarHelper
|
|
|
+import com.adealink.weparty.R as APP_R
|
|
|
|
|
|
@RouterUri(
|
|
|
path = [Profile.Search.PATH],
|
|
|
@@ -55,7 +59,8 @@ class SearchActivity : BaseActivity(),
|
|
|
private val resultAdapter by fastLazy { MultiTypeListAdapter<SearchResultItemData>() }
|
|
|
|
|
|
private val searchViewModel by viewModels<SearchViewModel>()
|
|
|
- private var isSearching = false
|
|
|
+ private var isSearching = MutableLiveData<Boolean>()
|
|
|
+ private var isInputEmpty = MutableLiveData<Boolean>()
|
|
|
|
|
|
override fun onBeforeCreate() {
|
|
|
super.onBeforeCreate()
|
|
|
@@ -78,6 +83,9 @@ class SearchActivity : BaseActivity(),
|
|
|
resultAdapter.register(SearchItemViewBinder(this))
|
|
|
binding.rvResult.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
|
|
|
binding.rvResult.adapter = resultAdapter
|
|
|
+ binding.rvResult.addItemDecoration(
|
|
|
+ VerticalSpaceItemDecoration(20.dp())
|
|
|
+ )
|
|
|
|
|
|
binding.etSearchInput.isClickable = true
|
|
|
binding.etSearchInput.setOnEditorActionListener(object : OnEditorActionListener {
|
|
|
@@ -86,7 +94,6 @@ class SearchActivity : BaseActivity(),
|
|
|
|| (actionId == EditorInfo.IME_ACTION_UNSPECIFIED && event != null && event.keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_DOWN)
|
|
|
) {
|
|
|
search(binding.etSearchInput.text?.toString(), false)
|
|
|
- binding.etSearchInput.setText(null)
|
|
|
}
|
|
|
return true
|
|
|
}
|
|
|
@@ -110,12 +117,11 @@ class SearchActivity : BaseActivity(),
|
|
|
|
|
|
override fun afterTextChanged(s: Editable?) {
|
|
|
val input = s?.trim()?.toString()
|
|
|
- binding.tvSearch.isEnabled = !input.isNullOrEmpty()
|
|
|
+ isInputEmpty.postValue(input.isNullOrEmpty())
|
|
|
}
|
|
|
})
|
|
|
binding.tvSearch.onClick {
|
|
|
search(binding.etSearchInput.text?.toString(), false)
|
|
|
- binding.etSearchInput.setText(null)
|
|
|
}
|
|
|
|
|
|
fitWindow()
|
|
|
@@ -134,16 +140,20 @@ class SearchActivity : BaseActivity(),
|
|
|
// 键盘显示时使用键盘高度,否则使用导航栏高度
|
|
|
val bottomInset = if (ime.bottom > 0) ime.bottom else nav.bottom
|
|
|
view.updatePadding(bottom = bottomInset)
|
|
|
-// binding.svContent.doOnPreDraw {
|
|
|
-// binding.svContent.smoothScrollTo(0, bottomInset)
|
|
|
-// }
|
|
|
insets
|
|
|
}
|
|
|
}
|
|
|
|
|
|
override fun loadData() {
|
|
|
super.loadData()
|
|
|
- updateHistoryList(SearchLocalService.getSearchHistory())
|
|
|
+ updateHistoryList(SearchLocalService.getSearchHistory()) { list ->
|
|
|
+ if (list.isEmpty()) {
|
|
|
+ binding.clHistory.gone()
|
|
|
+ return@updateHistoryList
|
|
|
+ }
|
|
|
+ binding.clHistory.show()
|
|
|
+ historyAdapter.submitList(list.map { SearchHistoryItemData(it) })
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private fun search(input: String?, fromHistory: Boolean) {
|
|
|
@@ -151,41 +161,86 @@ class SearchActivity : BaseActivity(),
|
|
|
if (keyword.isNullOrEmpty()) {
|
|
|
return
|
|
|
}
|
|
|
- //校验非法字符
|
|
|
+ binding.etSearchInput.clearFocus()
|
|
|
+ QMUIKeyboardHelper.hideKeyboard(binding.etSearchInput)
|
|
|
+ hideHistoryList()
|
|
|
|
|
|
+ //校验非法字符
|
|
|
+ isSearching.postValue(true)
|
|
|
+ searchViewModel.search(keyword)
|
|
|
|
|
|
if (!fromHistory) {
|
|
|
- addHistoryList(input)
|
|
|
+ addHistoryList(input) { list ->
|
|
|
+ historyAdapter.submitList(list.map { SearchHistoryItemData(it) })
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private fun addHistoryList(keyword: String?) {
|
|
|
+ private fun addHistoryList(
|
|
|
+ keyword: String?,
|
|
|
+ commit: ((historyList: List<String>) -> Unit)?
|
|
|
+ ) {
|
|
|
if (keyword.isNullOrEmpty()) {
|
|
|
return
|
|
|
}
|
|
|
historyList.add(0, keyword)
|
|
|
- updateHistoryList(historyList)
|
|
|
+ updateHistoryList(historyList, commit)
|
|
|
}
|
|
|
|
|
|
- private fun updateHistoryList(historyList: List<String>) {
|
|
|
+ private fun updateHistoryList(
|
|
|
+ historyList: List<String>,
|
|
|
+ commit: ((historyList: List<String>) -> Unit)?
|
|
|
+ ) {
|
|
|
this.historyList = historyList.take(SearchLocalService.MAX_HISTORY_COUNT).toMutableList()
|
|
|
SearchLocalService.saveSearchHistory(this.historyList)
|
|
|
- if (historyList.isEmpty()) {
|
|
|
- binding.clHistory.gone()
|
|
|
- return
|
|
|
- }
|
|
|
- binding.clHistory.show()
|
|
|
- historyAdapter.submitList(historyList.map { SearchHistoryItemData(it) })
|
|
|
+ commit?.invoke(this.historyList)
|
|
|
}
|
|
|
|
|
|
override fun observeViewModel() {
|
|
|
super.observeViewModel()
|
|
|
- searchViewModel.searchResultLD.observeWithoutCache(this) {
|
|
|
- isSearching = false
|
|
|
+ isSearching.observe(this) {
|
|
|
+ if (it) {
|
|
|
+ binding.clHistory.gone()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ isInputEmpty.observe(this) {
|
|
|
+ binding.tvSearch.isEnabled = !it
|
|
|
+ if (it) {
|
|
|
+ binding.clSearchResult.gone()
|
|
|
+ if (historyList.isEmpty()) {
|
|
|
+ binding.clHistory.gone()
|
|
|
+ } else {
|
|
|
+ binding.clHistory.show()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ searchViewModel.searchResultLD.observeWithoutCache(this) { rlt ->
|
|
|
+ isSearching.postValue(false)
|
|
|
+ when (rlt) {
|
|
|
+ is Rlt.Failed -> {
|
|
|
+ binding.clSearchResult.show()
|
|
|
+ binding.rvResult.gone()
|
|
|
+ binding.vErrorView.show(
|
|
|
+ errorEmptyResId = APP_R.drawable.common_list_empty_ic,
|
|
|
+ title = APP_R.string.commonui_list_empty
|
|
|
+ )
|
|
|
+ }
|
|
|
|
|
|
+ is Rlt.Success -> {
|
|
|
+ binding.clSearchResult.show()
|
|
|
+ binding.rvResult.show()
|
|
|
+ binding.vErrorView.gone()
|
|
|
+ resultAdapter.submitList(rlt.data)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private fun hideHistoryList() {
|
|
|
+ binding.clHistory.gone()
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
override fun onClick(
|
|
|
item: SearchResultItemData,
|
|
|
pos: Int
|
|
|
@@ -199,6 +254,7 @@ class SearchActivity : BaseActivity(),
|
|
|
item: SearchHistoryItemData,
|
|
|
pos: Int
|
|
|
) {
|
|
|
+ binding.etSearchInput.setText(item.keyword)
|
|
|
search(item.keyword, true)
|
|
|
}
|
|
|
|