Refactored MethodAUpdater and CategoriesUpdater

This commit is contained in:
misaochan 2016-01-21 16:16:49 +13:00
parent 0c29a875dd
commit 44e82e411c

View file

@ -89,6 +89,71 @@ public class CategorizationFragment extends SherlockFragment{
} }
} }
private ArrayList<String> recentCatQuery() {
ArrayList<String> items = new ArrayList<String>();
ArrayList<String> mergedItems= new ArrayList<String>();
try {
Cursor cursor = client.query(
CategoryContentProvider.BASE_URI,
Category.Table.ALL_FIELDS,
null,
new String[]{},
Category.Table.COLUMN_LAST_USED + " DESC");
// fixme add a limit on the original query instead of falling out of the loop?
while (cursor.moveToNext() && cursor.getPosition() < SEARCH_CATS_LIMIT) {
Category cat = Category.fromCursor(cursor);
items.add(cat.getName());
}
if (MwVolleyApi.GpsCatExists.getGpsCatExists() == true){
//Log.d(TAG, "GPS cats found in CategorizationFragment.java" + MwVolleyApi.getGpsCat().toString());
List<String> gpsItems = new ArrayList<String>(MwVolleyApi.getGpsCat());
//Log.d(TAG, "GPS items: " + gpsItems.toString());
mergedItems.addAll(gpsItems);
}
mergedItems.addAll(items);
}
catch (RemoteException e) {
throw new RuntimeException(e);
}
//Log.d(TAG, "Merged items: " + mergedItems.toString());
return mergedItems;
}
private void setCatsAfterAsync(ArrayList<String> categories, String filter) {
ArrayList<CategoryItem> items = new ArrayList<CategoryItem>();
HashSet<String> existingKeys = new HashSet<String>();
for(CategoryItem item : categoriesAdapter.getItems()) {
if(item.selected) {
items.add(item);
existingKeys.add(item.name);
}
}
for(String category : categories) {
if(!existingKeys.contains(category)) {
items.add(new CategoryItem(category, false));
}
}
categoriesAdapter.setItems(items);
categoriesAdapter.notifyDataSetInvalidated();
categoriesSearchInProgress.setVisibility(View.GONE);
if (categories.size() == 0) {
if(TextUtils.isEmpty(filter)) {
// If we found no recent cats, show the skip message!
categoriesSkip.setVisibility(View.VISIBLE);
} else {
categoriesNotFoundView.setText(getString(R.string.categories_not_found, filter));
categoriesNotFoundView.setVisibility(View.VISIBLE);
}
} else {
categoriesList.smoothScrollToPosition(existingKeys.size());
}
}
private class MethodAUpdater extends AsyncTask<Void, Void, ArrayList<String>> { private class MethodAUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
@ -106,71 +171,13 @@ public class CategorizationFragment extends SherlockFragment{
@Override @Override
protected void onPostExecute(ArrayList<String> categories) { protected void onPostExecute(ArrayList<String> categories) {
super.onPostExecute(categories); super.onPostExecute(categories);
ArrayList<CategoryItem> items = new ArrayList<CategoryItem>(); setCatsAfterAsync(categories, filter);
HashSet<String> existingKeys = new HashSet<String>();
for(CategoryItem item : categoriesAdapter.getItems()) {
if(item.selected) {
items.add(item);
existingKeys.add(item.name);
}
}
for(String category : categories) {
if(!existingKeys.contains(category)) {
items.add(new CategoryItem(category, false));
}
}
categoriesAdapter.setItems(items);
categoriesAdapter.notifyDataSetInvalidated();
categoriesSearchInProgress.setVisibility(View.GONE);
if (categories.size() == 0) {
if(TextUtils.isEmpty(filter)) {
// If we found no recent cats, show the skip message!
categoriesSkip.setVisibility(View.VISIBLE);
} else {
categoriesNotFoundView.setText(getString(R.string.categories_not_found, filter));
categoriesNotFoundView.setVisibility(View.VISIBLE);
}
} else {
categoriesList.smoothScrollToPosition(existingKeys.size());
}
} }
@Override @Override
protected ArrayList<String> doInBackground(Void... voids) { protected ArrayList<String> doInBackground(Void... voids) {
if(TextUtils.isEmpty(filter)) { if(TextUtils.isEmpty(filter)) {
ArrayList<String> items = new ArrayList<String>(); return recentCatQuery();
ArrayList<String> mergedItems= new ArrayList<String>();
try {
Cursor cursor = client.query(
CategoryContentProvider.BASE_URI,
Category.Table.ALL_FIELDS,
null,
new String[]{},
Category.Table.COLUMN_LAST_USED + " DESC");
// fixme add a limit on the original query instead of falling out of the loop?
while (cursor.moveToNext() && cursor.getPosition() < SEARCH_CATS_LIMIT) {
Category cat = Category.fromCursor(cursor);
items.add(cat.getName());
}
if (MwVolleyApi.GpsCatExists.getGpsCatExists() == true){
//Log.d(TAG, "GPS cats found in CategorizationFragment.java" + MwVolleyApi.getGpsCat().toString());
List<String> gpsItems = new ArrayList<String>(MwVolleyApi.getGpsCat());
//Log.d(TAG, "GPS items: " + gpsItems.toString());
mergedItems.addAll(gpsItems);
}
mergedItems.addAll(items);
}
catch (RemoteException e) {
// faaaail
throw new RuntimeException(e);
}
//Log.d(TAG, "Merged items: " + mergedItems.toString());
return mergedItems;
} }
if(categoriesCache.containsKey(filter)) { if(categoriesCache.containsKey(filter)) {
@ -196,7 +203,6 @@ public class CategorizationFragment extends SherlockFragment{
throw new RuntimeException(e); throw new RuntimeException(e);
} }
ArrayList<ApiResult> categoryNodes = result.getNodes("/api/query/search/p/@title"); ArrayList<ApiResult> categoryNodes = result.getNodes("/api/query/search/p/@title");
for(ApiResult categoryNode: categoryNodes) { for(ApiResult categoryNode: categoryNodes) {
String cat = categoryNode.getDocument().getTextContent(); String cat = categoryNode.getDocument().getTextContent();
@ -205,7 +211,6 @@ public class CategorizationFragment extends SherlockFragment{
} }
categoriesCache.put(filter, categories); categoriesCache.put(filter, categories);
return categories; return categories;
} }
} }
@ -226,68 +231,7 @@ public class CategorizationFragment extends SherlockFragment{
@Override @Override
protected void onPostExecute(ArrayList<String> categories) { protected void onPostExecute(ArrayList<String> categories) {
super.onPostExecute(categories); super.onPostExecute(categories);
ArrayList<CategoryItem> items = new ArrayList<CategoryItem>(); setCatsAfterAsync(categories, filter);
HashSet<String> existingKeys = new HashSet<String>();
for(CategoryItem item : categoriesAdapter.getItems()) {
if(item.selected) {
items.add(item);
existingKeys.add(item.name);
}
}
for(String category : categories) {
if(!existingKeys.contains(category)) {
items.add(new CategoryItem(category, false));
}
}
categoriesAdapter.setItems(items);
categoriesAdapter.notifyDataSetInvalidated();
categoriesSearchInProgress.setVisibility(View.GONE);
if (categories.size() == 0) {
if(TextUtils.isEmpty(filter)) {
// If we found no recent cats, show the skip message!
categoriesSkip.setVisibility(View.VISIBLE);
} else {
categoriesNotFoundView.setText(getString(R.string.categories_not_found, filter));
categoriesNotFoundView.setVisibility(View.VISIBLE);
}
} else {
categoriesList.smoothScrollToPosition(existingKeys.size());
}
}
private ArrayList<String> recentCatQuery() {
ArrayList<String> items = new ArrayList<String>();
ArrayList<String> mergedItems= new ArrayList<String>();
try {
Cursor cursor = client.query(
CategoryContentProvider.BASE_URI,
Category.Table.ALL_FIELDS,
null,
new String[]{},
Category.Table.COLUMN_LAST_USED + " DESC");
// fixme add a limit on the original query instead of falling out of the loop?
while (cursor.moveToNext() && cursor.getPosition() < SEARCH_CATS_LIMIT) {
Category cat = Category.fromCursor(cursor);
items.add(cat.getName());
}
if (MwVolleyApi.GpsCatExists.getGpsCatExists() == true){
//Log.d(TAG, "GPS cats found in CategorizationFragment.java" + MwVolleyApi.getGpsCat().toString());
List<String> gpsItems = new ArrayList<String>(MwVolleyApi.getGpsCat());
//Log.d(TAG, "GPS items: " + gpsItems.toString());
mergedItems.addAll(gpsItems);
}
mergedItems.addAll(items);
}
catch (RemoteException e) {
throw new RuntimeException(e);
}
//Log.d(TAG, "Merged items: " + mergedItems.toString());
return mergedItems;
} }
@Override @Override
@ -322,9 +266,7 @@ public class CategorizationFragment extends SherlockFragment{
categories.add(categoryNode.getDocument().getTextContent()); categories.add(categoryNode.getDocument().getTextContent());
} }
categoriesCache.put(filter, categories); categoriesCache.put(filter, categories);
return categories; return categories;
} }
} }