1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-24 07:37:30 +00:00

make following search better?

This commit is contained in:
Austin Huang 2021-07-24 19:30:42 -04:00
parent 5ce6859837
commit de56bfaa10
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F

View File

@ -6,6 +6,7 @@ import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
@ -113,11 +114,15 @@ class FollowViewerFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener {
binding.swipeRefreshLayout.isRefreshing = true binding.swipeRefreshLayout.isRefreshing = true
val liveData = if (searching) viewModel.search(isFollowersList) val liveData = if (searching) viewModel.search(isFollowersList)
else viewModel.fetch(isFollowersList, null) else viewModel.fetch(isFollowersList, null)
liveData.observe(viewLifecycleOwner) { val listOb = object : Observer<Resource<Any?>> {
override fun onChanged(it: Resource<Any?>) {
binding.swipeRefreshLayout.isRefreshing = it.status != Resource.Status.SUCCESS binding.swipeRefreshLayout.isRefreshing = it.status != Resource.Status.SUCCESS
layoutManager.scrollToPosition(totalItemsCount) layoutManager.scrollToPosition(totalItemsCount)
liveData.removeObserver(this)
} }
} }
liveData.observe(viewLifecycleOwner, listOb)
}
binding.rvFollow.addOnScrollListener(lazyLoader) binding.rvFollow.addOnScrollListener(lazyLoader)
binding.rvFollow.layoutManager = layoutManager binding.rvFollow.layoutManager = layoutManager
viewModel.getList(isFollowersList).observe(viewLifecycleOwner) { viewModel.getList(isFollowersList).observe(viewLifecycleOwner) {
@ -153,25 +158,28 @@ class FollowViewerFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener {
override fun onQueryTextChange(query: String): Boolean { override fun onQueryTextChange(query: String): Boolean {
if (query.isEmpty()) { if (query.isEmpty()) {
if (!isCompare && searching) { if (!isCompare && searching) {
searching = false
viewModel.setQuery(null, isFollowersList) viewModel.setQuery(null, isFollowersList)
viewModel.getSearch().removeObservers(viewLifecycleOwner) viewModel.getSearch().removeObservers(viewLifecycleOwner)
viewModel.getList(isFollowersList).observe(viewLifecycleOwner) { viewModel.getList(isFollowersList).observe(viewLifecycleOwner) {
refreshAdapter(it, null, null, null) refreshAdapter(it, null, null, null)
} }
searching = false
return true return true
} }
if (isCompare && searching) { if (searching) {
adapter!!.filter.filter("") adapter!!.filter.filter("")
searching = false
}
return true return true
} }
} if (!searching) {
viewModel.getList(isFollowersList).removeObservers(viewLifecycleOwner)
searching = true searching = true
}
if (isCompare && adapter != null) { if (isCompare && adapter != null) {
adapter!!.filter.filter(query) adapter!!.filter.filter(query)
return true return true
} }
viewModel.getList(isFollowersList).removeObservers(viewLifecycleOwner)
binding.swipeRefreshLayout.isRefreshing = true binding.swipeRefreshLayout.isRefreshing = true
viewModel.setQuery(query, isFollowersList) viewModel.setQuery(query, isFollowersList)
viewModel.getSearch().observe(viewLifecycleOwner) { viewModel.getSearch().observe(viewLifecycleOwner) {