Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent possible crash when showing results for other languages #4958

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions app/src/main/java/org/wikipedia/search/SearchResultsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -176,31 +176,32 @@ class SearchResultsFragment : Fragment() {

private inner class NoSearchResultAdapter : RecyclerView.Adapter<NoSearchResultItemViewHolder>() {
override fun onBindViewHolder(holder: NoSearchResultItemViewHolder, position: Int) {
holder.bindItem(position, viewModel.resultsCount[position])
holder.bindItem(viewModel.countsPerLanguageCode[position])
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoSearchResultItemViewHolder {
return NoSearchResultItemViewHolder(ItemSearchNoResultsBinding.inflate(layoutInflater, parent, false))
}

override fun getItemCount(): Int { return viewModel.resultsCount.size }
override fun getItemCount(): Int { return viewModel.countsPerLanguageCode.size }
}

private inner class NoSearchResultItemViewHolder(val itemBinding: ItemSearchNoResultsBinding) : DefaultViewHolder<View>(itemBinding.root) {
private val accentColorStateList = getThemedColorStateList(requireContext(), R.attr.progressive_color)
private val secondaryColorStateList = getThemedColorStateList(requireContext(), R.attr.secondary_color)
fun bindItem(position: Int, resultsCount: Int) {
if (resultsCount == 0 && viewModel.invokeSource == Constants.InvokeSource.PLACES) {
fun bindItem(resultPair: Pair<String, Int>) {
val langCode = resultPair.first
val resultCount = resultPair.second
if (resultCount == 0 && viewModel.invokeSource == Constants.InvokeSource.PLACES) {
PlacesEvent.logAction("no_results_impression", "search_view")
}
val langCode = WikipediaApp.instance.languageState.appLanguageCodes[position]
itemBinding.resultsText.text = if (resultsCount == 0) getString(R.string.search_results_count_zero) else resources.getQuantityString(R.plurals.search_results_count, resultsCount, resultsCount)
itemBinding.resultsText.setTextColor(if (resultsCount == 0) secondaryColorStateList else accentColorStateList)
itemBinding.languageCode.visibility = if (viewModel.resultsCount.size == 1) View.GONE else View.VISIBLE
itemBinding.resultsText.text = if (resultCount == 0) getString(R.string.search_results_count_zero) else resources.getQuantityString(R.plurals.search_results_count, resultCount, resultCount)
itemBinding.resultsText.setTextColor(if (resultCount == 0) secondaryColorStateList else accentColorStateList)
itemBinding.languageCode.visibility = if (viewModel.countsPerLanguageCode.size == 1) View.GONE else View.VISIBLE
itemBinding.languageCode.setLangCode(langCode)
itemBinding.languageCode.setTextColor(if (resultsCount == 0) secondaryColorStateList else accentColorStateList)
itemBinding.languageCode.setBackgroundTint(if (resultsCount == 0) secondaryColorStateList else accentColorStateList)
view.isEnabled = resultsCount > 0
itemBinding.languageCode.setTextColor(if (resultCount == 0) secondaryColorStateList else accentColorStateList)
itemBinding.languageCode.setBackgroundTint(if (resultCount == 0) secondaryColorStateList else accentColorStateList)
view.isEnabled = resultCount > 0
view.setOnClickListener {
if (!isAdded) {
return@setOnClickListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ class SearchResultsViewModel : ViewModel() {

private val batchSize = 20
private val delayMillis = 200L
var resultsCount = mutableListOf<Int>()
var countsPerLanguageCode = mutableListOf<Pair<String, Int>>()
var searchTerm: String? = null
var languageCode: String? = null
lateinit var invokeSource: Constants.InvokeSource

@OptIn(FlowPreview::class) // TODO: revisit if the debounce method changed.
val searchResultsFlow = Pager(PagingConfig(pageSize = batchSize, initialLoadSize = batchSize)) {
SearchResultsPagingSource(searchTerm, languageCode, resultsCount, invokeSource)
SearchResultsPagingSource(searchTerm, languageCode, countsPerLanguageCode, invokeSource)
}.flow.debounce(delayMillis).cachedIn(viewModelScope)

class SearchResultsPagingSource(
private val searchTerm: String?,
private val languageCode: String?,
private var resultsCount: MutableList<Int>?,
private var countsPerLanguageCode: MutableList<Pair<String, Int>>,
private var invokeSource: Constants.InvokeSource
) : PagingSource<Int, SearchResult>() {

Expand Down Expand Up @@ -93,14 +93,12 @@ class SearchResultsViewModel : ViewModel() {
}

if (resultList.isEmpty() && response?.continuation == null) {
resultsCount?.clear()
countsPerLanguageCode.clear()
WikipediaApp.instance.languageState.appLanguageCodes.forEach { langCode ->
if (langCode == languageCode) {
resultsCount?.add(0)
} else {
var countResultSize = 0
if (langCode != languageCode) {
val prefixSearchResponse = ServiceFactory.get(WikiSite.forLanguageCode(langCode))
.prefixSearch(searchTerm, params.loadSize, 0)
var countResultSize = 0
prefixSearchResponse.query?.pages?.let {
countResultSize = it.size
}
Expand All @@ -109,12 +107,8 @@ class SearchResultsViewModel : ViewModel() {
.fullTextSearch(searchTerm, params.loadSize, null)
countResultSize = fullTextSearchResponse.query?.pages?.size ?: 0
}
resultsCount?.add(countResultSize)
}
}
// make a singleton list if all results are empty.
if (resultsCount?.sum() == 0) {
resultsCount = mutableListOf(0)
countsPerLanguageCode.add(langCode to countResultSize)
}
}

Expand Down
Loading