Merge pull request #320 from misaochan/refactor-categorizationfragment

Refactor CategorizationFragment.java
This commit is contained in:
Josephine Lim 2016-11-14 19:52:28 +13:00 committed by GitHub
commit 238698d336
3 changed files with 132 additions and 127 deletions

View file

@ -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<CategorizationFragment.CategoryItem> items;
public CategoriesAdapter(Context context, ArrayList<CategorizationFragment.CategoryItem> 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<CategorizationFragment.CategoryItem> getItems() {
return items;
}
public void setItems(ArrayList<CategorizationFragment.CategoryItem> 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;
}
}

View file

@ -39,6 +39,7 @@ import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -263,53 +264,77 @@ public class CategorizationFragment extends Fragment {
} }
} }
private class CategoriesAdapter extends BaseAdapter {
private Context context; /**
private ArrayList<CategoryItem> 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<CategoryItem> items) { final CountDownLatch latch = new CountDownLatch(1);
this.context = context;
this.items = items; prefixUpdaterSub = new PrefixUpdater(this) {
@Override
protected ArrayList<String> doInBackground(Void... voids) {
ArrayList<String> result = new ArrayList<String>();
try {
result = super.doInBackground();
latch.await();
}
catch (InterruptedException e) {
Log.w(TAG, e);
//Thread.currentThread().interrupt();
}
return result;
} }
public int getCount() { @Override
return items.size(); protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
results.addAll(result);
Log.d(TAG, "Prefix result: " + result);
String filter = categoriesFilter.getText().toString();
ArrayList<String> resultsList = new ArrayList<String>(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<String> 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);
} }
public Object getItem(int i) { private void startUpdatingCategoryList() {
return items.get(i);
if (prefixUpdaterSub != null) {
prefixUpdaterSub.cancel(true);
} }
public ArrayList<CategoryItem> getItems() { if (methodAUpdaterSub != null) {
return items; methodAUpdaterSub.cancel(true);
} }
public void setItems(ArrayList<CategoryItem> items) { requestSearchResults();
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;
}
CategoryItem item = (CategoryItem) this.getItem(i);
checkedView.setChecked(item.selected);
checkedView.setText(item.name);
checkedView.setTag(i);
return checkedView;
}
} }
public int getCurrentSelectedCount() { public int getCurrentSelectedCount() {
@ -434,78 +459,6 @@ public class CategorizationFragment extends Fragment {
return rootView; 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<String> doInBackground(Void... voids) {
ArrayList<String> result = new ArrayList<String>();
try {
result = super.doInBackground();
latch.await();
}
catch (InterruptedException e) {
Log.w(TAG, e);
//Thread.currentThread().interrupt();
}
return result;
}
@Override
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
results.addAll(result);
Log.d(TAG, "Prefix result: " + result);
String filter = categoriesFilter.getText().toString();
ArrayList<String> resultsList = new ArrayList<String>(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<String> 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 @Override
public void onCreateOptionsMenu(Menu menu, android.view.MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, android.view.MenuInflater inflater) {
menu.clear(); menu.clear();

View file

@ -79,21 +79,6 @@ public class MethodAUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
@Override @Override
protected ArrayList<String> doInBackground(Void... voids) { protected ArrayList<String> doInBackground(Void... voids) {
//If user hasn't typed anything in yet, get GPS and recent items
if(TextUtils.isEmpty(filter)) {
ArrayList<String> mergedItems = new ArrayList<String>(catFragment.mergeItems());
Log.d(TAG, "Merged items, waiting for filter");
ArrayList<String> filteredItems = new ArrayList<String>(filterYears(mergedItems));
return filteredItems;
}
//if user types in something that is in cache, return cached category
if(catFragment.categoriesCache.containsKey(filter)) {
ArrayList<String> cachedItems = new ArrayList<String>(catFragment.categoriesCache.get(filter));
Log.d(TAG, "Found cache items, waiting for filter");
ArrayList<String> filteredItems = new ArrayList<String>(filterYears(cachedItems));
return filteredItems;
}
//otherwise if user has typed something in that isn't in cache, search API for matching categories //otherwise if user has typed something in that isn't in cache, search API for matching categories
MWApi api = CommonsApplication.createMWApi(); MWApi api = CommonsApplication.createMWApi();