mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Move DB tasks to model/dao object
This commit is contained in:
parent
0c0c814604
commit
84a5f0a221
4 changed files with 77 additions and 88 deletions
|
|
@ -2,9 +2,7 @@ package fr.free.nrw.commons.category;
|
||||||
|
|
||||||
import android.content.ContentProviderClient;
|
import android.content.ContentProviderClient;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.database.Cursor;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.RemoteException;
|
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v7.app.AlertDialog;
|
import android.support.v7.app.AlertDialog;
|
||||||
|
|
@ -37,6 +35,7 @@ import butterknife.BindView;
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
import fr.free.nrw.commons.CommonsApplication;
|
import fr.free.nrw.commons.CommonsApplication;
|
||||||
import fr.free.nrw.commons.R;
|
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.mwapi.MediaWikiApi;
|
||||||
import fr.free.nrw.commons.upload.MwVolleyApi;
|
import fr.free.nrw.commons.upload.MwVolleyApi;
|
||||||
import io.reactivex.Observable;
|
import io.reactivex.Observable;
|
||||||
|
|
@ -71,11 +70,11 @@ public class CategorizationFragment extends Fragment {
|
||||||
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 client;
|
private ContentProviderClient databaseClient;
|
||||||
private final CategoriesAdapterFactory adapterFactory = new CategoriesAdapterFactory(item -> {
|
private final CategoriesAdapterFactory adapterFactory = new CategoriesAdapterFactory(item -> {
|
||||||
if (item.isSelected()) {
|
if (item.isSelected()) {
|
||||||
selectedCategories.add(item);
|
selectedCategories.add(item);
|
||||||
updateCategoryCount(item, client);
|
updateCategoryCount(item, databaseClient);
|
||||||
} else {
|
} else {
|
||||||
selectedCategories.remove(item);
|
selectedCategories.remove(item);
|
||||||
}
|
}
|
||||||
|
|
@ -84,37 +83,20 @@ public class CategorizationFragment extends Fragment {
|
||||||
private void updateCategoryCount(CategoryItem item, ContentProviderClient client) {
|
private void updateCategoryCount(CategoryItem item, ContentProviderClient client) {
|
||||||
Category cat = lookupCategory(item.getName());
|
Category cat = lookupCategory(item.getName());
|
||||||
cat.incTimesUsed();
|
cat.incTimesUsed();
|
||||||
|
cat.save(client);
|
||||||
cat.setContentProviderClient(client);
|
|
||||||
cat.save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Category lookupCategory(String name) {
|
private Category lookupCategory(String name) {
|
||||||
Cursor cursor = null;
|
Category cat = Category.find(databaseClient, name);
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (cat == null) {
|
||||||
// Newly used category...
|
// Newly used category...
|
||||||
Category cat = new Category();
|
cat = new Category();
|
||||||
cat.setName(name);
|
cat.setName(name);
|
||||||
cat.setLastUsed(new Date());
|
cat.setLastUsed(new Date());
|
||||||
cat.setTimesUsed(0);
|
cat.setTimesUsed(0);
|
||||||
|
}
|
||||||
|
|
||||||
return cat;
|
return cat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -223,7 +205,7 @@ public class CategorizationFragment extends Fragment {
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
client.release();
|
databaseClient.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -274,7 +256,7 @@ public class CategorizationFragment extends Fragment {
|
||||||
setHasOptionsMenu(true);
|
setHasOptionsMenu(true);
|
||||||
onCategoriesSaveHandler = (OnCategoriesSaveHandler) getActivity();
|
onCategoriesSaveHandler = (OnCategoriesSaveHandler) getActivity();
|
||||||
getActivity().setTitle(R.string.categories_activity_title);
|
getActivity().setTitle(R.string.categories_activity_title);
|
||||||
client = getActivity().getContentResolver().acquireContentProviderClient(AUTHORITY);
|
databaseClient = getActivity().getContentResolver().acquireContentProviderClient(AUTHORITY);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> getStringList(List<CategoryItem> input) {
|
private List<String> getStringList(List<CategoryItem> input) {
|
||||||
|
|
@ -285,53 +267,6 @@ public class CategorizationFragment extends Fragment {
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves category suggestions from title input
|
|
||||||
*
|
|
||||||
* @return a list containing title-related categories
|
|
||||||
*/
|
|
||||||
private List<String> 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<String> recentCatQuery() {
|
|
||||||
ArrayList<String> 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<CategoryItem> gpsCategories() {
|
private Observable<CategoryItem> gpsCategories() {
|
||||||
return Observable.fromIterable(
|
return Observable.fromIterable(
|
||||||
MwVolleyApi.GpsCatExists.getGpsCatExists() ?
|
MwVolleyApi.GpsCatExists.getGpsCatExists() ?
|
||||||
|
|
@ -351,7 +286,7 @@ public class CategorizationFragment extends Fragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Observable<CategoryItem> recentCategories() {
|
private Observable<CategoryItem> recentCategories() {
|
||||||
return Observable.fromIterable(recentCatQuery())
|
return Observable.fromIterable(Category.recentCategories(databaseClient, SEARCH_CATS_LIMIT))
|
||||||
.map(s -> new CategoryItem(s, false));
|
.map(s -> new CategoryItem(s, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import android.support.annotation.NonNull;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import fr.free.nrw.commons.CommonsApplication;
|
import fr.free.nrw.commons.CommonsApplication;
|
||||||
|
import fr.free.nrw.commons.data.Category;
|
||||||
import fr.free.nrw.commons.data.DBOpenHelper;
|
import fr.free.nrw.commons.data.DBOpenHelper;
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package fr.free.nrw.commons.category;
|
package fr.free.nrw.commons.data;
|
||||||
|
|
||||||
import android.content.ContentProviderClient;
|
import android.content.ContentProviderClient;
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
|
|
@ -6,11 +6,15 @@ import android.database.Cursor;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import fr.free.nrw.commons.category.CategoryContentProvider;
|
||||||
|
|
||||||
public class Category {
|
public class Category {
|
||||||
private ContentProviderClient client;
|
|
||||||
private Uri contentUri;
|
private Uri contentUri;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
@ -54,11 +58,7 @@ public class Category {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Database/content-provider stuff
|
// Database/content-provider stuff
|
||||||
public void setContentProviderClient(ContentProviderClient client) {
|
public void save(ContentProviderClient client) {
|
||||||
this.client = client;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void save() {
|
|
||||||
try {
|
try {
|
||||||
if (contentUri == null) {
|
if (contentUri == null) {
|
||||||
contentUri = client.insert(CategoryContentProvider.BASE_URI, this.toContentValues());
|
contentUri = client.insert(CategoryContentProvider.BASE_URI, this.toContentValues());
|
||||||
|
|
@ -88,6 +88,60 @@ public class Category {
|
||||||
return c;
|
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<String> recentCategories(ContentProviderClient client, int limit) {
|
||||||
|
ArrayList<String> 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 class Table {
|
||||||
public static final String TABLE_NAME = "categories";
|
public static final String TABLE_NAME = "categories";
|
||||||
|
|
||||||
|
|
@ -4,7 +4,6 @@ import android.content.Context;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
import android.database.sqlite.SQLiteOpenHelper;
|
import android.database.sqlite.SQLiteOpenHelper;
|
||||||
|
|
||||||
import fr.free.nrw.commons.category.Category;
|
|
||||||
import fr.free.nrw.commons.contributions.Contribution;
|
import fr.free.nrw.commons.contributions.Contribution;
|
||||||
import fr.free.nrw.commons.modifications.ModifierSequence;
|
import fr.free.nrw.commons.modifications.ModifierSequence;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue