Skip to content

Commit a99e20f

Browse files
authored
Merge pull request #1 from zihanpan/issue-5872-fix-search-error
Issue 5872 fix search error
2 parents 63f1ed8 + 14eb8c6 commit a99e20f

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

app/src/main/java/fr/free/nrw/commons/upload/categories/CategoriesPresenter.kt

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.free.nrw.commons.upload.categories
22

3+
import android.annotation.SuppressLint
34
import android.text.TextUtils
45
import androidx.lifecycle.LiveData
56
import androidx.lifecycle.MutableLiveData
@@ -53,6 +54,7 @@ class CategoriesPresenter
5354
@Inject
5455
lateinit var categoryEditHelper: CategoryEditHelper
5556

57+
@SuppressLint("TimberArgCount")
5658
override fun onAttachView(view: CategoriesContract.View) {
5759
this.view = view
5860
compositeDisposable.add(
@@ -68,7 +70,7 @@ class CategoriesPresenter
6870
{
6971
setCategoryListValue(it)
7072
view.showProgress(false)
71-
if (it.isEmpty()) {
73+
if (it.isEmpty() && !isInitialLoad) {
7274
view.showError(R.string.no_categories_found)
7375
}
7476
},
@@ -81,6 +83,9 @@ class CategoriesPresenter
8183
)
8284
}
8385

86+
private var isInitialLoad = true //avoid initial empty content of edittext lead to showError
87+
88+
8489
/**
8590
* If media is null : Fetches categories from server according to the term
8691
* Else : Fetches existing categories by their name, fetches categories from server according
@@ -94,7 +99,7 @@ class CategoriesPresenter
9499
.map {
95100
it.filter { categoryItem ->
96101
!repository.isSpammyCategory(categoryItem.name) ||
97-
categoryItem.name == term
102+
categoryItem.name == term
98103
}
99104
}
100105
} else {
@@ -114,7 +119,7 @@ class CategoriesPresenter
114119
.map {
115120
it.filter { categoryItem ->
116121
!repository.isSpammyCategory(categoryItem.name) ||
117-
categoryItem.name == term
122+
categoryItem.name == term
118123
}
119124
}.map { it.filterNot { categoryItem -> categoryItem.thumbnail == "hidden" } }
120125
}
@@ -123,13 +128,21 @@ class CategoriesPresenter
123128
override fun onDetachView() {
124129
view = DUMMY
125130
compositeDisposable.clear()
131+
isInitialLoad = true
126132
}
127133

128134
/**
129135
* asks the repository to fetch categories for the query
130136
* @param query
131137
*/
132138
override fun searchForCategories(query: String) {
139+
if (query.isBlank()) {
140+
if (!isInitialLoad) {
141+
view.showError(R.string.no_categories_found)
142+
}
143+
return
144+
}
145+
isInitialLoad = false
133146
searchTerms.onNext(query)
134147
}
135148

@@ -187,7 +200,7 @@ class CategoriesPresenter
187200
{
188201
setCategoryListValue(it)
189202
view.showProgress(false)
190-
if (it.isEmpty()) {
203+
if (it.isEmpty() && !isInitialLoad) {
191204
view.showError(R.string.no_categories_found)
192205
}
193206
},
@@ -223,9 +236,9 @@ class CategoriesPresenter
223236
) {
224237
val selectedCategories: MutableList<String> =
225238
(
226-
repository.selectedCategories.map { it.name }.toMutableList() +
227-
repository.selectedExistingCategories
228-
).toMutableList()
239+
repository.selectedCategories.map { it.name }.toMutableList() +
240+
repository.selectedExistingCategories
241+
).toMutableList()
229242

230243
if (selectedCategories.isNotEmpty()) {
231244
view.showProgressDialog()

0 commit comments

Comments
 (0)