mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-24 05:04:50 -04:00
Merge pull request #1457 from kiwix/feature/macgills/#1449-database-migration
Feature/macgills/#1449 database migration
This commit is contained in:
commit
6570b43b63
@ -188,9 +188,9 @@ def branchName = System.getenv('TRAVIS_PULL_REQUEST') ?: "false" == "false"
|
|||||||
def buildNumber = System.getenv('TRAVIS_BUILD_NUMBER') ?: "dev"
|
def buildNumber = System.getenv('TRAVIS_BUILD_NUMBER') ?: "dev"
|
||||||
|
|
||||||
ext {
|
ext {
|
||||||
versionMajor = 2
|
versionMajor = 3
|
||||||
versionMinor = 5
|
versionMinor = 0
|
||||||
versionPatch = 1
|
versionPatch = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
private String generateVersionName() {
|
private String generateVersionName() {
|
||||||
|
@ -51,7 +51,7 @@ public class KiwixDatabaseTest {
|
|||||||
@Test
|
@Test
|
||||||
@SuppressWarnings("CharsetObjectCanBeUsed") // Standard charset throws exception on < API19
|
@SuppressWarnings("CharsetObjectCanBeUsed") // Standard charset throws exception on < API19
|
||||||
public void testMigrateDatabase() throws IOException {
|
public void testMigrateDatabase() throws IOException {
|
||||||
KiwixDatabase kiwixDatabase = new KiwixDatabase(context, null, null);
|
KiwixDatabase kiwixDatabase = new KiwixDatabase(context, null, null, null, null);
|
||||||
kiwixDatabase.recreate();
|
kiwixDatabase.recreate();
|
||||||
String testId = "8ce5775a-10a9-bbf3-178a-9df69f23263c";
|
String testId = "8ce5775a-10a9-bbf3-178a-9df69f23263c";
|
||||||
String[] testBookmarks = new String[] { "Test1", "Test2", "Test3" };
|
String[] testBookmarks = new String[] { "Test1", "Test2", "Test3" };
|
||||||
|
@ -23,9 +23,7 @@ import com.yahoo.squidb.sql.Query;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.kiwix.kiwixmobile.data.ZimContentProvider;
|
|
||||||
import org.kiwix.kiwixmobile.data.local.KiwixDatabase;
|
import org.kiwix.kiwixmobile.data.local.KiwixDatabase;
|
||||||
import org.kiwix.kiwixmobile.data.local.entity.RecentSearch;
|
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
@ -58,10 +56,6 @@ public class RecentSearchDaoTest {
|
|||||||
recentSearchDao.getRecentSearches();
|
recentSearchDao.getRecentSearches();
|
||||||
|
|
||||||
// verify ordering is in descending order of search ID and the results are limited to 5 only
|
// verify ordering is in descending order of search ID and the results are limited to 5 only
|
||||||
verify(kiwixDatabase).query(any(),
|
verify(kiwixDatabase).query(any(), eq(Query.select()));
|
||||||
eq(Query.selectDistinct(RecentSearch.SEARCH_STRING).where(RecentSearch.ZIM_I_D.eq(
|
|
||||||
ZimContentProvider.getId()))
|
|
||||||
.orderBy(RecentSearch.ID.desc())
|
|
||||||
.limit(5)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,7 @@ import dagger.android.HasActivityInjector;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
import org.kiwix.kiwixmobile.data.local.KiwixDatabase;
|
||||||
import org.kiwix.kiwixmobile.di.components.ApplicationComponent;
|
import org.kiwix.kiwixmobile.di.components.ApplicationComponent;
|
||||||
import org.kiwix.kiwixmobile.di.components.DaggerApplicationComponent;
|
import org.kiwix.kiwixmobile.di.components.DaggerApplicationComponent;
|
||||||
|
|
||||||
@ -46,7 +47,8 @@ public class KiwixApplication extends MultiDexApplication implements HasActivity
|
|||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
DispatchingAndroidInjector<Activity> activityInjector;
|
DispatchingAndroidInjector<Activity> activityInjector;
|
||||||
private File logFile;
|
@Inject
|
||||||
|
KiwixDatabase kiwixDatabase;
|
||||||
|
|
||||||
public static KiwixApplication getInstance() {
|
public static KiwixApplication getInstance() {
|
||||||
return application;
|
return application;
|
||||||
@ -73,41 +75,36 @@ public class KiwixApplication extends MultiDexApplication implements HasActivity
|
|||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
AndroidThreeTen.init(this);
|
AndroidThreeTen.init(this);
|
||||||
|
writeLogFile();
|
||||||
|
applicationComponent.inject(this);
|
||||||
|
kiwixDatabase.forceMigration();
|
||||||
|
if (BuildConfig.DEBUG) {
|
||||||
|
StrictMode.setThreadPolicy(buildThreadPolicy(new StrictMode.ThreadPolicy.Builder()));
|
||||||
|
StrictMode.setVmPolicy(buildVmPolicy(new StrictMode.VmPolicy.Builder()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeLogFile() {
|
||||||
if (isExternalStorageWritable()) {
|
if (isExternalStorageWritable()) {
|
||||||
File appDirectory = new File(Environment.getExternalStorageDirectory() + "/Kiwix");
|
File appDirectory = new File(Environment.getExternalStorageDirectory() + "/Kiwix");
|
||||||
logFile = new File(appDirectory, "logcat.txt");
|
File logFile = new File(appDirectory, "logcat.txt");
|
||||||
Log.d("KIWIX", "Writing all logs into [" + logFile.getPath() + "]");
|
Log.d("KIWIX", "Writing all logs into [" + logFile.getPath() + "]");
|
||||||
|
|
||||||
// create app folder
|
// create app folder
|
||||||
if (!appDirectory.exists()) {
|
if (!appDirectory.exists()) {
|
||||||
appDirectory.mkdir();
|
appDirectory.mkdir();
|
||||||
}
|
}
|
||||||
|
|
||||||
// create log folder
|
|
||||||
if (!appDirectory.exists()) {
|
|
||||||
appDirectory.mkdir();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (logFile.exists() && logFile.isFile()) {
|
if (logFile.exists() && logFile.isFile()) {
|
||||||
logFile.delete();
|
logFile.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
// clear the previous logcat and then write the new one to the file
|
// clear the previous logcat and then write the new one to the file
|
||||||
try {
|
try {
|
||||||
logFile.createNewFile();
|
logFile.createNewFile();
|
||||||
Process process = Runtime.getRuntime().exec("logcat -c");
|
Runtime.getRuntime().exec("logcat -c");
|
||||||
process = Runtime.getRuntime().exec("logcat -f " + logFile.getPath() + " -s kiwix");
|
Runtime.getRuntime().exec("logcat -f " + logFile.getPath() + " -s kiwix");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e("KIWIX", "Error while writing logcat.txt", e);
|
Log.e("KIWIX", "Error while writing logcat.txt", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.d("KIWIX", "Started KiwixApplication");
|
|
||||||
applicationComponent.inject(this);
|
|
||||||
if (BuildConfig.DEBUG) {
|
|
||||||
StrictMode.setThreadPolicy(buildThreadPolicy(new StrictMode.ThreadPolicy.Builder()));
|
|
||||||
StrictMode.setVmPolicy(buildVmPolicy(new StrictMode.VmPolicy.Builder()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private StrictMode.ThreadPolicy buildThreadPolicy(StrictMode.ThreadPolicy.Builder builder) {
|
private StrictMode.ThreadPolicy buildThreadPolicy(StrictMode.ThreadPolicy.Builder builder) {
|
||||||
|
@ -7,10 +7,10 @@ data class BookmarkItem(
|
|||||||
val databaseId: Long = 0L,
|
val databaseId: Long = 0L,
|
||||||
val zimId: String,
|
val zimId: String,
|
||||||
val zimName: String,
|
val zimName: String,
|
||||||
val zimFilePath: String,
|
val zimFilePath: String?,
|
||||||
val bookmarkUrl: String,
|
val bookmarkUrl: String,
|
||||||
val bookmarkTitle: String,
|
val bookmarkTitle: String,
|
||||||
val favicon: String
|
val favicon: String?
|
||||||
) {
|
) {
|
||||||
constructor(entity: BookmarkEntity) : this(
|
constructor(entity: BookmarkEntity) : this(
|
||||||
entity.id,
|
entity.id,
|
||||||
|
@ -29,39 +29,48 @@ import java.io.FileNotFoundException;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.util.List;
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
import org.kiwix.kiwixmobile.data.ZimContentProvider;
|
import org.kiwix.kiwixmobile.data.ZimContentProvider;
|
||||||
import org.kiwix.kiwixmobile.data.local.dao.BookDao;
|
import org.kiwix.kiwixmobile.data.local.dao.BookDao;
|
||||||
import org.kiwix.kiwixmobile.data.local.dao.BookmarksDao;
|
import org.kiwix.kiwixmobile.data.local.dao.BookmarksDao;
|
||||||
import org.kiwix.kiwixmobile.data.local.dao.NetworkLanguageDao;
|
import org.kiwix.kiwixmobile.data.local.dao.NetworkLanguageDao;
|
||||||
|
import org.kiwix.kiwixmobile.data.local.dao.RecentSearchDao;
|
||||||
import org.kiwix.kiwixmobile.data.local.entity.BookDatabaseEntity;
|
import org.kiwix.kiwixmobile.data.local.entity.BookDatabaseEntity;
|
||||||
import org.kiwix.kiwixmobile.data.local.entity.Bookmark;
|
import org.kiwix.kiwixmobile.data.local.entity.Bookmark;
|
||||||
import org.kiwix.kiwixmobile.data.local.entity.LibraryDatabaseEntity;
|
import org.kiwix.kiwixmobile.data.local.entity.LibraryDatabaseEntity;
|
||||||
import org.kiwix.kiwixmobile.data.local.entity.NetworkLanguageDatabaseEntity;
|
import org.kiwix.kiwixmobile.data.local.entity.NetworkLanguageDatabaseEntity;
|
||||||
import org.kiwix.kiwixmobile.data.local.entity.RecentSearch;
|
import org.kiwix.kiwixmobile.data.local.entity.RecentSearch;
|
||||||
import org.kiwix.kiwixmobile.database.newdb.dao.NewBookDao;
|
import org.kiwix.kiwixmobile.database.newdb.dao.NewBookDao;
|
||||||
|
import org.kiwix.kiwixmobile.database.newdb.dao.NewBookmarksDao;
|
||||||
import org.kiwix.kiwixmobile.database.newdb.dao.NewLanguagesDao;
|
import org.kiwix.kiwixmobile.database.newdb.dao.NewLanguagesDao;
|
||||||
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity;
|
import org.kiwix.kiwixmobile.database.newdb.dao.NewRecentSearchDao;
|
||||||
import org.kiwix.kiwixmobile.utils.UpdateUtils;
|
import org.kiwix.kiwixmobile.utils.UpdateUtils;
|
||||||
|
|
||||||
import static org.kiwix.kiwixmobile.utils.Constants.TAG_KIWIX;
|
import static org.kiwix.kiwixmobile.utils.Constants.TAG_KIWIX;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
|
@Deprecated //delete once migrations are no longer needed
|
||||||
public class KiwixDatabase extends SquidDatabase {
|
public class KiwixDatabase extends SquidDatabase {
|
||||||
|
|
||||||
private static final int VERSION = 17;
|
private static final int TWO_POINT_FIVE_POINT_THREE = 16;
|
||||||
|
private static final int FINAL = 17;//3.0.0
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final NewBookDao bookDao;
|
private final NewBookDao bookDao;
|
||||||
private final NewLanguagesDao languagesDao;
|
private final NewLanguagesDao languagesDao;
|
||||||
|
private final NewBookmarksDao bookmarksDao;
|
||||||
|
private final NewRecentSearchDao recentSearchDao;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public KiwixDatabase(Context context, NewBookDao bookDao, NewLanguagesDao languagesDao) {
|
public KiwixDatabase(Context context, NewBookDao bookDao, NewLanguagesDao languagesDao,
|
||||||
|
NewBookmarksDao bookmarksDao,
|
||||||
|
NewRecentSearchDao recentSearchDao) {
|
||||||
super(context);
|
super(context);
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.bookDao = bookDao;
|
this.bookDao = bookDao;
|
||||||
this.languagesDao = languagesDao;
|
this.languagesDao = languagesDao;
|
||||||
|
this.bookmarksDao = bookmarksDao;
|
||||||
|
this.recentSearchDao = recentSearchDao;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -74,23 +83,14 @@ public class KiwixDatabase extends SquidDatabase {
|
|||||||
return new Table[] {
|
return new Table[] {
|
||||||
RecentSearch.TABLE,
|
RecentSearch.TABLE,
|
||||||
Bookmark.TABLE,
|
Bookmark.TABLE,
|
||||||
|
BookDatabaseEntity.TABLE,
|
||||||
|
NetworkLanguageDatabaseEntity.TABLE
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean onUpgrade(SQLiteDatabaseWrapper db, int oldVersion, int newVersion) {
|
protected boolean onUpgrade(SQLiteDatabaseWrapper db, int oldVersion, int newVersion) {
|
||||||
if (newVersion >= 16) { //2.5 attempt reading values from old db before they get dropped
|
Log.e("UPGRADE", "oldversion: " + oldVersion + " newVersion: " + newVersion);
|
||||||
try {
|
|
||||||
bookDao.migrationInsert(new BookDao(this).getBooks());
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
languagesDao.insert(new NetworkLanguageDao(this).getFilteredLanguages());
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
switch (oldVersion) {
|
switch (oldVersion) {
|
||||||
case 1:
|
case 1:
|
||||||
case 2:
|
case 2:
|
||||||
@ -132,40 +132,41 @@ public class KiwixDatabase extends SquidDatabase {
|
|||||||
tryCreateTable(BookDatabaseEntity.TABLE);
|
tryCreateTable(BookDatabaseEntity.TABLE);
|
||||||
case 14:
|
case 14:
|
||||||
case 15:
|
case 15:
|
||||||
tryAddColumn(Bookmark.ZIM_FILE_PATH);
|
try {
|
||||||
tryAddColumn(Bookmark.FAVICON);
|
bookDao.migrationInsert(new BookDao(this).getBooks());
|
||||||
migrateBookmarksVersion16();
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
languagesDao.insert(new NetworkLanguageDao(this).getFilteredLanguages());
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
tryDropTable(BookDatabaseEntity.TABLE);
|
tryDropTable(BookDatabaseEntity.TABLE);
|
||||||
tryDropTable(NetworkLanguageDatabaseEntity.TABLE);
|
tryDropTable(NetworkLanguageDatabaseEntity.TABLE);
|
||||||
tryDropTable(LibraryDatabaseEntity.TABLE);
|
tryDropTable(LibraryDatabaseEntity.TABLE);
|
||||||
case 16:
|
case TWO_POINT_FIVE_POINT_THREE:
|
||||||
new BookmarksDao(this).processBookmark(UpdateUtils::reformatProviderUrl);
|
try {
|
||||||
//TODO MIGRATIONS BEFORE 3.0
|
final BookmarksDao oldBookmarksDao = new BookmarksDao(this);
|
||||||
|
oldBookmarksDao.processBookmark(UpdateUtils::reformatProviderUrl);
|
||||||
|
this.bookmarksDao.migrationInsert(oldBookmarksDao.getBookmarks(false), bookDao);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
tryDropTable(Bookmark.TABLE);
|
||||||
|
try {
|
||||||
|
recentSearchDao.migrationInsert(new RecentSearchDao(this).getRecentSearches());
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
tryDropTable(RecentSearch.TABLE);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void migrateBookmarksVersion16() {
|
|
||||||
BookmarksDao bookmarksDao = new BookmarksDao(this);
|
|
||||||
BookDao bookDao = new BookDao(this);
|
|
||||||
List<Bookmark> bookmarks = bookmarksDao.getBookmarks(false);
|
|
||||||
List<LibraryNetworkEntity.Book> books = bookDao.getBooks();
|
|
||||||
for (Bookmark bookmark : bookmarks) {
|
|
||||||
if (bookmark.getZimId() != null) {
|
|
||||||
for (LibraryNetworkEntity.Book book : books) {
|
|
||||||
if (bookmark.getZimId().equals(book.getId())) {
|
|
||||||
bookmark.setZimFilePath(book.getUrl()).setFavicon(book.getFavicon());
|
|
||||||
bookmarksDao.saveBookmark(bookmark);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int getVersion() {
|
protected int getVersion() {
|
||||||
return VERSION;
|
return FINAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void migrateBookmarksVersion6() {
|
public void migrateBookmarksVersion6() {
|
||||||
@ -199,6 +200,14 @@ public class KiwixDatabase extends SquidDatabase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Now that the database is no longer used
|
||||||
|
* we need to make a migration happen with an explicit call
|
||||||
|
*/
|
||||||
|
public void forceMigration() {
|
||||||
|
beginTransaction();
|
||||||
|
endTransaction();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -40,36 +40,6 @@ public class BookmarksDao {
|
|||||||
this.kiwixDatabase = kiwixDatabase;
|
this.kiwixDatabase = kiwixDatabase;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Url of the bookmarks from the current Zim file.
|
|
||||||
*/
|
|
||||||
public List<String> getCurrentZimBookmarksUrl() {
|
|
||||||
ArrayList<String> bookmarksUrl = new ArrayList<>();
|
|
||||||
try (SquidCursor<Bookmark> bookmarkCursor = kiwixDatabase.query(Bookmark.class,
|
|
||||||
Query.selectDistinct(Bookmark.BOOKMARK_URL)
|
|
||||||
.where(Bookmark.ZIM_ID.eq(ZimContentProvider.getId())
|
|
||||||
.or(Bookmark.ZIM_NAME.eq(ZimContentProvider.getName())))
|
|
||||||
.orderBy(Bookmark.BOOKMARK_TITLE.asc()))) {
|
|
||||||
while (bookmarkCursor.moveToNext()) {
|
|
||||||
bookmarksUrl.add(bookmarkCursor.get(Bookmark.BOOKMARK_URL));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return bookmarksUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void saveBookmark(Bookmark bookmark) {
|
|
||||||
kiwixDatabase.deleteWhere(Bookmark.class, Bookmark.BOOKMARK_URL.eq(bookmark.getBookmarkUrl())
|
|
||||||
.and(Bookmark.ZIM_ID.eq(bookmark.getZimId())));
|
|
||||||
|
|
||||||
kiwixDatabase.persist(new Bookmark()
|
|
||||||
.setZimId(bookmark.getZimId())
|
|
||||||
.setZimName(bookmark.getZimName())
|
|
||||||
.setZimFilePath(bookmark.getZimFilePath())
|
|
||||||
.setFavicon(bookmark.getFavicon())
|
|
||||||
.setBookmarkUrl(bookmark.getBookmarkUrl())
|
|
||||||
.setBookmarkTitle(bookmark.getBookmarkTitle()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Bookmark> getBookmarks(boolean fromCurrentBook) {
|
public List<Bookmark> getBookmarks(boolean fromCurrentBook) {
|
||||||
ArrayList<Bookmark> bookmarks = new ArrayList<>();
|
ArrayList<Bookmark> bookmarks = new ArrayList<>();
|
||||||
Query query = Query.select();
|
Query query = Query.select();
|
||||||
@ -80,32 +50,16 @@ public class BookmarksDao {
|
|||||||
.query(Bookmark.class, query.orderBy(Bookmark.BOOKMARK_TITLE.asc()))) {
|
.query(Bookmark.class, query.orderBy(Bookmark.BOOKMARK_TITLE.asc()))) {
|
||||||
while (squidCursor.moveToNext()) {
|
while (squidCursor.moveToNext()) {
|
||||||
Bookmark bookmark = new Bookmark();
|
Bookmark bookmark = new Bookmark();
|
||||||
|
|
||||||
bookmark.setZimId(squidCursor.get(Bookmark.ZIM_ID));
|
bookmark.setZimId(squidCursor.get(Bookmark.ZIM_ID));
|
||||||
bookmark.setZimName(squidCursor.get(Bookmark.ZIM_NAME));
|
bookmark.setZimName(squidCursor.get(Bookmark.ZIM_NAME));
|
||||||
bookmark.setZimFilePath(squidCursor.get(Bookmark.ZIM_FILE_PATH));
|
|
||||||
bookmark.setFavicon(squidCursor.get(Bookmark.FAVICON));
|
|
||||||
bookmark.setBookmarkTitle(squidCursor.get(Bookmark.BOOKMARK_TITLE));
|
bookmark.setBookmarkTitle(squidCursor.get(Bookmark.BOOKMARK_TITLE));
|
||||||
bookmark.setBookmarkUrl(squidCursor.get(Bookmark.BOOKMARK_URL));
|
bookmark.setBookmarkUrl(squidCursor.get(Bookmark.BOOKMARK_URL));
|
||||||
|
|
||||||
bookmarks.add(bookmark);
|
bookmarks.add(bookmark);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return bookmarks;
|
return bookmarks;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteBookmarks(List<Bookmark> bookmarks) {
|
|
||||||
for (Bookmark bookmark : bookmarks) {
|
|
||||||
kiwixDatabase.deleteWhere(Bookmark.class, Bookmark.BOOKMARK_URL.eq(bookmark.getBookmarkUrl())
|
|
||||||
.and(Bookmark.ZIM_ID.eq(bookmark.getZimId())));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void deleteBookmark(Bookmark bookmark) {
|
|
||||||
kiwixDatabase.deleteWhere(Bookmark.class, Bookmark.BOOKMARK_URL.eq(bookmark.getBookmarkUrl())
|
|
||||||
.and(Bookmark.ZIM_ID.eq(bookmark.getZimId())));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void processBookmark(StringOperation operation) {
|
public void processBookmark(StringOperation operation) {
|
||||||
try (SquidCursor<Bookmark> bookmarkCursor = kiwixDatabase.query(Bookmark.class,
|
try (SquidCursor<Bookmark> bookmarkCursor = kiwixDatabase.query(Bookmark.class,
|
||||||
Query.select(Bookmark.ID, Bookmark.BOOKMARK_URL))) {
|
Query.select(Bookmark.ID, Bookmark.BOOKMARK_URL))) {
|
||||||
|
@ -22,7 +22,6 @@ import com.yahoo.squidb.sql.Query;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import org.kiwix.kiwixmobile.data.ZimContentProvider;
|
|
||||||
import org.kiwix.kiwixmobile.data.local.KiwixDatabase;
|
import org.kiwix.kiwixmobile.data.local.KiwixDatabase;
|
||||||
import org.kiwix.kiwixmobile.data.local.entity.RecentSearch;
|
import org.kiwix.kiwixmobile.data.local.entity.RecentSearch;
|
||||||
|
|
||||||
@ -43,43 +42,14 @@ public class RecentSearchDao {
|
|||||||
/**
|
/**
|
||||||
* Returns a distinct enumeration of the {@code NUM_RECENT_RESULTS} most recent searches.
|
* Returns a distinct enumeration of the {@code NUM_RECENT_RESULTS} most recent searches.
|
||||||
*/
|
*/
|
||||||
public List<String> getRecentSearches() {
|
public List<RecentSearch> getRecentSearches() {
|
||||||
SquidCursor<RecentSearch> searchCursor = mDb.query(
|
List<RecentSearch> result = new ArrayList<>();
|
||||||
RecentSearch.class,
|
try (SquidCursor<RecentSearch> searchCursor = mDb.query(
|
||||||
Query.selectDistinct(RecentSearch.SEARCH_STRING)
|
RecentSearch.class, Query.select())) {
|
||||||
.where(RecentSearch.ZIM_I_D.eq(ZimContentProvider.getId()))
|
|
||||||
.orderBy(RecentSearch.ID.desc())
|
|
||||||
.limit(NUM_RECENT_RESULTS));
|
|
||||||
List<String> result = new ArrayList<>();
|
|
||||||
try {
|
|
||||||
while (searchCursor.moveToNext()) {
|
while (searchCursor.moveToNext()) {
|
||||||
result.add(searchCursor.get(RecentSearch.SEARCH_STRING));
|
result.add(new RecentSearch(searchCursor));
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
searchCursor.close();
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Save {@code searchString} as the most recent search.
|
|
||||||
*/
|
|
||||||
public void saveSearch(String searchString) {
|
|
||||||
mDb.persist(
|
|
||||||
new RecentSearch().setSearchString(searchString).setZimID(ZimContentProvider.getId()));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete all entries that exactly matches {@code searchString}
|
|
||||||
*/
|
|
||||||
public void deleteSearchString(String searchString) {
|
|
||||||
mDb.deleteWhere(RecentSearch.class, RecentSearch.SEARCH_STRING.eq(searchString));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deletes all entries.
|
|
||||||
*/
|
|
||||||
public void deleteSearchHistory() {
|
|
||||||
mDb.deleteWhere(RecentSearch.class, RecentSearch.ID.isNotNull());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -28,8 +28,6 @@ public class BookmarksSpec {
|
|||||||
@ColumnSpec(constraints = "NOT NULL")
|
@ColumnSpec(constraints = "NOT NULL")
|
||||||
public String ZimId;
|
public String ZimId;
|
||||||
public String ZimName;
|
public String ZimName;
|
||||||
public String zimFilePath;
|
|
||||||
public String bookmarkUrl;
|
public String bookmarkUrl;
|
||||||
public String bookmarkTitle;
|
public String bookmarkTitle;
|
||||||
public String favicon;
|
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.database.newdb.dao
|
|||||||
import io.objectbox.Box
|
import io.objectbox.Box
|
||||||
import io.objectbox.kotlin.inValues
|
import io.objectbox.kotlin.inValues
|
||||||
import io.objectbox.kotlin.query
|
import io.objectbox.kotlin.query
|
||||||
|
import org.kiwix.kiwixmobile.data.local.entity.Bookmark
|
||||||
import org.kiwix.kiwixmobile.database.newdb.entities.BookOnDiskEntity
|
import org.kiwix.kiwixmobile.database.newdb.entities.BookOnDiskEntity
|
||||||
import org.kiwix.kiwixmobile.database.newdb.entities.BookOnDiskEntity_
|
import org.kiwix.kiwixmobile.database.newdb.entities.BookOnDiskEntity_
|
||||||
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book
|
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book
|
||||||
@ -62,4 +63,11 @@ class NewBookDao @Inject constructor(private val box: Box<BookOnDiskEntity>) {
|
|||||||
private fun delete(books: List<BookOnDiskEntity>) {
|
private fun delete(books: List<BookOnDiskEntity>) {
|
||||||
box.remove(books)
|
box.remove(books)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getFavIconAndZimFile(it: Bookmark): Pair<String?, String?> {
|
||||||
|
val bookOnDiskEntity = box.query {
|
||||||
|
equal(BookOnDiskEntity_.bookId, it.zimId)
|
||||||
|
}.find().getOrNull(0)
|
||||||
|
return bookOnDiskEntity?.let { Pair(it.favIcon, it.file.path) } ?: Pair(null, null)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ import io.objectbox.Box
|
|||||||
import io.objectbox.kotlin.query
|
import io.objectbox.kotlin.query
|
||||||
import org.kiwix.kiwixmobile.bookmark.BookmarkItem
|
import org.kiwix.kiwixmobile.bookmark.BookmarkItem
|
||||||
import org.kiwix.kiwixmobile.data.ZimContentProvider
|
import org.kiwix.kiwixmobile.data.ZimContentProvider
|
||||||
|
import org.kiwix.kiwixmobile.data.local.entity.Bookmark
|
||||||
import org.kiwix.kiwixmobile.database.newdb.entities.BookmarkEntity
|
import org.kiwix.kiwixmobile.database.newdb.entities.BookmarkEntity
|
||||||
import org.kiwix.kiwixmobile.database.newdb.entities.BookmarkEntity_
|
import org.kiwix.kiwixmobile.database.newdb.entities.BookmarkEntity_
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@ -58,4 +59,11 @@ class NewBookmarksDao @Inject constructor(val box: Box<BookmarkEntity>) {
|
|||||||
equal(BookmarkEntity_.bookmarkUrl, bookmarkUrl)
|
equal(BookmarkEntity_.bookmarkUrl, bookmarkUrl)
|
||||||
}.remove()
|
}.remove()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun migrationInsert(
|
||||||
|
bookmarks: MutableList<Bookmark>,
|
||||||
|
bookDao: NewBookDao
|
||||||
|
) {
|
||||||
|
box.put(bookmarks.zip(bookmarks.map { bookDao.getFavIconAndZimFile(it) }).map(::BookmarkEntity))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.database.newdb.dao
|
|||||||
import io.objectbox.Box
|
import io.objectbox.Box
|
||||||
import io.objectbox.kotlin.query
|
import io.objectbox.kotlin.query
|
||||||
import org.kiwix.kiwixmobile.data.ZimContentProvider
|
import org.kiwix.kiwixmobile.data.ZimContentProvider
|
||||||
|
import org.kiwix.kiwixmobile.data.local.entity.RecentSearch
|
||||||
import org.kiwix.kiwixmobile.database.newdb.entities.RecentSearchEntity
|
import org.kiwix.kiwixmobile.database.newdb.entities.RecentSearchEntity
|
||||||
import org.kiwix.kiwixmobile.database.newdb.entities.RecentSearchEntity_
|
import org.kiwix.kiwixmobile.database.newdb.entities.RecentSearchEntity_
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@ -51,6 +52,10 @@ class NewRecentSearchDao @Inject constructor(val box: Box<RecentSearchEntity>) {
|
|||||||
box.removeAll()
|
box.removeAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun migrationInsert(recentSearches: MutableList<RecentSearch>) {
|
||||||
|
box.put(recentSearches.map(::RecentSearchEntity))
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val NUM_RECENT_RESULTS = 5
|
const val NUM_RECENT_RESULTS = 5
|
||||||
}
|
}
|
||||||
|
@ -20,16 +20,17 @@ package org.kiwix.kiwixmobile.database.newdb.entities
|
|||||||
import io.objectbox.annotation.Entity
|
import io.objectbox.annotation.Entity
|
||||||
import io.objectbox.annotation.Id
|
import io.objectbox.annotation.Id
|
||||||
import org.kiwix.kiwixmobile.bookmark.BookmarkItem
|
import org.kiwix.kiwixmobile.bookmark.BookmarkItem
|
||||||
|
import org.kiwix.kiwixmobile.data.local.entity.Bookmark
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
data class BookmarkEntity(
|
data class BookmarkEntity(
|
||||||
@Id var id: Long = 0,
|
@Id var id: Long = 0,
|
||||||
val zimId: String,
|
val zimId: String,
|
||||||
var zimName: String,
|
var zimName: String,
|
||||||
var zimFilePath: String,
|
var zimFilePath: String?,
|
||||||
var bookmarkUrl: String,
|
var bookmarkUrl: String,
|
||||||
var bookmarkTitle: String,
|
var bookmarkTitle: String,
|
||||||
var favicon: String
|
var favicon: String?
|
||||||
) {
|
) {
|
||||||
constructor(item: BookmarkItem) : this(
|
constructor(item: BookmarkItem) : this(
|
||||||
item.databaseId,
|
item.databaseId,
|
||||||
@ -40,4 +41,20 @@ data class BookmarkEntity(
|
|||||||
item.bookmarkTitle,
|
item.bookmarkTitle,
|
||||||
item.favicon
|
item.favicon
|
||||||
)
|
)
|
||||||
|
|
||||||
|
private constructor(bookmark: Bookmark, zimFilePath: String?, favicon: String?) : this(
|
||||||
|
0,
|
||||||
|
bookmark.zimId,
|
||||||
|
bookmark.zimName,
|
||||||
|
zimFilePath,
|
||||||
|
bookmark.bookmarkUrl,
|
||||||
|
bookmark.bookmarkTitle,
|
||||||
|
favicon
|
||||||
|
)
|
||||||
|
|
||||||
|
constructor(bookmarkWithFavIconAndFile: Pair<Bookmark, Pair<String?, String?>>) : this(
|
||||||
|
bookmarkWithFavIconAndFile.first,
|
||||||
|
bookmarkWithFavIconAndFile.second.first,
|
||||||
|
bookmarkWithFavIconAndFile.second.second
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.database.newdb.entities
|
|||||||
import io.objectbox.annotation.Entity
|
import io.objectbox.annotation.Entity
|
||||||
import io.objectbox.annotation.Id
|
import io.objectbox.annotation.Id
|
||||||
import org.kiwix.kiwixmobile.data.ZimContentProvider
|
import org.kiwix.kiwixmobile.data.ZimContentProvider
|
||||||
|
import org.kiwix.kiwixmobile.data.local.entity.RecentSearch
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
data class RecentSearchEntity(
|
data class RecentSearchEntity(
|
||||||
@ -31,4 +32,10 @@ data class RecentSearchEntity(
|
|||||||
searchTerm = searchTerm,
|
searchTerm = searchTerm,
|
||||||
zimId = ZimContentProvider.getId()
|
zimId = ZimContentProvider.getId()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
constructor(recentSearch: RecentSearch) : this(
|
||||||
|
0,
|
||||||
|
recentSearch.searchString,
|
||||||
|
recentSearch.zimID
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
@ -33,8 +33,16 @@ import javax.inject.Singleton
|
|||||||
|
|
||||||
@Module
|
@Module
|
||||||
class DatabaseModule {
|
class DatabaseModule {
|
||||||
@Provides @Singleton fun providesBoxStore(context: Context): BoxStore =
|
companion object {
|
||||||
MyObjectBox.builder().androidContext(context.applicationContext).build()
|
var boxStore: BoxStore? = null
|
||||||
|
}
|
||||||
|
|
||||||
|
@Provides @Singleton fun providesBoxStore(context: Context): BoxStore {
|
||||||
|
if (boxStore == null) {
|
||||||
|
boxStore = MyObjectBox.builder().androidContext(context.applicationContext).build()
|
||||||
|
}
|
||||||
|
return boxStore!!
|
||||||
|
}
|
||||||
|
|
||||||
@Provides @Singleton fun providesNewDownloadDao(boxStore: BoxStore): NewDownloadDao =
|
@Provides @Singleton fun providesNewDownloadDao(boxStore: BoxStore): NewDownloadDao =
|
||||||
NewDownloadDao(boxStore.boxFor())
|
NewDownloadDao(boxStore.boxFor())
|
||||||
|
@ -15,6 +15,6 @@ fun ImageView.setBitmap(base64String: Base64String) {
|
|||||||
|
|
||||||
// methods that accept inline classes as parameters are not allowed to be called from java
|
// methods that accept inline classes as parameters are not allowed to be called from java
|
||||||
// hence this facade
|
// hence this facade
|
||||||
fun ImageView.setBitmapFromString(string: String) {
|
fun ImageView.setBitmapFromString(string: String?) {
|
||||||
setBitmap(Base64String(string))
|
setBitmap(Base64String(string))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user