33
33
import android .widget .TextView ;
34
34
import android .widget .Toast ;
35
35
36
+ import org .mediawiki .api .ApiResult ;
37
+ import org .mediawiki .api .MWApi ;
38
+
39
+ import java .io .IOException ;
36
40
import java .util .ArrayList ;
37
41
import java .util .Date ;
38
42
import java .util .HashMap ;
41
45
import java .util .List ;
42
46
import java .util .Set ;
43
47
import java .util .concurrent .CountDownLatch ;
48
+ import java .util .concurrent .ExecutionException ;
44
49
import java .util .concurrent .ScheduledThreadPoolExecutor ;
45
50
51
+ import fr .free .nrw .commons .CommonsApplication ;
46
52
import fr .free .nrw .commons .R ;
47
53
import fr .free .nrw .commons .Utils ;
48
54
import fr .free .nrw .commons .upload .MwVolleyApi ;
@@ -73,6 +79,9 @@ public static interface OnCategoriesSaveHandler {
73
79
PrefixUpdater prefixUpdaterSub ;
74
80
MethodAUpdater methodAUpdaterSub ;
75
81
82
+ private final ArrayList <String > titleCatItems = new ArrayList <String >();
83
+ final CountDownLatch mergeLatch = new CountDownLatch (1 );
84
+
76
85
private ContentProviderClient client ;
77
86
78
87
protected final static int SEARCH_CATS_LIMIT = 25 ;
@@ -113,12 +122,48 @@ public void writeToParcel(Parcel parcel, int flags) {
113
122
}
114
123
115
124
/**
116
- * Retrieves recently-used categories and nearby categories, and merges them without duplicates.
117
- * @return a list containing these categories
125
+ * Retrieves category suggestions from title input
126
+ * @return a list containing title-related categories
127
+ */
128
+ protected ArrayList <String > titleCatQuery () {
129
+
130
+ TitleCategories titleCategoriesSub ;
131
+
132
+ //Retrieve the title that was saved when user tapped submit icon
133
+ SharedPreferences titleDesc = PreferenceManager .getDefaultSharedPreferences (getActivity ());
134
+ String title = titleDesc .getString ("Title" , "" );
135
+ Log .d (TAG , "Title: " + title );
136
+
137
+ //Override onPostExecute to access the results of async API call
138
+ titleCategoriesSub = new TitleCategories (title ) {
139
+ @ Override
140
+ protected void onPostExecute (ArrayList <String > result ) {
141
+ super .onPostExecute (result );
142
+ Log .d (TAG , "Results in onPostExecute: " + result );
143
+ titleCatItems .addAll (result );
144
+ Log .d (TAG , "TitleCatItems in onPostExecute: " + titleCatItems );
145
+ mergeLatch .countDown ();
146
+ }
147
+ };
148
+
149
+ Utils .executeAsyncTask (titleCategoriesSub );
150
+ Log .d (TAG , "TitleCatItems in titleCatQuery: " + titleCatItems );
151
+
152
+ //Only return titleCatItems after API call has finished
153
+ try {
154
+ mergeLatch .await ();
155
+ } catch (InterruptedException e ) {
156
+ Log .e (TAG , "Interrupted exception: " , e );
157
+ }
158
+ return titleCatItems ;
159
+ }
160
+
161
+ /**
162
+ * Retrieves recently-used categories
163
+ * @return a list containing recent categories
118
164
*/
119
165
protected ArrayList <String > recentCatQuery () {
120
166
ArrayList <String > items = new ArrayList <String >();
121
- Set <String > mergedItems = new LinkedHashSet <String >();
122
167
123
168
try {
124
169
Cursor cursor = client .query (
@@ -133,23 +178,49 @@ protected ArrayList<String> recentCatQuery() {
133
178
items .add (cat .getName ());
134
179
}
135
180
cursor .close ();
181
+ }
182
+ catch (RemoteException e ) {
183
+ throw new RuntimeException (e );
184
+ }
185
+ return items ;
186
+ }
136
187
137
- if (MwVolleyApi .GpsCatExists .getGpsCatExists () == true ) {
138
- //Log.d(TAG, "GPS cats found in CategorizationFragment.java" + MwVolleyApi.getGpsCat().toString());
139
- List <String > gpsItems = new ArrayList <String >(MwVolleyApi .getGpsCat ());
140
- //Log.d(TAG, "GPS items: " + gpsItems.toString());
188
+ /**
189
+ * Merges nearby categories, categories suggested based on title, and recent categories... without duplicates.
190
+ * @return a list containing merged categories
191
+ */
192
+ protected ArrayList <String > mergeItems () {
141
193
142
- mergedItems .addAll (gpsItems );
143
- }
194
+ Set <String > mergedItems = new LinkedHashSet <String >();
195
+
196
+ Log .d (TAG , "Calling APIs for GPS cats, title cats and recent cats..." );
144
197
145
- mergedItems .addAll (items );
198
+ List <String > gpsItems = new ArrayList <String >();
199
+ if (MwVolleyApi .GpsCatExists .getGpsCatExists ()) {
200
+ gpsItems .addAll (MwVolleyApi .getGpsCat ());
146
201
}
147
- catch (RemoteException e ) {
148
- throw new RuntimeException (e );
202
+ List <String > titleItems = new ArrayList <String >(titleCatQuery ());
203
+ List <String > recentItems = new ArrayList <String >(recentCatQuery ());
204
+
205
+ //Await results of titleItems, which is likely to come in last
206
+ try {
207
+ mergeLatch .await ();
208
+ Log .d (TAG , "Waited for merge" );
209
+ } catch (InterruptedException e ) {
210
+ Log .e (TAG , "Interrupted Exception: " , e );
149
211
}
150
212
213
+ mergedItems .addAll (gpsItems );
214
+ Log .d (TAG , "Adding GPS items: " + gpsItems );
215
+ mergedItems .addAll (titleItems );
216
+ Log .d (TAG , "Adding title items: " + titleItems );
217
+ mergedItems .addAll (recentItems );
218
+ Log .d (TAG , "Adding recent items: " + recentItems );
219
+
151
220
//Needs to be an ArrayList and not a List unless we want to modify a big portion of preexisting code
152
221
ArrayList <String > mergedItemsList = new ArrayList <String >(mergedItems );
222
+ Log .d (TAG , "Merged item list: " + mergedItemsList );
223
+
153
224
return mergedItemsList ;
154
225
}
155
226
0 commit comments