mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Convert BookmarkPicturesDao to kotlin and share some useful DB methods
This commit is contained in:
parent
e529320e98
commit
a8bf02a019
10 changed files with 250 additions and 275 deletions
|
|
@ -16,7 +16,7 @@ import com.facebook.imagepipeline.core.ImagePipelineConfig
|
||||||
import fr.free.nrw.commons.auth.LoginActivity
|
import fr.free.nrw.commons.auth.LoginActivity
|
||||||
import fr.free.nrw.commons.auth.SessionManager
|
import fr.free.nrw.commons.auth.SessionManager
|
||||||
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable
|
||||||
import fr.free.nrw.commons.category.CategoryDao
|
import fr.free.nrw.commons.category.CategoryDao
|
||||||
import fr.free.nrw.commons.concurrency.BackgroundPoolExceptionHandler
|
import fr.free.nrw.commons.concurrency.BackgroundPoolExceptionHandler
|
||||||
import fr.free.nrw.commons.concurrency.ThreadPoolService
|
import fr.free.nrw.commons.concurrency.ThreadPoolService
|
||||||
|
|
@ -256,7 +256,7 @@ class CommonsApplication : MultiDexApplication() {
|
||||||
} catch (e: SQLiteException) {
|
} catch (e: SQLiteException) {
|
||||||
Timber.e(e)
|
Timber.e(e)
|
||||||
}
|
}
|
||||||
BookmarkPicturesDao.Table.onDelete(db)
|
BookmarksTable.onDelete(db)
|
||||||
BookmarkItemsTable.onDelete(db)
|
BookmarkItemsTable.onDelete(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,9 @@ import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable.COLUMN_IS_SELECTED
|
||||||
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable.COLUMN_NAME
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable.COLUMN_NAME
|
||||||
import fr.free.nrw.commons.category.CategoryItem
|
import fr.free.nrw.commons.category.CategoryItem
|
||||||
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
|
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
|
||||||
|
import fr.free.nrw.commons.utils.arrayToString
|
||||||
|
import fr.free.nrw.commons.utils.getString
|
||||||
|
import fr.free.nrw.commons.utils.getStringArray
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Named
|
import javax.inject.Named
|
||||||
import javax.inject.Provider
|
import javax.inject.Provider
|
||||||
|
|
@ -156,13 +159,6 @@ class BookmarkItemsDao @Inject constructor(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Cursor.getStringArray(name: String): List<String> =
|
|
||||||
stringToArray(getString(name))
|
|
||||||
|
|
||||||
@SuppressLint("Range")
|
|
||||||
private fun Cursor.getString(name: String): String =
|
|
||||||
getString(getColumnIndex(name))
|
|
||||||
|
|
||||||
private fun convertToCategoryItems(
|
private fun convertToCategoryItems(
|
||||||
categoryNameList: List<String>,
|
categoryNameList: List<String>,
|
||||||
categoryDescriptionList: List<String>,
|
categoryDescriptionList: List<String>,
|
||||||
|
|
@ -182,26 +178,6 @@ class BookmarkItemsDao @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts string to List
|
|
||||||
* @param listString comma separated single string from of list items
|
|
||||||
* @return List of string
|
|
||||||
*/
|
|
||||||
private fun stringToArray(listString: String?): List<String> {
|
|
||||||
if (listString.isNullOrEmpty()) return emptyList();
|
|
||||||
val elements = listString.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
|
|
||||||
return listOf(*elements)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts string to List
|
|
||||||
* @param list list of items
|
|
||||||
* @return string comma separated single string of items
|
|
||||||
*/
|
|
||||||
private fun arrayToString(list: List<String?>?): String? {
|
|
||||||
return list?.joinToString(",")
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Takes data from DepictedItem and create a content value object
|
* Takes data from DepictedItem and create a content value object
|
||||||
* @param depictedItem depicted item
|
* @param depictedItem depicted item
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,11 @@ import android.content.ContentValues
|
||||||
import android.database.Cursor
|
import android.database.Cursor
|
||||||
import android.database.sqlite.SQLiteQueryBuilder
|
import android.database.sqlite.SQLiteQueryBuilder
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.text.TextUtils
|
|
||||||
import fr.free.nrw.commons.BuildConfig
|
import fr.free.nrw.commons.BuildConfig
|
||||||
import fr.free.nrw.commons.di.CommonsDaggerContentProvider
|
import fr.free.nrw.commons.di.CommonsDaggerContentProvider
|
||||||
import androidx.core.net.toUri
|
import androidx.core.net.toUri
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao.Table.COLUMN_MEDIA_NAME
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.COLUMN_MEDIA_NAME
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao.Table.TABLE_NAME
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.TABLE_NAME
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles private storage for Bookmark pictures
|
* Handles private storage for Bookmark pictures
|
||||||
|
|
|
||||||
|
|
@ -1,227 +0,0 @@
|
||||||
package fr.free.nrw.commons.bookmarks.pictures;
|
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.content.ContentProviderClient;
|
|
||||||
import android.content.ContentValues;
|
|
||||||
import android.database.Cursor;
|
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
|
||||||
import android.os.RemoteException;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import javax.inject.Named;
|
|
||||||
import javax.inject.Provider;
|
|
||||||
import javax.inject.Singleton;
|
|
||||||
|
|
||||||
import fr.free.nrw.commons.bookmarks.models.Bookmark;
|
|
||||||
|
|
||||||
import static fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesContentProvider.BASE_URI;
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
public class BookmarkPicturesDao {
|
|
||||||
|
|
||||||
private final Provider<ContentProviderClient> clientProvider;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public BookmarkPicturesDao(@Named("bookmarks") Provider<ContentProviderClient> clientProvider) {
|
|
||||||
this.clientProvider = clientProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Find all persisted pictures bookmarks on database
|
|
||||||
*
|
|
||||||
* @return list of bookmarks
|
|
||||||
*/
|
|
||||||
@NonNull
|
|
||||||
public List<Bookmark> getAllBookmarks() {
|
|
||||||
List<Bookmark> items = new ArrayList<>();
|
|
||||||
Cursor cursor = null;
|
|
||||||
ContentProviderClient db = clientProvider.get();
|
|
||||||
try {
|
|
||||||
cursor = db.query(
|
|
||||||
BookmarkPicturesContentProvider.BASE_URI,
|
|
||||||
Table.ALL_FIELDS,
|
|
||||||
null,
|
|
||||||
new String[]{},
|
|
||||||
null);
|
|
||||||
while (cursor != null && cursor.moveToNext()) {
|
|
||||||
items.add(fromCursor(cursor));
|
|
||||||
}
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
if (cursor != null) {
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
return items;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Look for a bookmark in database and in order to insert or delete it
|
|
||||||
*
|
|
||||||
* @param bookmark : Bookmark object
|
|
||||||
* @return boolean : is bookmark now fav ?
|
|
||||||
*/
|
|
||||||
public boolean updateBookmark(Bookmark bookmark) {
|
|
||||||
boolean bookmarkExists = findBookmark(bookmark);
|
|
||||||
if (bookmarkExists) {
|
|
||||||
deleteBookmark(bookmark);
|
|
||||||
} else {
|
|
||||||
addBookmark(bookmark);
|
|
||||||
}
|
|
||||||
return !bookmarkExists;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add a Bookmark to database
|
|
||||||
*
|
|
||||||
* @param bookmark : Bookmark to add
|
|
||||||
*/
|
|
||||||
private void addBookmark(Bookmark bookmark) {
|
|
||||||
ContentProviderClient db = clientProvider.get();
|
|
||||||
try {
|
|
||||||
db.insert(BASE_URI, toContentValues(bookmark));
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete a bookmark from database
|
|
||||||
*
|
|
||||||
* @param bookmark : Bookmark to delete
|
|
||||||
*/
|
|
||||||
private void deleteBookmark(Bookmark bookmark) {
|
|
||||||
ContentProviderClient db = clientProvider.get();
|
|
||||||
try {
|
|
||||||
if (bookmark.getContentUri() == null) {
|
|
||||||
throw new RuntimeException("tried to delete item with no content URI");
|
|
||||||
} else {
|
|
||||||
db.delete(bookmark.getContentUri(), null, null);
|
|
||||||
}
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Find a bookmark from database based on its name
|
|
||||||
*
|
|
||||||
* @param bookmark : Bookmark to find
|
|
||||||
* @return boolean : is bookmark in database ?
|
|
||||||
*/
|
|
||||||
public boolean findBookmark(Bookmark bookmark) {
|
|
||||||
if (bookmark == null) {//Avoiding NPE's
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Cursor cursor = null;
|
|
||||||
ContentProviderClient db = clientProvider.get();
|
|
||||||
try {
|
|
||||||
cursor = db.query(
|
|
||||||
BookmarkPicturesContentProvider.BASE_URI,
|
|
||||||
Table.ALL_FIELDS,
|
|
||||||
Table.COLUMN_MEDIA_NAME + "=?",
|
|
||||||
new String[]{bookmark.getMediaName()},
|
|
||||||
null);
|
|
||||||
if (cursor != null && cursor.moveToFirst()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
// This feels lazy, but to hell with checked exceptions. :)
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
if (cursor != null) {
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressLint("Range")
|
|
||||||
@NonNull
|
|
||||||
Bookmark fromCursor(Cursor cursor) {
|
|
||||||
String fileName = cursor.getString(cursor.getColumnIndex(Table.COLUMN_MEDIA_NAME));
|
|
||||||
return new Bookmark(
|
|
||||||
fileName,
|
|
||||||
cursor.getString(cursor.getColumnIndex(Table.COLUMN_CREATOR)),
|
|
||||||
BookmarkPicturesContentProvider.uriForName(fileName)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private ContentValues toContentValues(Bookmark bookmark) {
|
|
||||||
ContentValues cv = new ContentValues();
|
|
||||||
cv.put(BookmarkPicturesDao.Table.COLUMN_MEDIA_NAME, bookmark.getMediaName());
|
|
||||||
cv.put(BookmarkPicturesDao.Table.COLUMN_CREATOR, bookmark.getMediaCreator());
|
|
||||||
return cv;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static class Table {
|
|
||||||
public static final String TABLE_NAME = "bookmarks";
|
|
||||||
|
|
||||||
public static final String COLUMN_MEDIA_NAME = "media_name";
|
|
||||||
public static final String COLUMN_CREATOR = "media_creator";
|
|
||||||
|
|
||||||
// NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES.
|
|
||||||
public static final String[] ALL_FIELDS = {
|
|
||||||
COLUMN_MEDIA_NAME,
|
|
||||||
COLUMN_CREATOR
|
|
||||||
};
|
|
||||||
|
|
||||||
public static final String DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS " + TABLE_NAME;
|
|
||||||
|
|
||||||
public static final String CREATE_TABLE_STATEMENT = "CREATE TABLE " + TABLE_NAME + " ("
|
|
||||||
+ COLUMN_MEDIA_NAME + " STRING PRIMARY KEY,"
|
|
||||||
+ COLUMN_CREATOR + " STRING"
|
|
||||||
+ ");";
|
|
||||||
|
|
||||||
public static void onCreate(SQLiteDatabase db) {
|
|
||||||
db.execSQL(CREATE_TABLE_STATEMENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void onDelete(SQLiteDatabase db) {
|
|
||||||
db.execSQL(DROP_TABLE_STATEMENT);
|
|
||||||
onCreate(db);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void onUpdate(SQLiteDatabase db, int from, int to) {
|
|
||||||
if (from == to) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (from < 7) {
|
|
||||||
// doesn't exist yet
|
|
||||||
from++;
|
|
||||||
onUpdate(db, from, to);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (from == 7) {
|
|
||||||
// table added in version 8
|
|
||||||
onCreate(db);
|
|
||||||
from++;
|
|
||||||
onUpdate(db, from, to);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (from == 8) {
|
|
||||||
from++;
|
|
||||||
onUpdate(db, from, to);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,141 @@
|
||||||
|
package fr.free.nrw.commons.bookmarks.pictures
|
||||||
|
|
||||||
|
import android.content.ContentProviderClient
|
||||||
|
import android.content.ContentValues
|
||||||
|
import android.database.Cursor
|
||||||
|
import android.os.RemoteException
|
||||||
|
import androidx.core.content.contentValuesOf
|
||||||
|
import fr.free.nrw.commons.bookmarks.models.Bookmark
|
||||||
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesContentProvider.Companion.BASE_URI
|
||||||
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesContentProvider.Companion.uriForName
|
||||||
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.ALL_FIELDS
|
||||||
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.COLUMN_CREATOR
|
||||||
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.COLUMN_MEDIA_NAME
|
||||||
|
import fr.free.nrw.commons.utils.getString
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Named
|
||||||
|
import javax.inject.Provider
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class BookmarkPicturesDao @Inject constructor(
|
||||||
|
@param:Named("bookmarks") private val clientProvider: Provider<ContentProviderClient>
|
||||||
|
) {
|
||||||
|
/**
|
||||||
|
* Find all persisted pictures bookmarks on database
|
||||||
|
*
|
||||||
|
* @return list of bookmarks
|
||||||
|
*/
|
||||||
|
fun getAllBookmarks(): List<Bookmark> {
|
||||||
|
val items: MutableList<Bookmark> = mutableListOf()
|
||||||
|
var cursor: Cursor? = null
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
cursor = db.query(
|
||||||
|
BASE_URI, ALL_FIELDS, null, arrayOf(), null
|
||||||
|
)
|
||||||
|
while (cursor != null && cursor.moveToNext()) {
|
||||||
|
items.add(fromCursor(cursor))
|
||||||
|
}
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
cursor?.close()
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
return items
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Look for a bookmark in database and in order to insert or delete it
|
||||||
|
*
|
||||||
|
* @param bookmark : Bookmark object
|
||||||
|
* @return boolean : is bookmark now fav ?
|
||||||
|
*/
|
||||||
|
fun updateBookmark(bookmark: Bookmark): Boolean {
|
||||||
|
val bookmarkExists = findBookmark(bookmark)
|
||||||
|
if (bookmarkExists) {
|
||||||
|
deleteBookmark(bookmark)
|
||||||
|
} else {
|
||||||
|
addBookmark(bookmark)
|
||||||
|
}
|
||||||
|
return !bookmarkExists
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a Bookmark to database
|
||||||
|
*
|
||||||
|
* @param bookmark : Bookmark to add
|
||||||
|
*/
|
||||||
|
private fun addBookmark(bookmark: Bookmark) {
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
db.insert(BASE_URI, toContentValues(bookmark))
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a bookmark from database
|
||||||
|
*
|
||||||
|
* @param bookmark : Bookmark to delete
|
||||||
|
*/
|
||||||
|
private fun deleteBookmark(bookmark: Bookmark) {
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
if (bookmark.contentUri == null) {
|
||||||
|
throw RuntimeException("tried to delete item with no content URI")
|
||||||
|
} else {
|
||||||
|
db.delete(bookmark.contentUri!!, null, null)
|
||||||
|
}
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a bookmark from database based on its name
|
||||||
|
*
|
||||||
|
* @param bookmark : Bookmark to find
|
||||||
|
* @return boolean : is bookmark in database ?
|
||||||
|
*/
|
||||||
|
fun findBookmark(bookmark: Bookmark?): Boolean {
|
||||||
|
if (bookmark == null) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var cursor: Cursor? = null
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
cursor = db.query(
|
||||||
|
BASE_URI, ALL_FIELDS, "$COLUMN_MEDIA_NAME=?", arrayOf(bookmark.mediaName), null
|
||||||
|
)
|
||||||
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
cursor?.close()
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
fun fromCursor(cursor: Cursor): Bookmark {
|
||||||
|
val fileName = cursor.getString(COLUMN_MEDIA_NAME)
|
||||||
|
return Bookmark(
|
||||||
|
fileName, cursor.getString(COLUMN_CREATOR), uriForName(fileName)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun toContentValues(bookmark: Bookmark): ContentValues = contentValuesOf(
|
||||||
|
COLUMN_MEDIA_NAME to bookmark.mediaName,
|
||||||
|
COLUMN_CREATOR to bookmark.mediaCreator
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
package fr.free.nrw.commons.bookmarks.pictures
|
||||||
|
|
||||||
|
import android.database.sqlite.SQLiteDatabase
|
||||||
|
|
||||||
|
object BookmarksTable {
|
||||||
|
const val TABLE_NAME: String = "bookmarks"
|
||||||
|
const val COLUMN_MEDIA_NAME: String = "media_name"
|
||||||
|
const val COLUMN_CREATOR: String = "media_creator"
|
||||||
|
|
||||||
|
// NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES.
|
||||||
|
val ALL_FIELDS = arrayOf(
|
||||||
|
COLUMN_MEDIA_NAME,
|
||||||
|
COLUMN_CREATOR
|
||||||
|
)
|
||||||
|
|
||||||
|
const val DROP_TABLE_STATEMENT: String = "DROP TABLE IF EXISTS $TABLE_NAME"
|
||||||
|
|
||||||
|
const val CREATE_TABLE_STATEMENT: String = ("CREATE TABLE $TABLE_NAME (" +
|
||||||
|
"$COLUMN_MEDIA_NAME STRING PRIMARY KEY, " +
|
||||||
|
"$COLUMN_CREATOR STRING" +
|
||||||
|
");")
|
||||||
|
|
||||||
|
fun onCreate(db: SQLiteDatabase) =
|
||||||
|
db.execSQL(CREATE_TABLE_STATEMENT)
|
||||||
|
|
||||||
|
fun onDelete(db: SQLiteDatabase) {
|
||||||
|
db.execSQL(DROP_TABLE_STATEMENT)
|
||||||
|
onCreate(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onUpdate(db: SQLiteDatabase, from: Int, to: Int) {
|
||||||
|
if (from == to) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (from < 7) {
|
||||||
|
// doesn't exist yet
|
||||||
|
onUpdate(db, from+1, to)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (from == 7) {
|
||||||
|
// table added in version 8
|
||||||
|
onCreate(db)
|
||||||
|
onUpdate(db, from+1, to)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (from == 8) {
|
||||||
|
onUpdate(db, from+1, to)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,7 +5,7 @@ import android.database.sqlite.SQLiteDatabase
|
||||||
import android.database.sqlite.SQLiteException
|
import android.database.sqlite.SQLiteException
|
||||||
import android.database.sqlite.SQLiteOpenHelper
|
import android.database.sqlite.SQLiteOpenHelper
|
||||||
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable
|
||||||
import fr.free.nrw.commons.category.CategoryDao
|
import fr.free.nrw.commons.category.CategoryDao
|
||||||
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesDao
|
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesDao
|
||||||
import fr.free.nrw.commons.recentlanguages.RecentLanguagesDao
|
import fr.free.nrw.commons.recentlanguages.RecentLanguagesDao
|
||||||
|
|
@ -29,7 +29,7 @@ class DBOpenHelper(
|
||||||
*/
|
*/
|
||||||
override fun onCreate(db: SQLiteDatabase) {
|
override fun onCreate(db: SQLiteDatabase) {
|
||||||
CategoryDao.Table.onCreate(db)
|
CategoryDao.Table.onCreate(db)
|
||||||
BookmarkPicturesDao.Table.onCreate(db)
|
BookmarksTable.onCreate(db)
|
||||||
BookmarkItemsTable.onCreate(db)
|
BookmarkItemsTable.onCreate(db)
|
||||||
RecentSearchesDao.Table.onCreate(db)
|
RecentSearchesDao.Table.onCreate(db)
|
||||||
RecentLanguagesDao.Table.onCreate(db)
|
RecentLanguagesDao.Table.onCreate(db)
|
||||||
|
|
@ -37,7 +37,7 @@ class DBOpenHelper(
|
||||||
|
|
||||||
override fun onUpgrade(db: SQLiteDatabase, from: Int, to: Int) {
|
override fun onUpgrade(db: SQLiteDatabase, from: Int, to: Int) {
|
||||||
CategoryDao.Table.onUpdate(db, from, to)
|
CategoryDao.Table.onUpdate(db, from, to)
|
||||||
BookmarkPicturesDao.Table.onUpdate(db, from, to)
|
BookmarksTable.onUpdate(db, from, to)
|
||||||
BookmarkItemsTable.onUpdate(db, from, to)
|
BookmarkItemsTable.onUpdate(db, from, to)
|
||||||
RecentSearchesDao.Table.onUpdate(db, from, to)
|
RecentSearchesDao.Table.onUpdate(db, from, to)
|
||||||
RecentLanguagesDao.Table.onUpdate(db, from, to)
|
RecentLanguagesDao.Table.onUpdate(db, from, to)
|
||||||
|
|
|
||||||
32
app/src/main/java/fr/free/nrw/commons/utils/DatabaseUtils.kt
Normal file
32
app/src/main/java/fr/free/nrw/commons/utils/DatabaseUtils.kt
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
package fr.free.nrw.commons.utils
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import android.database.Cursor
|
||||||
|
|
||||||
|
fun Cursor.getStringArray(name: String): List<String> =
|
||||||
|
stringToArray(getString(name))
|
||||||
|
|
||||||
|
@SuppressLint("Range")
|
||||||
|
fun Cursor.getString(name: String): String =
|
||||||
|
getString(getColumnIndex(name))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts string to List
|
||||||
|
* @param listString comma separated single string from of list items
|
||||||
|
* @return List of string
|
||||||
|
*/
|
||||||
|
fun stringToArray(listString: String?): List<String> {
|
||||||
|
if (listString.isNullOrEmpty()) return emptyList();
|
||||||
|
val elements = listString.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
|
||||||
|
return listOf(*elements)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts string to List
|
||||||
|
* @param list list of items
|
||||||
|
* @return string comma separated single string of items
|
||||||
|
*/
|
||||||
|
fun arrayToString(list: List<String?>?): String? {
|
||||||
|
return list?.joinToString(",")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -20,13 +20,13 @@ import com.nhaarman.mockitokotlin2.whenever
|
||||||
import fr.free.nrw.commons.TestCommonsApplication
|
import fr.free.nrw.commons.TestCommonsApplication
|
||||||
import fr.free.nrw.commons.bookmarks.models.Bookmark
|
import fr.free.nrw.commons.bookmarks.models.Bookmark
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesContentProvider.Companion.BASE_URI
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesContentProvider.Companion.BASE_URI
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao.Table.COLUMN_CREATOR
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.COLUMN_CREATOR
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao.Table.COLUMN_MEDIA_NAME
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.COLUMN_MEDIA_NAME
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao.Table.CREATE_TABLE_STATEMENT
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.CREATE_TABLE_STATEMENT
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao.Table.DROP_TABLE_STATEMENT
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.DROP_TABLE_STATEMENT
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao.Table.onCreate
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.onCreate
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao.Table.onDelete
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.onDelete
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao.Table.onUpdate
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.onUpdate
|
||||||
import org.junit.Assert.assertEquals
|
import org.junit.Assert.assertEquals
|
||||||
import org.junit.Assert.assertFalse
|
import org.junit.Assert.assertFalse
|
||||||
import org.junit.Assert.assertTrue
|
import org.junit.Assert.assertTrue
|
||||||
|
|
@ -84,7 +84,7 @@ class BookmarkPictureDaoTest {
|
||||||
fun getAllBookmarks() {
|
fun getAllBookmarks() {
|
||||||
whenever(client.query(any(), any(), anyOrNull(), any(), anyOrNull())).thenReturn(createCursor(14))
|
whenever(client.query(any(), any(), anyOrNull(), any(), anyOrNull())).thenReturn(createCursor(14))
|
||||||
|
|
||||||
var result = testObject.allBookmarks
|
var result = testObject.getAllBookmarks()
|
||||||
|
|
||||||
assertEquals(14, (result.size))
|
assertEquals(14, (result.size))
|
||||||
}
|
}
|
||||||
|
|
@ -92,19 +92,19 @@ class BookmarkPictureDaoTest {
|
||||||
@Test(expected = RuntimeException::class)
|
@Test(expected = RuntimeException::class)
|
||||||
fun getAllBookmarksTranslatesExceptions() {
|
fun getAllBookmarksTranslatesExceptions() {
|
||||||
whenever(client.query(any(), any(), anyOrNull(), any(), anyOrNull())).thenThrow(RemoteException(""))
|
whenever(client.query(any(), any(), anyOrNull(), any(), anyOrNull())).thenThrow(RemoteException(""))
|
||||||
testObject.allBookmarks
|
testObject.getAllBookmarks()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun getAllBookmarksReturnsEmptyList_emptyCursor() {
|
fun getAllBookmarksReturnsEmptyList_emptyCursor() {
|
||||||
whenever(client.query(any(), any(), anyOrNull(), any(), anyOrNull())).thenReturn(createCursor(0))
|
whenever(client.query(any(), any(), anyOrNull(), any(), anyOrNull())).thenReturn(createCursor(0))
|
||||||
assertTrue(testObject.allBookmarks.isEmpty())
|
assertTrue(testObject.getAllBookmarks().isEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun getAllBookmarksReturnsEmptyList_nullCursor() {
|
fun getAllBookmarksReturnsEmptyList_nullCursor() {
|
||||||
whenever(client.query(any(), any(), anyOrNull(), any(), anyOrNull())).thenReturn(null)
|
whenever(client.query(any(), any(), anyOrNull(), any(), anyOrNull())).thenReturn(null)
|
||||||
assertTrue(testObject.allBookmarks.isEmpty())
|
assertTrue(testObject.getAllBookmarks().isEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -113,7 +113,7 @@ class BookmarkPictureDaoTest {
|
||||||
whenever(client.query(any(), any(), anyOrNull(), any(), anyOrNull())).thenReturn(mockCursor)
|
whenever(client.query(any(), any(), anyOrNull(), any(), anyOrNull())).thenReturn(mockCursor)
|
||||||
whenever(mockCursor.moveToFirst()).thenReturn(false)
|
whenever(mockCursor.moveToFirst()).thenReturn(false)
|
||||||
|
|
||||||
testObject.allBookmarks
|
testObject.getAllBookmarks()
|
||||||
|
|
||||||
verify(mockCursor).close()
|
verify(mockCursor).close()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ class BookmarkPicturesControllerTest {
|
||||||
fun setup() {
|
fun setup() {
|
||||||
MockitoAnnotations.initMocks(this)
|
MockitoAnnotations.initMocks(this)
|
||||||
val mockMedia = mockMedia
|
val mockMedia = mockMedia
|
||||||
whenever(bookmarkDao!!.allBookmarks)
|
whenever(bookmarkDao!!.getAllBookmarks())
|
||||||
.thenReturn(mockBookmarkList)
|
.thenReturn(mockBookmarkList)
|
||||||
whenever(
|
whenever(
|
||||||
mediaClient!!.getMedia(
|
mediaClient!!.getMedia(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue