25
25
import butterknife .BindView ;
26
26
import butterknife .ButterKnife ;
27
27
import fr .free .nrw .commons .R ;
28
+ import fr .free .nrw .commons .category .CategoryClient ;
28
29
import fr .free .nrw .commons .category .CategoryDetailsActivity ;
29
30
import fr .free .nrw .commons .di .CommonsDaggerSupportFragment ;
30
31
import fr .free .nrw .commons .explore .recentsearches .RecentSearch ;
@@ -58,9 +59,12 @@ public class SearchCategoryFragment extends CommonsDaggerSupportFragment {
58
59
String query ;
59
60
@ BindView (R .id .bottomProgressBar )
60
61
ProgressBar bottomProgressBar ;
62
+ boolean isLoadingCategories ;
61
63
62
64
@ Inject RecentSearchesDao recentSearchesDao ;
63
65
@ Inject MediaWikiApi mwApi ;
66
+ @ Inject CategoryClient categoryClient ;
67
+
64
68
@ Inject
65
69
@ Named ("default_preferences" )
66
70
JsonKvStore basicKvStore ;
@@ -135,48 +139,51 @@ public void updateCategoryList(String query) {
135
139
progressBar .setVisibility (GONE );
136
140
queryList .clear ();
137
141
categoriesAdapter .clear ();
138
- compositeDisposable .add (Observable . fromCallable (() -> mwApi . searchCategory ( query ,queryList . size ()) )
142
+ compositeDisposable .add (categoryClient . searchCategories ( query ,25 )
139
143
.subscribeOn (Schedulers .io ())
140
144
.observeOn (AndroidSchedulers .mainThread ())
141
145
.timeout (TIMEOUT_SECONDS , TimeUnit .SECONDS )
142
146
.doOnSubscribe (disposable -> saveQuery (query ))
147
+ .collect (ArrayList <String >::new , ArrayList ::add )
143
148
.subscribe (this ::handleSuccess , this ::handleError ));
144
149
}
145
150
146
151
147
152
/**
148
- * Adds more results to existing search results
153
+ * Adds 25 more results to existing search results
149
154
*/
150
155
public void addCategoriesToList (String query ) {
156
+ if (isLoadingCategories ) return ;
157
+ isLoadingCategories =true ;
151
158
this .query = query ;
152
159
bottomProgressBar .setVisibility (View .VISIBLE );
153
160
progressBar .setVisibility (GONE );
154
- compositeDisposable .add (Observable . fromCallable (() -> mwApi . searchCategory ( query ,queryList .size () ))
161
+ compositeDisposable .add (categoryClient . searchCategories ( query ,25 , queryList .size ())
155
162
.subscribeOn (Schedulers .io ())
156
163
.observeOn (AndroidSchedulers .mainThread ())
157
164
.timeout (TIMEOUT_SECONDS , TimeUnit .SECONDS )
165
+ .collect (ArrayList <String >::new , ArrayList ::add )
158
166
.subscribe (this ::handlePaginationSuccess , this ::handleError ));
159
167
}
160
168
161
169
/**
162
170
* Handles the success scenario
163
171
* it initializes the recycler view by adding items to the adapter
164
- * @param mediaList
165
172
*/
166
173
private void handlePaginationSuccess (List <String > mediaList ) {
167
174
queryList .addAll (mediaList );
168
175
progressBar .setVisibility (View .GONE );
169
176
bottomProgressBar .setVisibility (GONE );
170
177
categoriesAdapter .addAll (mediaList );
171
178
categoriesAdapter .notifyDataSetChanged ();
179
+ isLoadingCategories =false ;
172
180
}
173
181
174
182
175
183
176
184
/**
177
185
* Handles the success scenario
178
186
* it initializes the recycler view by adding items to the adapter
179
- * @param mediaList
180
187
*/
181
188
private void handleSuccess (List <String > mediaList ) {
182
189
queryList = mediaList ;
@@ -194,7 +201,6 @@ private void handleSuccess(List<String> mediaList) {
194
201
195
202
/**
196
203
* Logs and handles API error scenario
197
- * @param throwable
198
204
*/
199
205
private void handleError (Throwable throwable ) {
200
206
Timber .e (throwable , "Error occurred while loading queried categories" );
@@ -213,7 +219,7 @@ private void handleError(Throwable throwable) {
213
219
private void initErrorView () {
214
220
progressBar .setVisibility (GONE );
215
221
categoriesNotFoundView .setVisibility (VISIBLE );
216
- categoriesNotFoundView .setText (getString (R .string .categories_not_found , query ));
222
+ categoriesNotFoundView .setText (getString (R .string .categories_not_found ));
217
223
}
218
224
219
225
/**
0 commit comments