Injected the CategoryDao where needed.

This commit is contained in:
Paul Hawke 2018-01-04 21:45:44 -06:00
parent feb435304f
commit 1d3befcbd2
4 changed files with 35 additions and 19 deletions

View file

@ -1,6 +1,5 @@
package fr.free.nrw.commons.category; package fr.free.nrw.commons.category;
import android.content.ContentProviderClient;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
@ -48,7 +47,6 @@ import timber.log.Timber;
import static android.view.KeyEvent.ACTION_UP; import static android.view.KeyEvent.ACTION_UP;
import static android.view.KeyEvent.KEYCODE_BACK; import static android.view.KeyEvent.KEYCODE_BACK;
import static fr.free.nrw.commons.category.CategoryContentProvider.AUTHORITY;
/** /**
* Displays the category suggestion and selection screen. Category search is initiated here. * Displays the category suggestion and selection screen. Category search is initiated here.
@ -70,12 +68,12 @@ public class CategorizationFragment extends DaggerFragment {
@Inject MediaWikiApi mwApi; @Inject MediaWikiApi mwApi;
@Inject @Named("default_preferences") SharedPreferences prefs; @Inject @Named("default_preferences") SharedPreferences prefs;
@Inject CategoryDao categoryDao;
private RVRendererAdapter<CategoryItem> categoriesAdapter; private RVRendererAdapter<CategoryItem> categoriesAdapter;
private OnCategoriesSaveHandler onCategoriesSaveHandler; private OnCategoriesSaveHandler onCategoriesSaveHandler;
private HashMap<String, ArrayList<String>> categoriesCache; private HashMap<String, ArrayList<String>> categoriesCache;
private List<CategoryItem> selectedCategories = new ArrayList<>(); private List<CategoryItem> selectedCategories = new ArrayList<>();
private ContentProviderClient databaseClient;
private final CategoriesAdapterFactory adapterFactory = new CategoriesAdapterFactory(item -> { private final CategoriesAdapterFactory adapterFactory = new CategoriesAdapterFactory(item -> {
if (item.isSelected()) { if (item.isSelected()) {
@ -141,7 +139,6 @@ public class CategorizationFragment extends DaggerFragment {
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
databaseClient.release();
} }
@Override @Override
@ -179,7 +176,6 @@ public class CategorizationFragment extends DaggerFragment {
setHasOptionsMenu(true); setHasOptionsMenu(true);
onCategoriesSaveHandler = (OnCategoriesSaveHandler) getActivity(); onCategoriesSaveHandler = (OnCategoriesSaveHandler) getActivity();
getActivity().setTitle(R.string.categories_activity_title); getActivity().setTitle(R.string.categories_activity_title);
databaseClient = getActivity().getContentResolver().acquireContentProviderClient(AUTHORITY);
} }
private void updateCategoryList(String filter) { private void updateCategoryList(String filter) {
@ -262,7 +258,7 @@ public class CategorizationFragment extends DaggerFragment {
} }
private Observable<CategoryItem> recentCategories() { private Observable<CategoryItem> recentCategories() {
return Observable.fromIterable(new CategoryDao(databaseClient).recentCategories(SEARCH_CATS_LIMIT)) return Observable.fromIterable(categoryDao.recentCategories(SEARCH_CATS_LIMIT))
.map(s -> new CategoryItem(s, false)); .map(s -> new CategoryItem(s, false));
} }
@ -313,7 +309,6 @@ public class CategorizationFragment extends DaggerFragment {
} }
private void updateCategoryCount(CategoryItem item) { private void updateCategoryCount(CategoryItem item) {
CategoryDao categoryDao = new CategoryDao(databaseClient);
Category category = categoryDao.find(item.getName()); Category category = categoryDao.find(item.getName());
// Newly used category... // Newly used category...

View file

@ -12,25 +12,33 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import fr.free.nrw.commons.category.CategoryContentProvider; import fr.free.nrw.commons.category.CategoryContentProvider;
public class CategoryDao { public class CategoryDao {
private final ContentProviderClient client; private final Provider<ContentProviderClient> clientProvider;
public CategoryDao(ContentProviderClient client) { @Inject
this.client = client; public CategoryDao(@Named("category") Provider<ContentProviderClient> clientProvider) {
this.clientProvider = clientProvider;
} }
public void save(Category category) { public void save(Category category) {
ContentProviderClient db = clientProvider.get();
try { try {
if (category.getContentUri() == null) { if (category.getContentUri() == null) {
category.setContentUri(client.insert(CategoryContentProvider.BASE_URI, toContentValues(category))); category.setContentUri(db.insert(CategoryContentProvider.BASE_URI, toContentValues(category)));
} else { } else {
client.update(category.getContentUri(), toContentValues(category), null, null); db.update(category.getContentUri(), toContentValues(category), null, null);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} finally {
db.release();
} }
} }
@ -43,8 +51,9 @@ public class CategoryDao {
public @Nullable public @Nullable
Category find(String name) { Category find(String name) {
Cursor cursor = null; Cursor cursor = null;
ContentProviderClient db = clientProvider.get();
try { try {
cursor = client.query( cursor = db.query(
CategoryContentProvider.BASE_URI, CategoryContentProvider.BASE_URI,
Table.ALL_FIELDS, Table.ALL_FIELDS,
Table.COLUMN_NAME + "=?", Table.COLUMN_NAME + "=?",
@ -60,6 +69,7 @@ public class CategoryDao {
if (cursor != null) { if (cursor != null) {
cursor.close(); cursor.close();
} }
db.release();
} }
return null; return null;
} }
@ -73,8 +83,9 @@ public class CategoryDao {
List<String> recentCategories(int limit) { List<String> recentCategories(int limit) {
List<String> items = new ArrayList<>(); List<String> items = new ArrayList<>();
Cursor cursor = null; Cursor cursor = null;
ContentProviderClient db = clientProvider.get();
try { try {
cursor = client.query( cursor = db.query(
CategoryContentProvider.BASE_URI, CategoryContentProvider.BASE_URI,
Table.ALL_FIELDS, Table.ALL_FIELDS,
null, null,
@ -91,6 +102,7 @@ public class CategoryDao {
if (cursor != null) { if (cursor != null) {
cursor.close(); cursor.close();
} }
db.release();
} }
return items; return items;
} }

View file

@ -1,5 +1,6 @@
package fr.free.nrw.commons.di; package fr.free.nrw.commons.di;
import android.content.ContentProviderClient;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.util.LruCache; import android.support.v4.util.LruCache;
@ -26,6 +27,8 @@ import static android.content.Context.MODE_PRIVATE;
@Module @Module
@SuppressWarnings({"WeakerAccess", "unused"}) @SuppressWarnings({"WeakerAccess", "unused"})
public class CommonsApplicationModule { public class CommonsApplicationModule {
public static final String CATEGORY_AUTHORITY = "fr.free.nrw.commons.categories.contentprovider";
private CommonsApplication application; private CommonsApplication application;
public CommonsApplicationModule(CommonsApplication application) { public CommonsApplicationModule(CommonsApplication application) {
@ -37,6 +40,12 @@ public class CommonsApplicationModule {
return new AccountUtil(application); return new AccountUtil(application);
} }
@Provides
@Named("category")
public ContentProviderClient provideContentProviderClient() {
return application.getContentResolver().acquireContentProviderClient(CATEGORY_AUTHORITY);
}
@Provides @Provides
@Named("application_preferences") @Named("application_preferences")
public SharedPreferences providesApplicationSharedPreferences() { public SharedPreferences providesApplicationSharedPreferences() {

View file

@ -50,20 +50,20 @@ import static org.mockito.Mockito.when;
public class CategoryDaoTest { public class CategoryDaoTest {
@Mock @Mock
ContentProviderClient client; private ContentProviderClient client;
@Mock @Mock
SQLiteDatabase database; private SQLiteDatabase database;
@Captor @Captor
ArgumentCaptor<ContentValues> captor; private ArgumentCaptor<ContentValues> captor;
@Captor @Captor
ArgumentCaptor<String[]> queryCaptor; private ArgumentCaptor<String[]> queryCaptor;
private CategoryDao testObject; private CategoryDao testObject;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
testObject = new CategoryDao(client); testObject = new CategoryDao(() -> client);
} }
@Test @Test