1
1
package fr.free.nrw.commons.upload.categories
2
2
3
+ import android.annotation.SuppressLint
3
4
import android.text.TextUtils
4
5
import androidx.lifecycle.LiveData
5
6
import androidx.lifecycle.MutableLiveData
@@ -53,6 +54,7 @@ class CategoriesPresenter
53
54
@Inject
54
55
lateinit var categoryEditHelper: CategoryEditHelper
55
56
57
+ @SuppressLint(" TimberArgCount" )
56
58
override fun onAttachView (view : CategoriesContract .View ) {
57
59
this .view = view
58
60
compositeDisposable.add(
@@ -68,7 +70,7 @@ class CategoriesPresenter
68
70
{
69
71
setCategoryListValue(it)
70
72
view.showProgress(false )
71
- if (it.isEmpty()) {
73
+ if (it.isEmpty() && ! isInitialLoad ) {
72
74
view.showError(R .string.no_categories_found)
73
75
}
74
76
},
@@ -81,6 +83,9 @@ class CategoriesPresenter
81
83
)
82
84
}
83
85
86
+ private var isInitialLoad = true // avoid initial empty content of edittext lead to showError
87
+
88
+
84
89
/* *
85
90
* If media is null : Fetches categories from server according to the term
86
91
* Else : Fetches existing categories by their name, fetches categories from server according
@@ -94,7 +99,7 @@ class CategoriesPresenter
94
99
.map {
95
100
it.filter { categoryItem ->
96
101
! repository.isSpammyCategory(categoryItem.name) ||
97
- categoryItem.name == term
102
+ categoryItem.name == term
98
103
}
99
104
}
100
105
} else {
@@ -114,7 +119,7 @@ class CategoriesPresenter
114
119
.map {
115
120
it.filter { categoryItem ->
116
121
! repository.isSpammyCategory(categoryItem.name) ||
117
- categoryItem.name == term
122
+ categoryItem.name == term
118
123
}
119
124
}.map { it.filterNot { categoryItem -> categoryItem.thumbnail == " hidden" } }
120
125
}
@@ -123,13 +128,21 @@ class CategoriesPresenter
123
128
override fun onDetachView () {
124
129
view = DUMMY
125
130
compositeDisposable.clear()
131
+ isInitialLoad = true
126
132
}
127
133
128
134
/* *
129
135
* asks the repository to fetch categories for the query
130
136
* @param query
131
137
*/
132
138
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
133
146
searchTerms.onNext(query)
134
147
}
135
148
@@ -187,7 +200,7 @@ class CategoriesPresenter
187
200
{
188
201
setCategoryListValue(it)
189
202
view.showProgress(false )
190
- if (it.isEmpty()) {
203
+ if (it.isEmpty() && ! isInitialLoad ) {
191
204
view.showError(R .string.no_categories_found)
192
205
}
193
206
},
@@ -223,9 +236,9 @@ class CategoriesPresenter
223
236
) {
224
237
val selectedCategories: MutableList <String > =
225
238
(
226
- repository.selectedCategories.map { it.name }.toMutableList() +
227
- repository.selectedExistingCategories
228
- ).toMutableList()
239
+ repository.selectedCategories.map { it.name }.toMutableList() +
240
+ repository.selectedExistingCategories
241
+ ).toMutableList()
229
242
230
243
if (selectedCategories.isNotEmpty()) {
231
244
view.showProgressDialog()
0 commit comments