diff --git a/app/src/main/java/fr/free/nrw/commons/category/CategoriesAdapter.java b/app/src/main/java/fr/free/nrw/commons/category/CategoriesAdapter.java new file mode 100644 index 000000000..66818942d --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/category/CategoriesAdapter.java @@ -0,0 +1,67 @@ +package fr.free.nrw.commons.category; + + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.CheckedTextView; +import android.widget.TextView; + +import java.util.ArrayList; +import java.util.TreeSet; + +import fr.free.nrw.commons.R; + +public class CategoriesAdapter extends BaseAdapter { + + private Context context; + private LayoutInflater mInflater; + + private ArrayList items; + + public CategoriesAdapter(Context context, ArrayList items) { + this.context = context; + this.items = items; + mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + } + + public int getCount() { + return items.size(); + } + + public Object getItem(int i) { + return items.get(i); + } + + public ArrayList getItems() { + return items; + } + + public void setItems(ArrayList items) { + this.items = items; + } + + public long getItemId(int i) { + return i; + } + + public View getView(int i, View view, ViewGroup viewGroup) { + CheckedTextView checkedView; + + if(view == null) { + checkedView = (CheckedTextView) mInflater.inflate(R.layout.layout_categories_item, null); + + } else { + checkedView = (CheckedTextView) view; + } + + CategorizationFragment.CategoryItem item = (CategorizationFragment.CategoryItem) this.getItem(i); + checkedView.setChecked(item.selected); + checkedView.setText(item.name); + checkedView.setTag(i); + + return checkedView; + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java b/app/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java index 342f225c2..869311684 100644 --- a/app/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java @@ -39,6 +39,7 @@ import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import java.util.TreeSet; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -263,53 +264,77 @@ public class CategorizationFragment extends Fragment { } } - private class CategoriesAdapter extends BaseAdapter { - private Context context; - private ArrayList items; + /** + * Makes asynchronous calls to the Commons MediaWiki API via anonymous subclasses of + * 'MethodAUpdater' and 'PrefixUpdater'. Some of their methods are overridden in order to + * aggregate the results. A CountDownLatch is used to ensure that MethodA results are shown + * above Prefix results. + */ + private void requestSearchResults() { - private CategoriesAdapter(Context context, ArrayList items) { - this.context = context; - this.items = items; - } + final CountDownLatch latch = new CountDownLatch(1); - public int getCount() { - return items.size(); - } - - public Object getItem(int i) { - return items.get(i); - } - - public ArrayList getItems() { - return items; - } - - public void setItems(ArrayList items) { - this.items = items; - } - - public long getItemId(int i) { - return i; - } - - public View getView(int i, View view, ViewGroup viewGroup) { - CheckedTextView checkedView; - - if(view == null) { - checkedView = (CheckedTextView) getActivity().getLayoutInflater().inflate(R.layout.layout_categories_item, null); - - } else { - checkedView = (CheckedTextView) view; + prefixUpdaterSub = new PrefixUpdater(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; } - CategoryItem item = (CategoryItem) this.getItem(i); - checkedView.setChecked(item.selected); - checkedView.setText(item.name); - checkedView.setTag(i); + @Override + protected void onPostExecute(ArrayList result) { + super.onPostExecute(result); - return checkedView; + results.addAll(result); + Log.d(TAG, "Prefix result: " + result); + + String filter = categoriesFilter.getText().toString(); + ArrayList resultsList = new ArrayList(results); + categoriesCache.put(filter, resultsList); + Log.d(TAG, "Final results List: " + resultsList); + + categoriesAdapter.notifyDataSetChanged(); + setCatsAfterAsync(resultsList, filter); + } + }; + + methodAUpdaterSub = new MethodAUpdater(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(); + } + }; + Utils.executeAsyncTask(prefixUpdaterSub); + Utils.executeAsyncTask(methodAUpdaterSub); + } + + private void startUpdatingCategoryList() { + + if (prefixUpdaterSub != null) { + prefixUpdaterSub.cancel(true); } + + if (methodAUpdaterSub != null) { + methodAUpdaterSub.cancel(true); + } + + requestSearchResults(); } public int getCurrentSelectedCount() { @@ -434,78 +459,6 @@ public class CategorizationFragment extends Fragment { return rootView; } - /** - * Makes asynchronous calls to the Commons MediaWiki API via anonymous subclasses of - * 'MethodAUpdater' and 'PrefixUpdater'. Some of their methods are overridden in order to - * aggregate the results. A CountDownLatch is used to ensure that MethodA results are shown - * above Prefix results. - */ - private void requestSearchResults() { - - final CountDownLatch latch = new CountDownLatch(1); - - prefixUpdaterSub = new PrefixUpdater(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); - - String filter = categoriesFilter.getText().toString(); - ArrayList resultsList = new ArrayList(results); - categoriesCache.put(filter, resultsList); - Log.d(TAG, "Final results List: " + resultsList); - - categoriesAdapter.notifyDataSetChanged(); - setCatsAfterAsync(resultsList, filter); - } - }; - - methodAUpdaterSub = new MethodAUpdater(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(); - } - }; - Utils.executeAsyncTask(prefixUpdaterSub); - Utils.executeAsyncTask(methodAUpdaterSub); - } - - private void startUpdatingCategoryList() { - - if (prefixUpdaterSub != null) { - prefixUpdaterSub.cancel(true); - } - - if (methodAUpdaterSub != null) { - methodAUpdaterSub.cancel(true); - } - - requestSearchResults(); - } - @Override public void onCreateOptionsMenu(Menu menu, android.view.MenuInflater inflater) { menu.clear(); diff --git a/app/src/main/java/fr/free/nrw/commons/category/MethodAUpdater.java b/app/src/main/java/fr/free/nrw/commons/category/MethodAUpdater.java index 32f8660bf..bc9a8092d 100644 --- a/app/src/main/java/fr/free/nrw/commons/category/MethodAUpdater.java +++ b/app/src/main/java/fr/free/nrw/commons/category/MethodAUpdater.java @@ -79,21 +79,6 @@ public class MethodAUpdater extends AsyncTask> { @Override protected ArrayList doInBackground(Void... voids) { - //If user hasn't typed anything in yet, get GPS and recent items - if(TextUtils.isEmpty(filter)) { - ArrayList mergedItems = new ArrayList(catFragment.mergeItems()); - Log.d(TAG, "Merged items, waiting for filter"); - ArrayList filteredItems = new ArrayList(filterYears(mergedItems)); - return filteredItems; - } - - //if user types in something that is in cache, return cached category - if(catFragment.categoriesCache.containsKey(filter)) { - ArrayList cachedItems = new ArrayList(catFragment.categoriesCache.get(filter)); - Log.d(TAG, "Found cache items, waiting for filter"); - ArrayList filteredItems = new ArrayList(filterYears(cachedItems)); - return filteredItems; - } //otherwise if user has typed something in that isn't in cache, search API for matching categories MWApi api = CommonsApplication.createMWApi();