From 0443f65c4a53ab2ec8399d3b95a04e0c2180f7c0 Mon Sep 17 00:00:00 2001 From: Mikel Date: Sun, 6 Aug 2017 20:11:27 +0100 Subject: [PATCH] Reorder to clarify --- .../category/CategorizationFragment.java | 135 +++++++++--------- .../nrw/commons/upload/ShareActivity.java | 2 +- 2 files changed, 69 insertions(+), 68 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 4ecb27a8b..5521ee629 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 @@ -68,6 +68,7 @@ public class CategorizationFragment extends Fragment { private HashMap> categoriesCache; private List selectedCategories = new ArrayList<>(); private ContentProviderClient databaseClient; + private final CategoriesAdapterFactory adapterFactory = new CategoriesAdapterFactory(item -> { if (item.isSelected()) { selectedCategories.add(item); @@ -77,26 +78,6 @@ public class CategorizationFragment extends Fragment { } }); - private void updateCategoryCount(CategoryItem item, ContentProviderClient client) { - Category cat = lookupCategory(item.getName()); - cat.incTimesUsed(); - cat.save(client); - } - - private Category lookupCategory(String name) { - Category cat = Category.find(databaseClient, name); - - if (cat == null) { - // Newly used category... - cat = new Category(); - cat.setName(name); - cat.setLastUsed(new Date()); - cat.setTimesUsed(0); - } - - return cat; - } - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -131,50 +112,6 @@ public class CategorizationFragment extends Fragment { return rootView; } - private void updateCategoryList(String filter) { - Observable.fromIterable(selectedCategories) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .doOnSubscribe(disposable -> { - categoriesSearchInProgress.setVisibility(View.VISIBLE); - categoriesNotFoundView.setVisibility(View.GONE); - categoriesSkip.setVisibility(View.GONE); - categoriesAdapter.clear(); - }) - .observeOn(Schedulers.io()) - .concatWith( - search(filter) - .mergeWith(search2(filter)) - .filter(categoryItem -> !selectedCategories.contains(categoryItem)) - .switchIfEmpty( - gpsCategories() - .concatWith(titleCategories()) - .concatWith(recentCategories()) - .filter(categoryItem -> !selectedCategories.contains(categoryItem)) - ) - ) - .filter(categoryItem -> !containsYear(categoryItem.getName())) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe( - s -> categoriesAdapter.add(s), - throwable -> Timber.e(throwable), - () -> { - categoriesAdapter.notifyDataSetChanged(); - categoriesSearchInProgress.setVisibility(View.GONE); - - if (categoriesAdapter.getItemCount() == 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); - } - } - } - ); - } - @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { menu.clear(); @@ -243,6 +180,50 @@ public class CategorizationFragment extends Fragment { databaseClient = getActivity().getContentResolver().acquireContentProviderClient(AUTHORITY); } + private void updateCategoryList(String filter) { + Observable.fromIterable(selectedCategories) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .doOnSubscribe(disposable -> { + categoriesSearchInProgress.setVisibility(View.VISIBLE); + categoriesNotFoundView.setVisibility(View.GONE); + categoriesSkip.setVisibility(View.GONE); + categoriesAdapter.clear(); + }) + .observeOn(Schedulers.io()) + .concatWith( + searchAll(filter) + .mergeWith(searchCategories(filter)) + .filter(categoryItem -> !selectedCategories.contains(categoryItem)) + .switchIfEmpty( + gpsCategories() + .concatWith(titleCategories()) + .concatWith(recentCategories()) + .filter(categoryItem -> !selectedCategories.contains(categoryItem)) + ) + ) + .filter(categoryItem -> !containsYear(categoryItem.getName())) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + s -> categoriesAdapter.add(s), + throwable -> Timber.e(throwable), + () -> { + categoriesAdapter.notifyDataSetChanged(); + categoriesSearchInProgress.setVisibility(View.GONE); + + if (categoriesAdapter.getItemCount() == 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); + } + } + } + ); + } + private List getStringList(List input) { List output = new ArrayList<>(); for (CategoryItem item : input) { @@ -273,7 +254,7 @@ public class CategorizationFragment extends Fragment { .map(s -> new CategoryItem(s, false)); } - private Observable search(String term) { + private Observable searchAll(String term) { //If user hasn't typed anything in yet, get GPS and recent items if (TextUtils.isEmpty(term)) { return Observable.empty(); @@ -291,7 +272,7 @@ public class CategorizationFragment extends Fragment { .map(name -> new CategoryItem(name, false)); } - private Observable search2(String term) { + private Observable searchCategories(String term) { //If user hasn't typed anything in yet, get GPS and recent items if (TextUtils.isEmpty(term)) { return Observable.empty(); @@ -319,6 +300,26 @@ public class CategorizationFragment extends Fragment { || items.matches("(.*)needing(.*)") || items.matches("(.*)taken on(.*)")); } + private void updateCategoryCount(CategoryItem item, ContentProviderClient client) { + Category cat = lookupCategory(item.getName()); + cat.incTimesUsed(); + cat.save(client); + } + + private Category lookupCategory(String name) { + Category cat = Category.find(databaseClient, name); + + if (cat == null) { + // Newly used category... + cat = new Category(); + cat.setName(name); + cat.setLastUsed(new Date()); + cat.setTimesUsed(0); + } + + return cat; + } + public int getCurrentSelectedCount() { return selectedCategories.size(); } @@ -336,7 +337,7 @@ public class CategorizationFragment extends Fragment { .show(); } - public void showConfirmationDialog() { + private void showConfirmationDialog() { new AlertDialog.Builder(getActivity()) .setMessage("Images without categories are rarely usable. " + "Are you sure you want to submit without selecting " diff --git a/app/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java b/app/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java index ecaa71aa5..42ec3cfe6 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java @@ -525,7 +525,7 @@ public class ShareActivity switch (item.getItemId()) { case android.R.id.home: if(categorizationFragment!=null && categorizationFragment.isVisible()) { - categorizationFragment.backButtonDialog(); + categorizationFragment.showBackButtonDialog(); } else { onBackPressed(); }