Improve code readability

This commit is contained in:
Mikel 2017-08-06 20:33:29 +01:00
parent 0443f65c4a
commit ef1fc0c9d9
3 changed files with 32 additions and 14 deletions

View file

@ -234,8 +234,8 @@ public class CategorizationFragment extends Fragment {
private Observable<CategoryItem> gpsCategories() {
return Observable.fromIterable(
MwVolleyApi.GpsCatExists.getGpsCatExists() ?
MwVolleyApi.getGpsCat() : new ArrayList<>())
MwVolleyApi.GpsCatExists.getGpsCatExists()
? MwVolleyApi.getGpsCat() : new ArrayList<>())
.map(name -> new CategoryItem(name, false));
}
@ -266,7 +266,7 @@ public class CategorizationFragment extends Fragment {
.map(name -> new CategoryItem(name, false));
}
//otherwise if user has typed something in that isn't in cache, search API for matching categories
//otherwise, search API for matching categories
return CommonsApplication.getInstance().getMWApi()
.allCategories(term, SEARCH_CATS_LIMIT)
.map(name -> new CategoryItem(name, false));
@ -283,7 +283,7 @@ public class CategorizationFragment extends Fragment {
.map(s -> new CategoryItem(s, false));
}
private boolean containsYear(String items) {
private boolean containsYear(String item) {
//Check for current and previous year to exclude these categories from removal
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
@ -293,11 +293,11 @@ public class CategorizationFragment extends Fragment {
String prevYearInString = String.valueOf(prevYear);
Timber.d("Previous year: %s", prevYearInString);
//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
//Check if item contains a 4-digit word anywhere within the string (.* is wildcard)
//And that item does not equal the current year or previous year
//And if it is an irrelevant category such as Media_needing_categories_as_of_16_June_2017(Issue #750)
return ((items.matches(".*(19|20)\\d{2}.*") && !items.contains(yearInString) && !items.contains(prevYearInString))
|| items.matches("(.*)needing(.*)") || items.matches("(.*)taken on(.*)"));
return ((item.matches(".*(19|20)\\d{2}.*") && !item.contains(yearInString) && !item.contains(prevYearInString))
|| item.matches("(.*)needing(.*)") || item.matches("(.*)taken on(.*)"));
}
private void updateCategoryCount(CategoryItem item, ContentProviderClient client) {
@ -324,6 +324,9 @@ public class CategorizationFragment extends Fragment {
return selectedCategories.size();
}
/**
* Show dialog asking for confirmation to leave without saving categories.
*/
public void showBackButtonDialog() {
new AlertDialog.Builder(getActivity())
.setMessage("Are you sure you want to go back? The image will not "

View file

@ -54,8 +54,12 @@ class CategoryItem implements Parcelable {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
CategoryItem that = (CategoryItem) o;

View file

@ -57,7 +57,12 @@ public class Category {
touch();
}
// Database/content-provider stuff
//region Database/content-provider stuff
/**
* Persist category.
* @param client ContentProviderClient to handle DB connection
*/
public void save(ContentProviderClient client) {
try {
if (contentUri == null) {
@ -78,7 +83,7 @@ public class Category {
return cv;
}
public static Category fromCursor(Cursor cursor) {
private static Category fromCursor(Cursor cursor) {
// Hardcoding column positions!
Category c = new Category();
c.contentUri = CategoryContentProvider.uriForId(cursor.getInt(0));
@ -88,6 +93,12 @@ public class Category {
return c;
}
/**
* Find persisted category in database, based on its name.
* @param client ContentProviderClient to handle DB connection
* @param name Category's name
* @return category from database, or null if not found
*/
public static @Nullable Category find(ContentProviderClient client, String name) {
Cursor cursor = null;
try {
@ -112,8 +123,7 @@ public class Category {
}
/**
* Retrieves recently-used categories
*
* Retrieve recently-used categories, ordered by descending date.
* @return a list containing recent categories
*/
public static @NonNull ArrayList<String> recentCategories(ContentProviderClient client, int limit) {
@ -198,4 +208,5 @@ public class Category {
}
}
}
//endregion
}