From de56bfaa10071fbaea9004d5b6f102f4c0fdc51f Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Sat, 24 Jul 2021 19:30:42 -0400 Subject: [PATCH] make following search better? --- .../fragments/FollowViewerFragment.kt | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.kt b/app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.kt index 1130dabd..c8315a48 100644 --- a/app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.kt +++ b/app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.kt @@ -6,6 +6,7 @@ import androidx.appcompat.app.ActionBar import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.SearchView import androidx.fragment.app.Fragment +import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider import androidx.navigation.fragment.findNavController import androidx.recyclerview.widget.LinearLayoutManager @@ -113,10 +114,14 @@ class FollowViewerFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener { binding.swipeRefreshLayout.isRefreshing = true val liveData = if (searching) viewModel.search(isFollowersList) else viewModel.fetch(isFollowersList, null) - liveData.observe(viewLifecycleOwner) { - binding.swipeRefreshLayout.isRefreshing = it.status != Resource.Status.SUCCESS - layoutManager.scrollToPosition(totalItemsCount) + val listOb = object : Observer> { + override fun onChanged(it: Resource) { + binding.swipeRefreshLayout.isRefreshing = it.status != Resource.Status.SUCCESS + layoutManager.scrollToPosition(totalItemsCount) + liveData.removeObserver(this) + } } + liveData.observe(viewLifecycleOwner, listOb) } binding.rvFollow.addOnScrollListener(lazyLoader) binding.rvFollow.layoutManager = layoutManager @@ -153,25 +158,28 @@ class FollowViewerFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener { override fun onQueryTextChange(query: String): Boolean { if (query.isEmpty()) { if (!isCompare && searching) { - searching = false viewModel.setQuery(null, isFollowersList) viewModel.getSearch().removeObservers(viewLifecycleOwner) viewModel.getList(isFollowersList).observe(viewLifecycleOwner) { refreshAdapter(it, null, null, null) } + searching = false return true } - if (isCompare && searching) { + if (searching) { adapter!!.filter.filter("") - return true + searching = false } + return true + } + if (!searching) { + viewModel.getList(isFollowersList).removeObservers(viewLifecycleOwner) + searching = true } - searching = true if (isCompare && adapter != null) { adapter!!.filter.filter(query) return true } - viewModel.getList(isFollowersList).removeObservers(viewLifecycleOwner) binding.swipeRefreshLayout.isRefreshing = true viewModel.setQuery(query, isFollowersList) viewModel.getSearch().observe(viewLifecycleOwner) {