10
10
11
11
import java .io .IOException ;
12
12
import java .util .ArrayList ;
13
+ import java .util .Calendar ;
14
+ import java .util .Iterator ;
15
+ import java .util .List ;
13
16
14
17
import javax .net .ssl .SSLPeerUnverifiedException ;
15
18
16
19
import fr .free .nrw .commons .CommonsApplication ;
17
20
21
+ import static android .R .id .list ;
22
+
18
23
/**
19
24
* Sends asynchronous queries to the Commons MediaWiki API to retrieve categories that share the
20
25
* same prefix as the keyword typed in by the user. The 'acprefix' action-specific parameter is used
@@ -41,16 +46,59 @@ protected void onPreExecute() {
41
46
catFragment .categoriesSkip .setVisibility (View .GONE );
42
47
}
43
48
49
+ /**
50
+ * Remove categories that contain a year in them (starting with 19__ or 20__), except for this year
51
+ * and previous year
52
+ * Rationale: https://github.com/commons-app/apps-android-commons/issues/47
53
+ * @param items Unfiltered list of categories
54
+ * @return Filtered category list
55
+ */
56
+ private ArrayList <String > filterYears (ArrayList <String > items ) {
57
+
58
+ Iterator <String > iterator ;
59
+
60
+ //Check for current and previous year to exclude these categories from removal
61
+ Calendar now = Calendar .getInstance ();
62
+ int year = now .get (Calendar .YEAR );
63
+ String yearInString = String .valueOf (year );
64
+ Log .d (TAG , "Year: " + yearInString );
65
+
66
+ int prevYear = year - 1 ;
67
+ String prevYearInString = String .valueOf (prevYear );
68
+ Log .d (TAG , "Previous year: " + prevYearInString );
69
+
70
+ //Copy to Iterator to prevent ConcurrentModificationException when removing item
71
+ for (iterator = items .iterator (); iterator .hasNext ();) {
72
+ String s = iterator .next ();
73
+
74
+ //Check if s contains a 4-digit word anywhere within the string (.* is wildcard)
75
+ //And that s does not equal the current year or previous year
76
+ if (s .matches (".*(19|20)\\ d{2}.*" ) && !s .contains (yearInString ) && !s .contains (prevYearInString )) {
77
+ Log .d (TAG , "Filtering out year " + s );
78
+ iterator .remove ();
79
+ }
80
+ }
81
+
82
+ Log .d (TAG , "Items: " + items .toString ());
83
+ return items ;
84
+ }
85
+
44
86
@ Override
45
87
protected ArrayList <String > doInBackground (Void ... voids ) {
46
88
//If user hasn't typed anything in yet, get GPS and recent items
47
89
if (TextUtils .isEmpty (filter )) {
48
- return catFragment .mergeItems ();
90
+ ArrayList <String > mergedItems = new ArrayList <String >(catFragment .mergeItems ());
91
+ Log .d (TAG , "Merged items, waiting for filter" );
92
+ ArrayList <String > filteredItems = new ArrayList <String >(filterYears (mergedItems ));
93
+ return filteredItems ;
49
94
}
50
95
51
96
//if user types in something that is in cache, return cached category
52
97
if (catFragment .categoriesCache .containsKey (filter )) {
53
- return catFragment .categoriesCache .get (filter );
98
+ ArrayList <String > cachedItems = new ArrayList <String >(catFragment .categoriesCache .get (filter ));
99
+ Log .d (TAG , "Found cache items, waiting for filter" );
100
+ ArrayList <String > filteredItems = new ArrayList <String >(filterYears (cachedItems ));
101
+ return filteredItems ;
54
102
}
55
103
56
104
//otherwise if user has typed something in that isn't in cache, search API for matching categories
@@ -76,6 +124,8 @@ protected ArrayList<String> doInBackground(Void... voids) {
76
124
categories .add (categoryNode .getDocument ().getTextContent ());
77
125
}
78
126
79
- return categories ;
127
+ Log .d (TAG , "Found categories from Prefix search, waiting for filter" );
128
+ ArrayList <String > filteredItems = new ArrayList <String >(filterYears (categories ));
129
+ return filteredItems ;
80
130
}
81
131
}
0 commit comments