diff --git a/commons/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java b/commons/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java index 5815c1e9a..1e20c0264 100644 --- a/commons/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java +++ b/commons/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java @@ -89,6 +89,71 @@ public class CategorizationFragment extends SherlockFragment{ } } + private ArrayList recentCatQuery() { + ArrayList items = new ArrayList(); + ArrayList mergedItems= new ArrayList(); + + try { + Cursor cursor = client.query( + CategoryContentProvider.BASE_URI, + Category.Table.ALL_FIELDS, + null, + new String[]{}, + Category.Table.COLUMN_LAST_USED + " DESC"); + // fixme add a limit on the original query instead of falling out of the loop? + while (cursor.moveToNext() && cursor.getPosition() < SEARCH_CATS_LIMIT) { + Category cat = Category.fromCursor(cursor); + items.add(cat.getName()); + } + + if (MwVolleyApi.GpsCatExists.getGpsCatExists() == true){ + //Log.d(TAG, "GPS cats found in CategorizationFragment.java" + MwVolleyApi.getGpsCat().toString()); + List gpsItems = new ArrayList(MwVolleyApi.getGpsCat()); + //Log.d(TAG, "GPS items: " + gpsItems.toString()); + + mergedItems.addAll(gpsItems); + } + + mergedItems.addAll(items); + } + catch (RemoteException e) { + throw new RuntimeException(e); + } + //Log.d(TAG, "Merged items: " + mergedItems.toString()); + return mergedItems; + } + + private void setCatsAfterAsync(ArrayList categories, String filter) { + + ArrayList items = new ArrayList(); + HashSet existingKeys = new HashSet(); + for(CategoryItem item : categoriesAdapter.getItems()) { + if(item.selected) { + items.add(item); + existingKeys.add(item.name); + } + } + for(String category : categories) { + if(!existingKeys.contains(category)) { + items.add(new CategoryItem(category, false)); + } + } + + categoriesAdapter.setItems(items); + categoriesAdapter.notifyDataSetInvalidated(); + categoriesSearchInProgress.setVisibility(View.GONE); + if (categories.size() == 0) { + if(TextUtils.isEmpty(filter)) { + // If we found no recent cats, show the skip message! + categoriesSkip.setVisibility(View.VISIBLE); + } else { + categoriesNotFoundView.setText(getString(R.string.categories_not_found, filter)); + categoriesNotFoundView.setVisibility(View.VISIBLE); + } + } else { + categoriesList.smoothScrollToPosition(existingKeys.size()); + } + } private class MethodAUpdater extends AsyncTask> { @@ -106,71 +171,13 @@ public class CategorizationFragment extends SherlockFragment{ @Override protected void onPostExecute(ArrayList categories) { super.onPostExecute(categories); - ArrayList items = new ArrayList(); - HashSet existingKeys = new HashSet(); - for(CategoryItem item : categoriesAdapter.getItems()) { - if(item.selected) { - items.add(item); - existingKeys.add(item.name); - } - } - for(String category : categories) { - if(!existingKeys.contains(category)) { - items.add(new CategoryItem(category, false)); - } - } - - categoriesAdapter.setItems(items); - categoriesAdapter.notifyDataSetInvalidated(); - categoriesSearchInProgress.setVisibility(View.GONE); - if (categories.size() == 0) { - if(TextUtils.isEmpty(filter)) { - // If we found no recent cats, show the skip message! - categoriesSkip.setVisibility(View.VISIBLE); - } else { - categoriesNotFoundView.setText(getString(R.string.categories_not_found, filter)); - categoriesNotFoundView.setVisibility(View.VISIBLE); - } - } else { - categoriesList.smoothScrollToPosition(existingKeys.size()); - } + setCatsAfterAsync(categories, filter); } @Override protected ArrayList doInBackground(Void... voids) { if(TextUtils.isEmpty(filter)) { - ArrayList items = new ArrayList(); - ArrayList mergedItems= new ArrayList(); - - try { - Cursor cursor = client.query( - CategoryContentProvider.BASE_URI, - Category.Table.ALL_FIELDS, - null, - new String[]{}, - Category.Table.COLUMN_LAST_USED + " DESC"); - // fixme add a limit on the original query instead of falling out of the loop? - while (cursor.moveToNext() && cursor.getPosition() < SEARCH_CATS_LIMIT) { - Category cat = Category.fromCursor(cursor); - items.add(cat.getName()); - } - - if (MwVolleyApi.GpsCatExists.getGpsCatExists() == true){ - //Log.d(TAG, "GPS cats found in CategorizationFragment.java" + MwVolleyApi.getGpsCat().toString()); - List gpsItems = new ArrayList(MwVolleyApi.getGpsCat()); - //Log.d(TAG, "GPS items: " + gpsItems.toString()); - - mergedItems.addAll(gpsItems); - } - - mergedItems.addAll(items); - } - catch (RemoteException e) { - // faaaail - throw new RuntimeException(e); - } - //Log.d(TAG, "Merged items: " + mergedItems.toString()); - return mergedItems; + return recentCatQuery(); } if(categoriesCache.containsKey(filter)) { @@ -196,7 +203,6 @@ public class CategorizationFragment extends SherlockFragment{ throw new RuntimeException(e); } - ArrayList categoryNodes = result.getNodes("/api/query/search/p/@title"); for(ApiResult categoryNode: categoryNodes) { String cat = categoryNode.getDocument().getTextContent(); @@ -205,7 +211,6 @@ public class CategorizationFragment extends SherlockFragment{ } categoriesCache.put(filter, categories); - return categories; } } @@ -226,68 +231,7 @@ public class CategorizationFragment extends SherlockFragment{ @Override protected void onPostExecute(ArrayList categories) { super.onPostExecute(categories); - ArrayList items = new ArrayList(); - HashSet existingKeys = new HashSet(); - for(CategoryItem item : categoriesAdapter.getItems()) { - if(item.selected) { - items.add(item); - existingKeys.add(item.name); - } - } - for(String category : categories) { - if(!existingKeys.contains(category)) { - items.add(new CategoryItem(category, false)); - } - } - - categoriesAdapter.setItems(items); - categoriesAdapter.notifyDataSetInvalidated(); - categoriesSearchInProgress.setVisibility(View.GONE); - if (categories.size() == 0) { - if(TextUtils.isEmpty(filter)) { - // If we found no recent cats, show the skip message! - categoriesSkip.setVisibility(View.VISIBLE); - } else { - categoriesNotFoundView.setText(getString(R.string.categories_not_found, filter)); - categoriesNotFoundView.setVisibility(View.VISIBLE); - } - } else { - categoriesList.smoothScrollToPosition(existingKeys.size()); - } - } - - private ArrayList recentCatQuery() { - ArrayList items = new ArrayList(); - ArrayList mergedItems= new ArrayList(); - - try { - Cursor cursor = client.query( - CategoryContentProvider.BASE_URI, - Category.Table.ALL_FIELDS, - null, - new String[]{}, - Category.Table.COLUMN_LAST_USED + " DESC"); - // fixme add a limit on the original query instead of falling out of the loop? - while (cursor.moveToNext() && cursor.getPosition() < SEARCH_CATS_LIMIT) { - Category cat = Category.fromCursor(cursor); - items.add(cat.getName()); - } - - if (MwVolleyApi.GpsCatExists.getGpsCatExists() == true){ - //Log.d(TAG, "GPS cats found in CategorizationFragment.java" + MwVolleyApi.getGpsCat().toString()); - List gpsItems = new ArrayList(MwVolleyApi.getGpsCat()); - //Log.d(TAG, "GPS items: " + gpsItems.toString()); - - mergedItems.addAll(gpsItems); - } - - mergedItems.addAll(items); - } - catch (RemoteException e) { - throw new RuntimeException(e); - } - //Log.d(TAG, "Merged items: " + mergedItems.toString()); - return mergedItems; + setCatsAfterAsync(categories, filter); } @Override @@ -322,9 +266,7 @@ public class CategorizationFragment extends SherlockFragment{ categories.add(categoryNode.getDocument().getTextContent()); } - categoriesCache.put(filter, categories); - return categories; } }