Skip to content

Commit

Permalink
show count of searched forums and users
Browse files Browse the repository at this point in the history
  • Loading branch information
5ec1cff committed Oct 3, 2023
1 parent e2c2618 commit 88350ba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ class SearchFragment : BaseFragment() {
layoutManager = LinearLayoutManager(requireContext())
adapter = mSuggestionAdapter
}
viewModel.forumCount.observe(viewLifecycleOwner) {
binding.searchTabLayout.getTabAt(0)?.text =
if (it == null) getString(R.string.search_tab_bar_title)
else getString(R.string.search_tab_bar_title) + "($it)"
}
viewModel.userCount.observe(viewLifecycleOwner) {
binding.searchTabLayout.getTabAt(2)?.text =
if (it == null) getString(R.string.search_tab_user_title)
else getString(R.string.search_tab_user_title) + "($it)"
}
}
return binding.root
}
Expand Down Expand Up @@ -193,6 +203,8 @@ class SearchFragment : BaseFragment() {
viewModel.searchForumEvent.value = Event(t)
viewModel.searchUserEvent.value = Event(t)
viewModel.searchPostEvent.value = Event(t)
viewModel.forumCount.value = null
viewModel.userCount.value = null
}

class SearchSuggestionViewHolder(val binding: SearchSuggestionItemBinding) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ class SearchViewModel : ViewModel() {
// search forums
val searchForumEvent = MutableLiveData<Event<String>>()
var searchedForums = MutableLiveData<SearchState<List<Forum>>>(SearchState.Uninitialized)
val forumCount = MutableLiveData<Int?>(null)
private var searchForumJob: Job? = null

// search users
val searchUserEvent = MutableLiveData<Event<String>>()
var searchedUsers = MutableLiveData<SearchState<List<User>>>(SearchState.Uninitialized)
val userCount = MutableLiveData<Int?>(null)
private var searchUserJob: Job? = null

fun fetchForums(keyword: String) {
Expand All @@ -77,10 +79,12 @@ class SearchViewModel : ViewModel() {
r.fuzzyMatch.forEach { list.add(it.toForum()) }
}
searchedForums.value = SearchState.Result(list)
forumCount.value = list.size
}.onFailure {
if (it !is CancellationException) {
Logger.e("failed to search forum $keyword", it)
searchedForums.value = SearchState.Error(it)
forumCount.value = null
}
}
}
Expand All @@ -99,10 +103,12 @@ class SearchViewModel : ViewModel() {
r.fuzzyMatch.forEach { list.add(it.toUser()) }
}
searchedUsers.value = SearchState.Result(list)
userCount.value = list.size
}.onFailure {
if (it !is CancellationException) {
Logger.e("failed to search user $keyword", it)
searchedUsers.value = SearchState.Error(it)
userCount.value = null
}
}
}
Expand Down

0 comments on commit 88350ba

Please sign in to comment.