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

Fix categories searching error when uploading photos #5884

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.free.nrw.commons.upload.categories

import android.annotation.SuppressLint
import android.text.TextUtils
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
Expand Down Expand Up @@ -53,6 +54,7 @@ class CategoriesPresenter
@Inject
lateinit var categoryEditHelper: CategoryEditHelper

@SuppressLint("TimberArgCount")
override fun onAttachView(view: CategoriesContract.View) {
this.view = view
compositeDisposable.add(
Expand All @@ -68,7 +70,7 @@ class CategoriesPresenter
{
setCategoryListValue(it)
view.showProgress(false)
if (it.isEmpty()) {
if (it.isEmpty() && !isInitialLoad) {
view.showError(R.string.no_categories_found)
}
},
Expand All @@ -81,6 +83,9 @@ class CategoriesPresenter
)
}

private var isInitialLoad = true //avoid initial empty content of edittext lead to showError


/**
* If media is null : Fetches categories from server according to the term
* Else : Fetches existing categories by their name, fetches categories from server according
Expand All @@ -94,7 +99,7 @@ class CategoriesPresenter
.map {
it.filter { categoryItem ->
!repository.isSpammyCategory(categoryItem.name) ||
categoryItem.name == term
categoryItem.name == term
}
}
} else {
Expand All @@ -114,7 +119,7 @@ class CategoriesPresenter
.map {
it.filter { categoryItem ->
!repository.isSpammyCategory(categoryItem.name) ||
categoryItem.name == term
categoryItem.name == term
}
}.map { it.filterNot { categoryItem -> categoryItem.thumbnail == "hidden" } }
}
Expand All @@ -123,13 +128,21 @@ class CategoriesPresenter
override fun onDetachView() {
view = DUMMY
compositeDisposable.clear()
isInitialLoad = true
}

/**
* asks the repository to fetch categories for the query
* @param query
*/
override fun searchForCategories(query: String) {
if (query.isBlank()) {
if (!isInitialLoad) {
view.showError(R.string.no_categories_found)
}
return
}
isInitialLoad = false
searchTerms.onNext(query)
}

Expand Down Expand Up @@ -187,7 +200,7 @@ class CategoriesPresenter
{
setCategoryListValue(it)
view.showProgress(false)
if (it.isEmpty()) {
if (it.isEmpty() && !isInitialLoad) {
view.showError(R.string.no_categories_found)
}
},
Expand Down Expand Up @@ -223,9 +236,9 @@ class CategoriesPresenter
) {
val selectedCategories: MutableList<String> =
(
repository.selectedCategories.map { it.name }.toMutableList() +
repository.selectedExistingCategories
).toMutableList()
repository.selectedCategories.map { it.name }.toMutableList() +
repository.selectedExistingCategories
).toMutableList()

if (selectedCategories.isNotEmpty()) {
view.showProgressDialog()
Expand Down
Loading