From 84a5f0a221cd8ab81e49c0044caffe6a35350e28 Mon Sep 17 00:00:00 2001 From: Mikel Date: Sun, 6 Aug 2017 19:27:47 +0100 Subject: [PATCH] Move DB tasks to model/dao object --- .../category/CategorizationFragment.java | 95 +++---------------- .../category/CategoryContentProvider.java | 1 + .../commons/{category => data}/Category.java | 68 +++++++++++-- .../free/nrw/commons/data/DBOpenHelper.java | 1 - 4 files changed, 77 insertions(+), 88 deletions(-) rename app/src/main/java/fr/free/nrw/commons/{category => data}/Category.java (64%) 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 19b0cd822..39248c062 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 @@ -2,9 +2,7 @@ package fr.free.nrw.commons.category; import android.content.ContentProviderClient; import android.content.SharedPreferences; -import android.database.Cursor; import android.os.Bundle; -import android.os.RemoteException; import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.support.v7.app.AlertDialog; @@ -37,6 +35,7 @@ import butterknife.BindView; import butterknife.ButterKnife; import fr.free.nrw.commons.CommonsApplication; import fr.free.nrw.commons.R; +import fr.free.nrw.commons.data.Category; import fr.free.nrw.commons.mwapi.MediaWikiApi; import fr.free.nrw.commons.upload.MwVolleyApi; import io.reactivex.Observable; @@ -71,11 +70,11 @@ public class CategorizationFragment extends Fragment { private OnCategoriesSaveHandler onCategoriesSaveHandler; private HashMap> categoriesCache; private List selectedCategories = new ArrayList<>(); - private ContentProviderClient client; + private ContentProviderClient databaseClient; private final CategoriesAdapterFactory adapterFactory = new CategoriesAdapterFactory(item -> { if (item.isSelected()) { selectedCategories.add(item); - updateCategoryCount(item, client); + updateCategoryCount(item, databaseClient); } else { selectedCategories.remove(item); } @@ -84,37 +83,20 @@ public class CategorizationFragment extends Fragment { private void updateCategoryCount(CategoryItem item, ContentProviderClient client) { Category cat = lookupCategory(item.getName()); cat.incTimesUsed(); - - cat.setContentProviderClient(client); - cat.save(); + cat.save(client); } private Category lookupCategory(String name) { - Cursor cursor = null; - try { - cursor = client.query( - CategoryContentProvider.BASE_URI, - Category.Table.ALL_FIELDS, - Category.Table.COLUMN_NAME + "=?", - new String[]{name}, - null); - if (cursor != null && cursor.moveToFirst()) { - return Category.fromCursor(cursor); - } - } catch (RemoteException e) { - // This feels lazy, but to hell with checked exceptions. :) - throw new RuntimeException(e); - } finally { - if (cursor != null) { - cursor.close(); - } + 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); } - // Newly used category... - Category cat = new Category(); - cat.setName(name); - cat.setLastUsed(new Date()); - cat.setTimesUsed(0); return cat; } @@ -223,7 +205,7 @@ public class CategorizationFragment extends Fragment { @Override public void onDestroy() { super.onDestroy(); - client.release(); + databaseClient.release(); } @Override @@ -274,7 +256,7 @@ public class CategorizationFragment extends Fragment { setHasOptionsMenu(true); onCategoriesSaveHandler = (OnCategoriesSaveHandler) getActivity(); getActivity().setTitle(R.string.categories_activity_title); - client = getActivity().getContentResolver().acquireContentProviderClient(AUTHORITY); + databaseClient = getActivity().getContentResolver().acquireContentProviderClient(AUTHORITY); } private List getStringList(List input) { @@ -285,53 +267,6 @@ public class CategorizationFragment extends Fragment { return output; } - /** - * Retrieves category suggestions from title input - * - * @return a list containing title-related categories - */ - private List titleCatQuery(String title) { - MediaWikiApi api = CommonsApplication.getInstance().getMWApi(); - //URL https://commons.wikimedia.org/w/api.php?action=query&format=xml&list=search&srwhat=text&srenablerewrites=1&srnamespace=14&srlimit=10&srsearch= - try { - return api.searchTitles(SEARCH_CATS_LIMIT, title); - } catch (IOException e) { - Timber.e(e, "IO Exception: "); - return new ArrayList<>(); - } - } - - /** - * Retrieves recently-used categories - * - * @return a list containing recent categories - */ - private ArrayList recentCatQuery() { - ArrayList items = new ArrayList<>(); - Cursor cursor = null; - try { - cursor = client.query( - CategoryContentProvider.BASE_URI, - Category.Table.ALL_FIELDS, - null, - new String[]{}, - Category.Table.COLUMN_LAST_USED + " DESC"); - // fixme add a limit on the original query instead of falling out of the loop? - while (cursor != null && cursor.moveToNext() - && cursor.getPosition() < SEARCH_CATS_LIMIT) { - Category cat = Category.fromCursor(cursor); - items.add(cat.getName()); - } - } catch (RemoteException e) { - throw new RuntimeException(e); - } finally { - if (cursor != null) { - cursor.close(); - } - } - return items; - } - private Observable gpsCategories() { return Observable.fromIterable( MwVolleyApi.GpsCatExists.getGpsCatExists() ? @@ -351,7 +286,7 @@ public class CategorizationFragment extends Fragment { } private Observable recentCategories() { - return Observable.fromIterable(recentCatQuery()) + return Observable.fromIterable(Category.recentCategories(databaseClient, SEARCH_CATS_LIMIT)) .map(s -> new CategoryItem(s, false)); } diff --git a/app/src/main/java/fr/free/nrw/commons/category/CategoryContentProvider.java b/app/src/main/java/fr/free/nrw/commons/category/CategoryContentProvider.java index c95e17cf6..e75f1adcf 100644 --- a/app/src/main/java/fr/free/nrw/commons/category/CategoryContentProvider.java +++ b/app/src/main/java/fr/free/nrw/commons/category/CategoryContentProvider.java @@ -11,6 +11,7 @@ import android.support.annotation.NonNull; import android.text.TextUtils; import fr.free.nrw.commons.CommonsApplication; +import fr.free.nrw.commons.data.Category; import fr.free.nrw.commons.data.DBOpenHelper; import timber.log.Timber; diff --git a/app/src/main/java/fr/free/nrw/commons/category/Category.java b/app/src/main/java/fr/free/nrw/commons/data/Category.java similarity index 64% rename from app/src/main/java/fr/free/nrw/commons/category/Category.java rename to app/src/main/java/fr/free/nrw/commons/data/Category.java index 68dd9200e..a5c5b6d75 100644 --- a/app/src/main/java/fr/free/nrw/commons/category/Category.java +++ b/app/src/main/java/fr/free/nrw/commons/data/Category.java @@ -1,4 +1,4 @@ -package fr.free.nrw.commons.category; +package fr.free.nrw.commons.data; import android.content.ContentProviderClient; import android.content.ContentValues; @@ -6,11 +6,15 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.os.RemoteException; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import java.util.ArrayList; import java.util.Date; +import fr.free.nrw.commons.category.CategoryContentProvider; + public class Category { - private ContentProviderClient client; private Uri contentUri; private String name; @@ -54,11 +58,7 @@ public class Category { } // Database/content-provider stuff - public void setContentProviderClient(ContentProviderClient client) { - this.client = client; - } - - public void save() { + public void save(ContentProviderClient client) { try { if (contentUri == null) { contentUri = client.insert(CategoryContentProvider.BASE_URI, this.toContentValues()); @@ -88,6 +88,60 @@ public class Category { return c; } + public static @Nullable Category find(ContentProviderClient client, String name) { + Cursor cursor = null; + try { + cursor = client.query( + CategoryContentProvider.BASE_URI, + Category.Table.ALL_FIELDS, + Category.Table.COLUMN_NAME + "=?", + new String[]{name}, + null); + if (cursor != null && cursor.moveToFirst()) { + return Category.fromCursor(cursor); + } + } catch (RemoteException e) { + // This feels lazy, but to hell with checked exceptions. :) + throw new RuntimeException(e); + } finally { + if (cursor != null) { + cursor.close(); + } + } + return null; + } + + /** + * Retrieves recently-used categories + * + * @return a list containing recent categories + */ + public static @NonNull ArrayList recentCategories(ContentProviderClient client, int limit) { + ArrayList items = new ArrayList<>(); + Cursor cursor = null; + try { + cursor = client.query( + CategoryContentProvider.BASE_URI, + Category.Table.ALL_FIELDS, + null, + new String[]{}, + Category.Table.COLUMN_LAST_USED + " DESC"); + // fixme add a limit on the original query instead of falling out of the loop? + while (cursor != null && cursor.moveToNext() + && cursor.getPosition() < limit) { + Category cat = Category.fromCursor(cursor); + items.add(cat.getName()); + } + } catch (RemoteException e) { + throw new RuntimeException(e); + } finally { + if (cursor != null) { + cursor.close(); + } + } + return items; + } + public static class Table { public static final String TABLE_NAME = "categories"; diff --git a/app/src/main/java/fr/free/nrw/commons/data/DBOpenHelper.java b/app/src/main/java/fr/free/nrw/commons/data/DBOpenHelper.java index 22171857a..8ef9f336c 100644 --- a/app/src/main/java/fr/free/nrw/commons/data/DBOpenHelper.java +++ b/app/src/main/java/fr/free/nrw/commons/data/DBOpenHelper.java @@ -4,7 +4,6 @@ import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; -import fr.free.nrw.commons.category.Category; import fr.free.nrw.commons.contributions.Contribution; import fr.free.nrw.commons.modifications.ModifierSequence;