From c638a211d5b9aac6fb2688a6bd94b4732fcc6815 Mon Sep 17 00:00:00 2001 From: misaochan Date: Fri, 22 Jan 2016 21:12:59 +1300 Subject: [PATCH] Separating MethodAUpdater and PrefixUpdater into their own files --- .../nrw/commons/category/AsyncResponse.java | 4 + .../category/CategorizationFragment.java | 133 +----------------- .../nrw/commons/category/MethodAUpdater.java | 78 ++++++++++ .../nrw/commons/category/PrefixUpdater.java | 75 ++++++++++ 4 files changed, 164 insertions(+), 126 deletions(-) create mode 100644 commons/src/main/java/fr/free/nrw/commons/category/AsyncResponse.java create mode 100644 commons/src/main/java/fr/free/nrw/commons/category/MethodAUpdater.java create mode 100644 commons/src/main/java/fr/free/nrw/commons/category/PrefixUpdater.java diff --git a/commons/src/main/java/fr/free/nrw/commons/category/AsyncResponse.java b/commons/src/main/java/fr/free/nrw/commons/category/AsyncResponse.java new file mode 100644 index 000000000..229ba09ab --- /dev/null +++ b/commons/src/main/java/fr/free/nrw/commons/category/AsyncResponse.java @@ -0,0 +1,4 @@ +package fr.free.nrw.commons.category; + +public interface AsyncResponse { +} 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 736b3d654..7f5fa1b69 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 @@ -37,24 +37,24 @@ public class CategorizationFragment extends SherlockFragment{ } ListView categoriesList; - EditText categoriesFilter; + protected EditText categoriesFilter; ProgressBar categoriesSearchInProgress; TextView categoriesNotFoundView; TextView categoriesSkip; CategoriesAdapter categoriesAdapter; - CategoriesUpdater lastUpdater = null; + PrefixUpdater lastUpdater = null; MethodAUpdater methodAUpdater = null; ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2); private OnCategoriesSaveHandler onCategoriesSaveHandler; - private HashMap> categoriesCache; + protected HashMap> categoriesCache; LinkedHashSet itemSet = new LinkedHashSet(); private ContentProviderClient client; - private final int SEARCH_CATS_LIMIT = 25; + protected final int SEARCH_CATS_LIMIT = 25; private static final String TAG = CategorizationFragment.class.getName(); public static class CategoryItem implements Parcelable { @@ -91,7 +91,7 @@ public class CategorizationFragment extends SherlockFragment{ } } - private ArrayList recentCatQuery() { + protected ArrayList recentCatQuery() { ArrayList items = new ArrayList(); ArrayList mergedItems= new ArrayList(); @@ -125,127 +125,8 @@ public class CategorizationFragment extends SherlockFragment{ return mergedItems; } - private class MethodAUpdater extends AsyncTask> { - private String filter; - @Override - protected void onPreExecute() { - super.onPreExecute(); - filter = categoriesFilter.getText().toString(); - categoriesSearchInProgress.setVisibility(View.VISIBLE); - categoriesNotFoundView.setVisibility(View.GONE); - - categoriesSkip.setVisibility(View.GONE); - } - - @Override - protected void onPostExecute(ArrayList categories) { - super.onPostExecute(categories); - setCatsAfterAsync(categories, filter); - } - - @Override - protected ArrayList doInBackground(Void... voids) { - if(TextUtils.isEmpty(filter)) { - return recentCatQuery(); - } - - if(categoriesCache.containsKey(filter)) { - return categoriesCache.get(filter); - } - - MWApi api = CommonsApplication.createMWApi(); - ApiResult result; - ArrayList categories = new ArrayList(); - - //URL https://commons.wikimedia.org/w/api.php?action=query&format=xml&list=search&srwhat=text&srenablerewrites=1&srnamespace=14&srlimit=10&srsearch= - try { - result = api.action("query") - .param("format", "xml") - .param("list", "search") - .param("srwhat", "text") - .param("srnamespace", "14") - .param("srlimit", SEARCH_CATS_LIMIT) - .param("srsearch", filter) - .get(); - Log.d(TAG, "Method A URL filter" + result.toString()); - } catch (IOException e) { - throw new RuntimeException(e); - } - - ArrayList categoryNodes = result.getNodes("/api/query/search/p/@title"); - for(ApiResult categoryNode: categoryNodes) { - String cat = categoryNode.getDocument().getTextContent(); - String catString = cat.replace("Category:", ""); - categories.add(catString); - } - - categoriesCache.put(filter, categories); - return categories; - } - } - - private class CategoriesUpdater extends AsyncTask> { - - private String filter; - @Override - protected void onPreExecute() { - super.onPreExecute(); - filter = categoriesFilter.getText().toString(); - categoriesSearchInProgress.setVisibility(View.VISIBLE); - categoriesNotFoundView.setVisibility(View.GONE); - - categoriesSkip.setVisibility(View.GONE); - } - - @Override - protected void onPostExecute(ArrayList categories) { - super.onPostExecute(categories); - setCatsAfterAsync(categories, filter); - - - } - - @Override - protected ArrayList doInBackground(Void... voids) { - //If user hasn't typed anything in yet, get GPS and recent items - if(TextUtils.isEmpty(filter)) { - return recentCatQuery(); - } - - //if user types in something that is in cache, return cached category - if(categoriesCache.containsKey(filter)) { - return categoriesCache.get(filter); - } - - //otherwise if user has typed something in that isn't in cache, search API for matching categories - MWApi api = CommonsApplication.createMWApi(); - ApiResult result; - ArrayList categories = new ArrayList(); - try { - result = api.action("query") - .param("list", "allcategories") - .param("acprefix", filter) - .param("aclimit", SEARCH_CATS_LIMIT) - .get(); - Log.d(TAG, "Prefix URL filter" + result.toString()); - } catch (IOException e) { - throw new RuntimeException(e); - } - - ArrayList categoryNodes = result.getNodes("/api/query/allcategories/c"); - for(ApiResult categoryNode: categoryNodes) { - categories.add(categoryNode.getDocument().getTextContent()); - } - - categoriesCache.put(filter, categories); - return categories; - } - } - - - - private void setCatsAfterAsync(ArrayList categories, String filter) { + protected void setCatsAfterAsync(ArrayList categories, String filter) { ArrayList items = new ArrayList(); HashSet existingKeys = new HashSet(); @@ -461,7 +342,7 @@ public class CategorizationFragment extends SherlockFragment{ ArrayList itemList = new ArrayList(itemSet); methodAUpdater = new MethodAUpdater(); - lastUpdater = new CategoriesUpdater(); + lastUpdater = new PrefixUpdater(); Utils.executeAsyncTask(lastUpdater, executor); Utils.executeAsyncTask(methodAUpdater, executor); diff --git a/commons/src/main/java/fr/free/nrw/commons/category/MethodAUpdater.java b/commons/src/main/java/fr/free/nrw/commons/category/MethodAUpdater.java new file mode 100644 index 000000000..8b25dfcce --- /dev/null +++ b/commons/src/main/java/fr/free/nrw/commons/category/MethodAUpdater.java @@ -0,0 +1,78 @@ +package fr.free.nrw.commons.category; + + +import android.os.AsyncTask; +import android.text.TextUtils; +import android.util.Log; +import android.view.View; + +import org.mediawiki.api.ApiResult; +import org.mediawiki.api.MWApi; + +import java.io.IOException; +import java.util.ArrayList; + +import fr.free.nrw.commons.CommonsApplication; + +public class MethodAUpdater extends AsyncTask> { + + private String filter; + private static final String TAG = MethodAUpdater.class.getName(); + CategorizationFragment catFragment = new CategorizationFragment(); + + @Override + protected void onPreExecute() { + super.onPreExecute(); + filter = catFragment.categoriesFilter.getText().toString(); + catFragment.categoriesSearchInProgress.setVisibility(View.VISIBLE); + catFragment.categoriesNotFoundView.setVisibility(View.GONE); + + catFragment.categoriesSkip.setVisibility(View.GONE); + } + + @Override + protected void onPostExecute(ArrayList categories) { + super.onPostExecute(categories); + catFragment.setCatsAfterAsync(categories, filter); + } + + @Override + protected ArrayList doInBackground(Void... voids) { + if(TextUtils.isEmpty(filter)) { + return catFragment.recentCatQuery(); + } + + if(catFragment.categoriesCache.containsKey(filter)) { + return catFragment.categoriesCache.get(filter); + } + + MWApi api = CommonsApplication.createMWApi(); + ApiResult result; + ArrayList categories = new ArrayList(); + + //URL https://commons.wikimedia.org/w/api.php?action=query&format=xml&list=search&srwhat=text&srenablerewrites=1&srnamespace=14&srlimit=10&srsearch= + try { + result = api.action("query") + .param("format", "xml") + .param("list", "search") + .param("srwhat", "text") + .param("srnamespace", "14") + .param("srlimit", catFragment.SEARCH_CATS_LIMIT) + .param("srsearch", filter) + .get(); + Log.d(TAG, "Method A URL filter" + result.toString()); + } catch (IOException e) { + throw new RuntimeException(e); + } + + ArrayList categoryNodes = result.getNodes("/api/query/search/p/@title"); + for(ApiResult categoryNode: categoryNodes) { + String cat = categoryNode.getDocument().getTextContent(); + String catString = cat.replace("Category:", ""); + categories.add(catString); + } + + catFragment.categoriesCache.put(filter, categories); + return categories; + } +} diff --git a/commons/src/main/java/fr/free/nrw/commons/category/PrefixUpdater.java b/commons/src/main/java/fr/free/nrw/commons/category/PrefixUpdater.java new file mode 100644 index 000000000..6d6a1e36d --- /dev/null +++ b/commons/src/main/java/fr/free/nrw/commons/category/PrefixUpdater.java @@ -0,0 +1,75 @@ +package fr.free.nrw.commons.category; + +import android.os.AsyncTask; +import android.text.TextUtils; +import android.util.Log; +import android.view.View; + +import org.mediawiki.api.ApiResult; +import org.mediawiki.api.MWApi; + +import java.io.IOException; +import java.util.ArrayList; + +import fr.free.nrw.commons.CommonsApplication; + +public class PrefixUpdater extends AsyncTask> { + //TODO: Return type? + + private String filter; + private static final String TAG = PrefixUpdater.class.getName(); + CategorizationFragment catFragment = new CategorizationFragment(); + @Override + protected void onPreExecute() { + super.onPreExecute(); + filter = catFragment.categoriesFilter.getText().toString(); + catFragment.categoriesSearchInProgress.setVisibility(View.VISIBLE); + catFragment.categoriesNotFoundView.setVisibility(View.GONE); + + catFragment.categoriesSkip.setVisibility(View.GONE); + } + + @Override + protected void onPostExecute(ArrayList categories) { + super.onPostExecute(categories); + catFragment.setCatsAfterAsync(categories, filter); + //TODO: Return its own List + + } + + @Override + protected ArrayList doInBackground(Void... voids) { + //If user hasn't typed anything in yet, get GPS and recent items + if(TextUtils.isEmpty(filter)) { + return catFragment.recentCatQuery(); + } + + //if user types in something that is in cache, return cached category + if(catFragment.categoriesCache.containsKey(filter)) { + return catFragment.categoriesCache.get(filter); + } + + //otherwise if user has typed something in that isn't in cache, search API for matching categories + MWApi api = CommonsApplication.createMWApi(); + ApiResult result; + ArrayList categories = new ArrayList(); + try { + result = api.action("query") + .param("list", "allcategories") + .param("acprefix", filter) + .param("aclimit", catFragment.SEARCH_CATS_LIMIT) + .get(); + Log.d(TAG, "Prefix URL filter" + result.toString()); + } catch (IOException e) { + throw new RuntimeException(e); + } + + ArrayList categoryNodes = result.getNodes("/api/query/allcategories/c"); + for(ApiResult categoryNode: categoryNodes) { + categories.add(categoryNode.getDocument().getTextContent()); + } + + catFragment.categoriesCache.put(filter, categories); + return categories; + } +}