mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Injected the CategoryDao where needed.
This commit is contained in:
parent
feb435304f
commit
1d3befcbd2
4 changed files with 35 additions and 19 deletions
|
|
@ -1,6 +1,5 @@
|
|||
package fr.free.nrw.commons.category;
|
||||
|
||||
import android.content.ContentProviderClient;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
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.KEYCODE_BACK;
|
||||
import static fr.free.nrw.commons.category.CategoryContentProvider.AUTHORITY;
|
||||
|
||||
/**
|
||||
* 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 @Named("default_preferences") SharedPreferences prefs;
|
||||
@Inject CategoryDao categoryDao;
|
||||
|
||||
private RVRendererAdapter<CategoryItem> categoriesAdapter;
|
||||
private OnCategoriesSaveHandler onCategoriesSaveHandler;
|
||||
private HashMap<String, ArrayList<String>> categoriesCache;
|
||||
private List<CategoryItem> selectedCategories = new ArrayList<>();
|
||||
private ContentProviderClient databaseClient;
|
||||
|
||||
private final CategoriesAdapterFactory adapterFactory = new CategoriesAdapterFactory(item -> {
|
||||
if (item.isSelected()) {
|
||||
|
|
@ -141,7 +139,6 @@ public class CategorizationFragment extends DaggerFragment {
|
|||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
databaseClient.release();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -179,7 +176,6 @@ public class CategorizationFragment extends DaggerFragment {
|
|||
setHasOptionsMenu(true);
|
||||
onCategoriesSaveHandler = (OnCategoriesSaveHandler) getActivity();
|
||||
getActivity().setTitle(R.string.categories_activity_title);
|
||||
databaseClient = getActivity().getContentResolver().acquireContentProviderClient(AUTHORITY);
|
||||
}
|
||||
|
||||
private void updateCategoryList(String filter) {
|
||||
|
|
@ -262,7 +258,7 @@ public class CategorizationFragment extends DaggerFragment {
|
|||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
|
|
@ -313,7 +309,6 @@ public class CategorizationFragment extends DaggerFragment {
|
|||
}
|
||||
|
||||
private void updateCategoryCount(CategoryItem item) {
|
||||
CategoryDao categoryDao = new CategoryDao(databaseClient);
|
||||
Category category = categoryDao.find(item.getName());
|
||||
|
||||
// Newly used category...
|
||||
|
|
|
|||
|
|
@ -12,25 +12,33 @@ import java.util.ArrayList;
|
|||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import javax.inject.Provider;
|
||||
|
||||
import fr.free.nrw.commons.category.CategoryContentProvider;
|
||||
|
||||
public class CategoryDao {
|
||||
|
||||
private final ContentProviderClient client;
|
||||
private final Provider<ContentProviderClient> clientProvider;
|
||||
|
||||
public CategoryDao(ContentProviderClient client) {
|
||||
this.client = client;
|
||||
@Inject
|
||||
public CategoryDao(@Named("category") Provider<ContentProviderClient> clientProvider) {
|
||||
this.clientProvider = clientProvider;
|
||||
}
|
||||
|
||||
public void save(Category category) {
|
||||
ContentProviderClient db = clientProvider.get();
|
||||
try {
|
||||
if (category.getContentUri() == null) {
|
||||
category.setContentUri(client.insert(CategoryContentProvider.BASE_URI, toContentValues(category)));
|
||||
category.setContentUri(db.insert(CategoryContentProvider.BASE_URI, toContentValues(category)));
|
||||
} else {
|
||||
client.update(category.getContentUri(), toContentValues(category), null, null);
|
||||
db.update(category.getContentUri(), toContentValues(category), null, null);
|
||||
}
|
||||
} catch (RemoteException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
db.release();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -43,8 +51,9 @@ public class CategoryDao {
|
|||
public @Nullable
|
||||
Category find(String name) {
|
||||
Cursor cursor = null;
|
||||
ContentProviderClient db = clientProvider.get();
|
||||
try {
|
||||
cursor = client.query(
|
||||
cursor = db.query(
|
||||
CategoryContentProvider.BASE_URI,
|
||||
Table.ALL_FIELDS,
|
||||
Table.COLUMN_NAME + "=?",
|
||||
|
|
@ -60,6 +69,7 @@ public class CategoryDao {
|
|||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
db.release();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
@ -73,8 +83,9 @@ public class CategoryDao {
|
|||
List<String> recentCategories(int limit) {
|
||||
List<String> items = new ArrayList<>();
|
||||
Cursor cursor = null;
|
||||
ContentProviderClient db = clientProvider.get();
|
||||
try {
|
||||
cursor = client.query(
|
||||
cursor = db.query(
|
||||
CategoryContentProvider.BASE_URI,
|
||||
Table.ALL_FIELDS,
|
||||
null,
|
||||
|
|
@ -91,6 +102,7 @@ public class CategoryDao {
|
|||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
db.release();
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package fr.free.nrw.commons.di;
|
||||
|
||||
import android.content.ContentProviderClient;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v4.util.LruCache;
|
||||
|
|
@ -26,6 +27,8 @@ import static android.content.Context.MODE_PRIVATE;
|
|||
@Module
|
||||
@SuppressWarnings({"WeakerAccess", "unused"})
|
||||
public class CommonsApplicationModule {
|
||||
public static final String CATEGORY_AUTHORITY = "fr.free.nrw.commons.categories.contentprovider";
|
||||
|
||||
private CommonsApplication application;
|
||||
|
||||
public CommonsApplicationModule(CommonsApplication application) {
|
||||
|
|
@ -37,6 +40,12 @@ public class CommonsApplicationModule {
|
|||
return new AccountUtil(application);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Named("category")
|
||||
public ContentProviderClient provideContentProviderClient() {
|
||||
return application.getContentResolver().acquireContentProviderClient(CATEGORY_AUTHORITY);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Named("application_preferences")
|
||||
public SharedPreferences providesApplicationSharedPreferences() {
|
||||
|
|
|
|||
|
|
@ -50,20 +50,20 @@ import static org.mockito.Mockito.when;
|
|||
public class CategoryDaoTest {
|
||||
|
||||
@Mock
|
||||
ContentProviderClient client;
|
||||
private ContentProviderClient client;
|
||||
@Mock
|
||||
SQLiteDatabase database;
|
||||
private SQLiteDatabase database;
|
||||
@Captor
|
||||
ArgumentCaptor<ContentValues> captor;
|
||||
private ArgumentCaptor<ContentValues> captor;
|
||||
@Captor
|
||||
ArgumentCaptor<String[]> queryCaptor;
|
||||
private ArgumentCaptor<String[]> queryCaptor;
|
||||
|
||||
private CategoryDao testObject;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
testObject = new CategoryDao(client);
|
||||
testObject = new CategoryDao(() -> client);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue