From a51f4252bb8a3a62e4bdfa55ca16869753151f8d Mon Sep 17 00:00:00 2001 From: misaochan Date: Sun, 24 Jan 2016 20:35:12 +1300 Subject: [PATCH] Getting results but only from either method --- .../category/CategorizationFragment.java | 127 +++++++++--------- 1 file changed, 61 insertions(+), 66 deletions(-) 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 5bfd20b4f..32924c020 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 @@ -46,14 +46,15 @@ public class CategorizationFragment extends SherlockFragment{ TextView categoriesSkip; CategoriesAdapter categoriesAdapter; - PrefixUpdater lastUpdater = null; - MethodAUpdater methodAUpdater = null; ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2); private OnCategoriesSaveHandler onCategoriesSaveHandler; protected HashMap> categoriesCache; + private final Set results = new LinkedHashSet(); + PrefixUpdaterSub prefixUpdaterSub = null; + MethodAUpdaterSub methodAUpdaterSub = null; private ContentProviderClient client; @@ -110,6 +111,7 @@ public class CategorizationFragment extends SherlockFragment{ Category cat = Category.fromCursor(cursor); items.add(cat.getName()); } + cursor.close(); if (MwVolleyApi.GpsCatExists.getGpsCatExists() == true){ //Log.d(TAG, "GPS cats found in CategorizationFragment.java" + MwVolleyApi.getGpsCat().toString()); @@ -145,17 +147,10 @@ public class CategorizationFragment extends SherlockFragment{ } } - //TODO: This will set items twice in Adapter. Need to be able to 'add' items to adapter instead? Need to convert LinkedHashSet to ArrayList first? - //TODO: Maybe DON'T call this Adapter method. Instead make an add(items) method that will build up the LinkedHashSet. Then move this whole thing to bottom categoriesAdapter.setItems(items); categoriesAdapter.notifyDataSetInvalidated(); categoriesSearchInProgress.setVisibility(View.GONE); - /* - itemSet.addAll(items); - Log.d(TAG, "Item Set" + itemSet.toString()); - */ - if (categories.size() == 0) { if(TextUtils.isEmpty(filter)) { // If we found no recent cats, show the skip message! @@ -335,74 +330,74 @@ public class CategorizationFragment extends SherlockFragment{ return rootView; } + final CountDownLatch latch = new CountDownLatch(1); + class PrefixUpdaterSub extends PrefixUpdater { + + public PrefixUpdaterSub() { + super(CategorizationFragment.this); + } + + @Override + protected ArrayList doInBackground(Void... voids) { + ArrayList result = new ArrayList(); + try { + result = super.doInBackground(); + latch.await(); + } + catch (InterruptedException e) { + Log.w(TAG, e); + Thread.currentThread().interrupt(); + } + return result; + } + + @Override + protected void onPostExecute(ArrayList result) { + super.onPostExecute(result); + + results.addAll(result); + Log.d(TAG, "Prefix result: " + result); + categoriesAdapter.notifyDataSetChanged(); + } + } + + + class MethodAUpdaterSub extends MethodAUpdater { + + public MethodAUpdaterSub() { + super(CategorizationFragment.this); + } + + @Override + protected void onPostExecute(ArrayList result) { + results.clear(); + super.onPostExecute(result); + + results.addAll(result); + Log.d(TAG, "Method A result: " + result); + categoriesAdapter.notifyDataSetChanged(); + + latch.countDown(); + } + } private void startUpdatingCategoryList() { - - final CountDownLatch latch = new CountDownLatch(1); - - class PrefixUpdaterSub extends PrefixUpdater { - - public PrefixUpdaterSub() { - super(CategorizationFragment.this); - } - - @Override - protected ArrayList doInBackground(Void... voids) { - ArrayList result = new ArrayList(); - try { - result = super.doInBackground(); - latch.await(); - } - catch (InterruptedException e) { - Log.w(TAG, e); - } - return result; - } - - @Override - protected void onPostExecute(ArrayList result) { - super.onPostExecute(result); - - results.addAll(result); - categoriesAdapter.notifyDataSetChanged(); - } + if (prefixUpdaterSub != null) { + prefixUpdaterSub.cancel(true); } - - class MethodAUpdaterSub extends MethodAUpdater { - - public MethodAUpdaterSub() { - super(CategorizationFragment.this); - } - - @Override - protected void onPostExecute(ArrayList result) { - super.onPostExecute(result); - - results.clear(); - results.addAll(result); - categoriesAdapter.notifyDataSetChanged(); - - latch.countDown(); - } + if (methodAUpdaterSub != null) { + methodAUpdaterSub.cancel(true); } - if (lastUpdater != null) { - lastUpdater.cancel(true); - } - - if (methodAUpdater != null) { - methodAUpdater.cancel(true); - } - - - PrefixUpdaterSub prefixUpdaterSub = new PrefixUpdaterSub(); - MethodAUpdaterSub methodAUpdaterSub = new MethodAUpdaterSub(); + prefixUpdaterSub = new PrefixUpdaterSub(); + methodAUpdaterSub = new MethodAUpdaterSub(); Utils.executeAsyncTask(prefixUpdaterSub); Utils.executeAsyncTask(methodAUpdaterSub); + Log.d(TAG, "Final results: " + results); }