From 4fe6d11596172361a3cef98aa3c332cd3428e81b Mon Sep 17 00:00:00 2001 From: MohitMali Date: Fri, 25 Aug 2023 18:42:00 +0530 Subject: [PATCH] Created ObjectBox to libkiwix migrator for Bookmarks. --- .../remote/ObjectBoxToLibkiwixMigrator.kt | 61 +++++++++++++++++++ .../core/di/components/CoreComponent.kt | 3 + .../kiwixmobile/core/main/CoreMainActivity.kt | 8 +++ .../core/utils/SharedPreferenceUtil.kt | 7 +++ 4 files changed, 79 insertions(+) create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/data/remote/ObjectBoxToLibkiwixMigrator.kt diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/remote/ObjectBoxToLibkiwixMigrator.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/data/remote/ObjectBoxToLibkiwixMigrator.kt new file mode 100644 index 000000000..d3651b917 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/remote/ObjectBoxToLibkiwixMigrator.kt @@ -0,0 +1,61 @@ +/* + * Kiwix Android + * Copyright (c) 2023 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.core.data.remote + +import io.objectbox.Box +import io.objectbox.BoxStore +import io.objectbox.kotlin.boxFor +import io.objectbox.kotlin.query +import io.objectbox.query.QueryBuilder +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import org.kiwix.kiwixmobile.core.CoreApp +import org.kiwix.kiwixmobile.core.dao.entities.BookmarkEntity +import org.kiwix.kiwixmobile.core.dao.entities.BookmarkEntity_ +import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil +import javax.inject.Inject + +class ObjectBoxToLibkiwixMigrator { + @Inject lateinit var boxStore: BoxStore + @Inject lateinit var sharedPreferenceUtil: SharedPreferenceUtil + + fun migrateBookmarksToLibkiwix() { + CoreApp.coreComponent.inject(this) + migrateBookMarks(boxStore.boxFor()) + // TODO we will migrate here for other entities + } + + fun migrateBookMarks(box: Box) { + val bookMarksList = box.all + bookMarksList.forEachIndexed { _, bookmarkEntity -> + CoroutineScope(Dispatchers.IO).launch { + // removing the single entity from the object box after migration. + box.query { + equal( + BookmarkEntity_.bookmarkUrl, + bookmarkEntity.bookmarkUrl, + QueryBuilder.StringOrder.CASE_INSENSITIVE + ) + }.remove() + } + } + sharedPreferenceUtil.putPrefBookMarkMigrated(true) + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt index c207a7d8d..a7077be32 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt @@ -37,6 +37,7 @@ import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao import org.kiwix.kiwixmobile.core.data.DataModule import org.kiwix.kiwixmobile.core.data.DataSource import org.kiwix.kiwixmobile.core.data.remote.KiwixService +import org.kiwix.kiwixmobile.core.data.remote.ObjectBoxToLibkiwixMigrator import org.kiwix.kiwixmobile.core.di.modules.ApplicationModule import org.kiwix.kiwixmobile.core.di.modules.CoreViewModelModule import org.kiwix.kiwixmobile.core.di.modules.JNIModule @@ -93,6 +94,7 @@ interface CoreComponent { fun newBookmarksDao(): NewBookmarksDao fun connectivityManager(): ConnectivityManager fun wifiManager(): WifiManager + fun objectBoxToLibkiwixMigrator(): ObjectBoxToLibkiwixMigrator fun context(): Context fun downloader(): Downloader fun notificationManager(): NotificationManager @@ -104,6 +106,7 @@ interface CoreComponent { fun inject(errorActivity: ErrorActivity) fun inject(searchFragment: SearchFragment) + fun inject(objectBoxToLibkiwixMigrator: ObjectBoxToLibkiwixMigrator) fun inject(settingsFragment: CoreSettingsFragment) fun coreServiceComponent(): CoreServiceComponent.Builder diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt index ad4f9f1b2..a937bff6b 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt @@ -43,6 +43,7 @@ import org.kiwix.kiwixmobile.core.CoreApp import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.base.BaseActivity import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions +import org.kiwix.kiwixmobile.core.data.remote.ObjectBoxToLibkiwixMigrator import org.kiwix.kiwixmobile.core.di.components.CoreActivityComponent import org.kiwix.kiwixmobile.core.error.ErrorActivity import org.kiwix.kiwixmobile.core.extensions.browserIntent @@ -85,6 +86,7 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider { abstract val topLevelDestinations: Set abstract val navHostContainer: FragmentContainerView abstract val mainActivity: AppCompatActivity + @Inject lateinit var objectBoxToLibkiwixMigrator: ObjectBoxToLibkiwixMigrator override fun onCreate(savedInstanceState: Bundle?) { setTheme(R.style.KiwixTheme) @@ -104,7 +106,13 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider { exitProcess(KIWIX_INTERNAL_ERROR) } } +<<<<<<< HEAD setMainActivityToCoreApp() +======= + if (!sharedPreferenceUtil.prefIsBookmarksMigrated) { + objectBoxToLibkiwixMigrator.migrateBookmarksToLibkiwix() + } +>>>>>>> 371eb5c2d (Created ObjectBox to libkiwix migrator for Bookmarks) } @Suppress("DEPRECATION") diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt index 212233689..9089be233 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt @@ -90,6 +90,9 @@ class SharedPreferenceUtil @Inject constructor(val context: Context) { val prefDeviceDefaultLanguage: String get() = sharedPreferences.getString(PREF_DEVICE_DEFAULT_LANG, "") ?: "" + val prefIsBookmarksMigrated: Boolean + get() = sharedPreferences.getBoolean(PREF_BOOKMARKS_MIGRATED, false) + val prefStorage: String get() { val storage = sharedPreferences.getString(PREF_STORAGE, null) @@ -115,6 +118,9 @@ class SharedPreferenceUtil @Inject constructor(val context: Context) { fun getPrefStorageTitle(defaultTitle: String): String = sharedPreferences.getString(PREF_STORAGE_TITLE, defaultTitle) ?: defaultTitle + fun putPrefBookMarkMigrated(isMigrated: Boolean) = + sharedPreferences.edit { putBoolean(PREF_BOOKMARKS_MIGRATED, isMigrated) } + fun putPrefLanguage(language: String) = sharedPreferences.edit { putString(PREF_LANG, language) } @@ -273,5 +279,6 @@ class SharedPreferenceUtil @Inject constructor(val context: Context) { const val PREF_SHOW_MANAGE_PERMISSION_DIALOG_ON_REFRESH = "pref_show_manage_external_files" const val IS_PLAY_STORE_BUILD = "is_play_store_build" const val PREF_PLAY_STORE_RESTRICTION = "pref_play_store_restriction" + const val PREF_BOOKMARKS_MIGRATED = "pref_bookmarks_migrated" } }