Created ObjectBox to libkiwix migrator for Bookmarks.

This commit is contained in:
MohitMali 2023-08-25 18:42:00 +05:30 committed by MohitMaliFtechiz
parent 816d9d59b8
commit 4fe6d11596
4 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,61 @@
/*
* Kiwix Android
* Copyright (c) 2023 Kiwix <android.kiwix.org>
* 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 <http://www.gnu.org/licenses/>.
*
*/
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<BookmarkEntity>) {
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)
}
}

View File

@ -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

View File

@ -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<Int>
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")

View File

@ -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"
}
}