mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-29 05:43:55 +01:00
Merge pull request #311 from misaochan/filter-year-cats
Filter year categories (except current and previous year)
This commit is contained in:
commit
5c1fd5be05
3 changed files with 106 additions and 8 deletions
|
|
@ -41,6 +41,7 @@ import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
@ -217,11 +218,11 @@ public class CategorizationFragment extends Fragment {
|
||||||
Log.d(TAG, "Adding title items: " + titleItems);
|
Log.d(TAG, "Adding title items: " + titleItems);
|
||||||
mergedItems.addAll(recentItems);
|
mergedItems.addAll(recentItems);
|
||||||
Log.d(TAG, "Adding recent items: " + recentItems);
|
Log.d(TAG, "Adding recent items: " + recentItems);
|
||||||
|
|
||||||
//Needs to be an ArrayList and not a List unless we want to modify a big portion of preexisting code
|
//Needs to be an ArrayList and not a List unless we want to modify a big portion of preexisting code
|
||||||
ArrayList<String> mergedItemsList = new ArrayList<String>(mergedItems);
|
ArrayList<String> mergedItemsList = new ArrayList<String>(mergedItems);
|
||||||
Log.d(TAG, "Merged item list: " + mergedItemsList);
|
|
||||||
|
|
||||||
|
Log.d(TAG, "Merged item list: " + mergedItemsList);
|
||||||
return mergedItemsList;
|
return mergedItemsList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@ import org.mediawiki.api.MWApi;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
import fr.free.nrw.commons.CommonsApplication;
|
import fr.free.nrw.commons.CommonsApplication;
|
||||||
|
|
||||||
|
|
@ -38,16 +40,59 @@ public class MethodAUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
|
||||||
catFragment.categoriesSkip.setVisibility(View.GONE);
|
catFragment.categoriesSkip.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove categories that contain a year in them (starting with 19__ or 20__), except for this year
|
||||||
|
* and previous year
|
||||||
|
* Rationale: https://github.com/commons-app/apps-android-commons/issues/47
|
||||||
|
* @param items Unfiltered list of categories
|
||||||
|
* @return Filtered category list
|
||||||
|
*/
|
||||||
|
private ArrayList<String> filterYears(ArrayList<String> items) {
|
||||||
|
|
||||||
|
Iterator<String> iterator;
|
||||||
|
|
||||||
|
//Check for current and previous year to exclude these categories from removal
|
||||||
|
Calendar now = Calendar.getInstance();
|
||||||
|
int year = now.get(Calendar.YEAR);
|
||||||
|
String yearInString = String.valueOf(year);
|
||||||
|
Log.d(TAG, "Year: " + yearInString);
|
||||||
|
|
||||||
|
int prevYear = year - 1;
|
||||||
|
String prevYearInString = String.valueOf(prevYear);
|
||||||
|
Log.d(TAG, "Previous year: " + prevYearInString);
|
||||||
|
|
||||||
|
//Copy to Iterator to prevent ConcurrentModificationException when removing item
|
||||||
|
for(iterator = items.iterator(); iterator.hasNext();) {
|
||||||
|
String s = iterator.next();
|
||||||
|
|
||||||
|
//Check if s contains a 4-digit word anywhere within the string (.* is wildcard)
|
||||||
|
//And that s does not equal the current year or previous year
|
||||||
|
if(s.matches(".*(19|20)\\d{2}.*") && !s.contains(yearInString) && !s.contains(prevYearInString)) {
|
||||||
|
Log.d(TAG, "Filtering out year " + s);
|
||||||
|
iterator.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d(TAG, "Items: " + items.toString());
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
@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 user hasn't typed anything in yet, get GPS and recent items
|
||||||
if(TextUtils.isEmpty(filter)) {
|
if(TextUtils.isEmpty(filter)) {
|
||||||
return catFragment.mergeItems();
|
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 user types in something that is in cache, return cached category
|
||||||
if(catFragment.categoriesCache.containsKey(filter)) {
|
if(catFragment.categoriesCache.containsKey(filter)) {
|
||||||
return catFragment.categoriesCache.get(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
|
||||||
|
|
@ -79,6 +124,8 @@ public class MethodAUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
|
||||||
categories.add(catString);
|
categories.add(catString);
|
||||||
}
|
}
|
||||||
|
|
||||||
return categories;
|
Log.d(TAG, "Found categories from Method A search, waiting for filter");
|
||||||
|
ArrayList<String> filteredItems = new ArrayList<String>(filterYears(categories));
|
||||||
|
return filteredItems;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,16 @@ import org.mediawiki.api.MWApi;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import javax.net.ssl.SSLPeerUnverifiedException;
|
import javax.net.ssl.SSLPeerUnverifiedException;
|
||||||
|
|
||||||
import fr.free.nrw.commons.CommonsApplication;
|
import fr.free.nrw.commons.CommonsApplication;
|
||||||
|
|
||||||
|
import static android.R.id.list;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends asynchronous queries to the Commons MediaWiki API to retrieve categories that share the
|
* Sends asynchronous queries to the Commons MediaWiki API to retrieve categories that share the
|
||||||
* same prefix as the keyword typed in by the user. The 'acprefix' action-specific parameter is used
|
* same prefix as the keyword typed in by the user. The 'acprefix' action-specific parameter is used
|
||||||
|
|
@ -41,16 +46,59 @@ public class PrefixUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
|
||||||
catFragment.categoriesSkip.setVisibility(View.GONE);
|
catFragment.categoriesSkip.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove categories that contain a year in them (starting with 19__ or 20__), except for this year
|
||||||
|
* and previous year
|
||||||
|
* Rationale: https://github.com/commons-app/apps-android-commons/issues/47
|
||||||
|
* @param items Unfiltered list of categories
|
||||||
|
* @return Filtered category list
|
||||||
|
*/
|
||||||
|
private ArrayList<String> filterYears(ArrayList<String> items) {
|
||||||
|
|
||||||
|
Iterator<String> iterator;
|
||||||
|
|
||||||
|
//Check for current and previous year to exclude these categories from removal
|
||||||
|
Calendar now = Calendar.getInstance();
|
||||||
|
int year = now.get(Calendar.YEAR);
|
||||||
|
String yearInString = String.valueOf(year);
|
||||||
|
Log.d(TAG, "Year: " + yearInString);
|
||||||
|
|
||||||
|
int prevYear = year - 1;
|
||||||
|
String prevYearInString = String.valueOf(prevYear);
|
||||||
|
Log.d(TAG, "Previous year: " + prevYearInString);
|
||||||
|
|
||||||
|
//Copy to Iterator to prevent ConcurrentModificationException when removing item
|
||||||
|
for(iterator = items.iterator(); iterator.hasNext();) {
|
||||||
|
String s = iterator.next();
|
||||||
|
|
||||||
|
//Check if s contains a 4-digit word anywhere within the string (.* is wildcard)
|
||||||
|
//And that s does not equal the current year or previous year
|
||||||
|
if(s.matches(".*(19|20)\\d{2}.*") && !s.contains(yearInString) && !s.contains(prevYearInString)) {
|
||||||
|
Log.d(TAG, "Filtering out year " + s);
|
||||||
|
iterator.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d(TAG, "Items: " + items.toString());
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
@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 user hasn't typed anything in yet, get GPS and recent items
|
||||||
if(TextUtils.isEmpty(filter)) {
|
if(TextUtils.isEmpty(filter)) {
|
||||||
return catFragment.mergeItems();
|
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 user types in something that is in cache, return cached category
|
||||||
if(catFragment.categoriesCache.containsKey(filter)) {
|
if(catFragment.categoriesCache.containsKey(filter)) {
|
||||||
return catFragment.categoriesCache.get(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
|
||||||
|
|
@ -76,6 +124,8 @@ public class PrefixUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
|
||||||
categories.add(categoryNode.getDocument().getTextContent());
|
categories.add(categoryNode.getDocument().getTextContent());
|
||||||
}
|
}
|
||||||
|
|
||||||
return categories;
|
Log.d(TAG, "Found categories from Prefix search, waiting for filter");
|
||||||
|
ArrayList<String> filteredItems = new ArrayList<String>(filterYears(categories));
|
||||||
|
return filteredItems;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue