Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracted and tested the database interactions from Category #1023

Merged
merged 1 commit into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions app/src/main/java/fr/free/nrw/commons/CommonsApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import dagger.android.DaggerApplication;
import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.contributions.ContributionDao;
import fr.free.nrw.commons.data.Category;
import fr.free.nrw.commons.data.CategoryDao;
import fr.free.nrw.commons.data.DBOpenHelper;
import fr.free.nrw.commons.di.CommonsApplicationComponent;
import fr.free.nrw.commons.di.CommonsApplicationModule;
Expand All @@ -49,15 +49,15 @@ public class CommonsApplication extends DaggerApplication {
@Inject @Named("default_preferences") SharedPreferences defaultPrefs;
@Inject @Named("application_preferences") SharedPreferences applicationPrefs;
@Inject @Named("prefs") SharedPreferences otherPrefs;

public static final String DEFAULT_EDIT_SUMMARY = "Uploaded using Android Commons app";

public static final String FEEDBACK_EMAIL = "commons-app-android@googlegroups.com";

public static final String LOGS_PRIVATE_EMAIL = "commons-app-android-private@googlegroups.com";

public static final String FEEDBACK_EMAIL_SUBJECT = "Commons Android App (%s) Feedback";

private CommonsApplicationComponent component;
private RefWatcher refWatcher;

Expand Down Expand Up @@ -95,7 +95,7 @@ protected RefWatcher setupLeakCanary() {
}
return LeakCanary.install(this);
}

/**
* Provides a way to get member refWatcher
*
Expand All @@ -106,7 +106,7 @@ public static RefWatcher getRefWatcher(Context context) {
CommonsApplication application = (CommonsApplication) context.getApplicationContext();
return application.refWatcher;
}

/**
* Helps in injecting dependency library Dagger
* @return Dagger injector
Expand Down Expand Up @@ -169,7 +169,7 @@ private void updateAllDatabases() {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

ModifierSequence.Table.onDelete(db);
Category.Table.onDelete(db);
CategoryDao.Table.onDelete(db);
ContributionDao.Table.onDelete(db);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import dagger.android.support.DaggerFragment;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.data.Category;
import fr.free.nrw.commons.data.CategoryDao;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import fr.free.nrw.commons.upload.MwVolleyApi;
import fr.free.nrw.commons.utils.StringSortingUtils;
Expand Down Expand Up @@ -79,7 +80,7 @@ public class CategorizationFragment extends DaggerFragment {
private final CategoriesAdapterFactory adapterFactory = new CategoriesAdapterFactory(item -> {
if (item.isSelected()) {
selectedCategories.add(item);
updateCategoryCount(item, databaseClient);
updateCategoryCount(item);
} else {
selectedCategories.remove(item);
}
Expand Down Expand Up @@ -261,7 +262,7 @@ private Observable<CategoryItem> titleCategories() {
}

private Observable<CategoryItem> recentCategories() {
return Observable.fromIterable(Category.recentCategories(databaseClient, SEARCH_CATS_LIMIT))
return Observable.fromIterable(new CategoryDao(databaseClient).recentCategories(SEARCH_CATS_LIMIT))
.map(s -> new CategoryItem(s, false));
}

Expand Down Expand Up @@ -311,24 +312,17 @@ private boolean containsYear(String item) {
|| item.matches("(.*)needing(.*)") || item.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);
private void updateCategoryCount(CategoryItem item) {
CategoryDao categoryDao = new CategoryDao(databaseClient);
Category category = categoryDao.find(item.getName());

if (cat == null) {
// Newly used category...
cat = new Category();
cat.setName(name);
cat.setLastUsed(new Date());
cat.setTimesUsed(0);
// Newly used category...
if (category == null) {
category = new Category(null, item.getName(), new Date(), 0);
}

return cat;
category.incTimesUsed();
categoryDao.save(category);
}

public int getCurrentSelectedCount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import timber.log.Timber;

import static android.content.UriMatcher.NO_MATCH;
import static fr.free.nrw.commons.data.Category.Table.ALL_FIELDS;
import static fr.free.nrw.commons.data.Category.Table.COLUMN_ID;
import static fr.free.nrw.commons.data.Category.Table.TABLE_NAME;
import static fr.free.nrw.commons.data.CategoryDao.Table.ALL_FIELDS;
import static fr.free.nrw.commons.data.CategoryDao.Table.COLUMN_ID;
import static fr.free.nrw.commons.data.CategoryDao.Table.TABLE_NAME;

public class CategoryContentProvider extends ContentProvider {

Expand Down
Loading