From ef1fc0c9d95dfd5ccee59806a853008169d324f5 Mon Sep 17 00:00:00 2001 From: Mikel Date: Sun, 6 Aug 2017 20:33:29 +0100 Subject: [PATCH] Improve code readability --- .../category/CategorizationFragment.java | 19 +++++++++++-------- .../nrw/commons/category/CategoryItem.java | 8 ++++++-- .../fr/free/nrw/commons/data/Category.java | 19 +++++++++++++++---- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java b/app/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java index 5521ee629..63532afb8 100644 --- a/app/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java @@ -234,8 +234,8 @@ public class CategorizationFragment extends Fragment { private Observable 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 " diff --git a/app/src/main/java/fr/free/nrw/commons/category/CategoryItem.java b/app/src/main/java/fr/free/nrw/commons/category/CategoryItem.java index 94a920427..f6bacfb51 100644 --- a/app/src/main/java/fr/free/nrw/commons/category/CategoryItem.java +++ b/app/src/main/java/fr/free/nrw/commons/category/CategoryItem.java @@ -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; diff --git a/app/src/main/java/fr/free/nrw/commons/data/Category.java b/app/src/main/java/fr/free/nrw/commons/data/Category.java index a5c5b6d75..757f6b691 100644 --- a/app/src/main/java/fr/free/nrw/commons/data/Category.java +++ b/app/src/main/java/fr/free/nrw/commons/data/Category.java @@ -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 recentCategories(ContentProviderClient client, int limit) { @@ -198,4 +208,5 @@ public class Category { } } } + //endregion }