mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Merge pull request #320 from misaochan/refactor-categorizationfragment
Refactor CategorizationFragment.java
This commit is contained in:
commit
238698d336
3 changed files with 132 additions and 127 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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<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) {
|
||||
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<CategoryItem> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItems(ArrayList<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) getActivity().getLayoutInflater().inflate(R.layout.layout_categories_item, null);
|
||||
|
||||
} else {
|
||||
checkedView = (CheckedTextView) view;
|
||||
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;
|
||||
}
|
||||
|
||||
CategoryItem item = (CategoryItem) this.getItem(i);
|
||||
checkedView.setChecked(item.selected);
|
||||
checkedView.setText(item.name);
|
||||
checkedView.setTag(i);
|
||||
@Override
|
||||
protected void onPostExecute(ArrayList<String> result) {
|
||||
super.onPostExecute(result);
|
||||
|
||||
return checkedView;
|
||||
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();
|
||||
}
|
||||
|
||||
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<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
|
||||
public void onCreateOptionsMenu(Menu menu, android.view.MenuInflater inflater) {
|
||||
menu.clear();
|
||||
|
|
|
|||
|
|
@ -79,21 +79,6 @@ public class MethodAUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
|
|||
|
||||
@Override
|
||||
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
|
||||
MWApi api = CommonsApplication.createMWApi();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue