mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Refactored MethodAUpdater and CategoriesUpdater
This commit is contained in:
parent
0c29a875dd
commit
44e82e411c
1 changed files with 68 additions and 126 deletions
|
|
@ -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>> {
|
||||
|
||||
|
|
@ -106,71 +171,13 @@ public class CategorizationFragment extends SherlockFragment{
|
|||
@Override
|
||||
protected void onPostExecute(ArrayList<String> categories) {
|
||||
super.onPostExecute(categories);
|
||||
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());
|
||||
}
|
||||
setCatsAfterAsync(categories, filter);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ArrayList<String> doInBackground(Void... voids) {
|
||||
if(TextUtils.isEmpty(filter)) {
|
||||
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) {
|
||||
// faaaail
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
//Log.d(TAG, "Merged items: " + mergedItems.toString());
|
||||
return mergedItems;
|
||||
return recentCatQuery();
|
||||
}
|
||||
|
||||
if(categoriesCache.containsKey(filter)) {
|
||||
|
|
@ -196,7 +203,6 @@ public class CategorizationFragment extends SherlockFragment{
|
|||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
ArrayList<ApiResult> categoryNodes = result.getNodes("/api/query/search/p/@title");
|
||||
for(ApiResult categoryNode: categoryNodes) {
|
||||
String cat = categoryNode.getDocument().getTextContent();
|
||||
|
|
@ -205,7 +211,6 @@ public class CategorizationFragment extends SherlockFragment{
|
|||
}
|
||||
|
||||
categoriesCache.put(filter, categories);
|
||||
|
||||
return categories;
|
||||
}
|
||||
}
|
||||
|
|
@ -226,68 +231,7 @@ public class CategorizationFragment extends SherlockFragment{
|
|||
@Override
|
||||
protected void onPostExecute(ArrayList<String> categories) {
|
||||
super.onPostExecute(categories);
|
||||
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 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;
|
||||
setCatsAfterAsync(categories, filter);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -322,9 +266,7 @@ public class CategorizationFragment extends SherlockFragment{
|
|||
categories.add(categoryNode.getDocument().getTextContent());
|
||||
}
|
||||
|
||||
|
||||
categoriesCache.put(filter, categories);
|
||||
|
||||
return categories;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue