Migrate app settings to match main when backing up app settings

such as library sort, convert to a string, and restore it back as an int when restoring
This commit is contained in:
Jays2Kings 2023-10-25 13:10:38 -07:00
parent a21247290f
commit 6359cd8207
7 changed files with 68 additions and 6 deletions

View file

@ -17,6 +17,7 @@ import eu.kanade.tachiyomi.data.updater.AppUpdateJob
import eu.kanade.tachiyomi.extension.ExtensionUpdateJob
import eu.kanade.tachiyomi.network.PREF_DOH_CLOUDFLARE
import eu.kanade.tachiyomi.ui.library.LibraryPresenter
import eu.kanade.tachiyomi.ui.library.LibrarySort
import eu.kanade.tachiyomi.ui.reader.settings.OrientationType
import eu.kanade.tachiyomi.ui.recents.RecentsPresenter
import eu.kanade.tachiyomi.util.system.launchIO
@ -239,6 +240,21 @@ object Migrations {
}
}
}
if (oldVersion < 110) {
try {
val librarySortString = prefs.getString("library_sorting_mode", "")
if (!librarySortString.isNullOrEmpty()) {
prefs.edit {
remove("library_sorting_mode")
putInt(
"library_sorting_mode",
LibrarySort.deserialize(librarySortString).mainValue,
)
}
}
} catch (_: Exception) {
}
}
return true
}

View file

@ -46,6 +46,7 @@ import eu.kanade.tachiyomi.source.ConfigurableSource
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.preferenceKey
import eu.kanade.tachiyomi.source.sourcePreferences
import eu.kanade.tachiyomi.ui.library.LibrarySort
import kotlinx.serialization.protobuf.ProtoBuf
import okio.buffer
import okio.gzip
@ -244,6 +245,12 @@ class BackupCreator(val context: Context) {
private fun Map<String, *>.toBackupPreferences(): List<BackupPreference> {
return this.filterKeys { !Preference.isPrivate(it) }
.mapNotNull { (key, value) ->
// j2k fork differences
if (key == "library_sorting_mode" && value is Int) {
val stringValue = (LibrarySort.valueOf(value) ?: LibrarySort.Title).serialize()
return@mapNotNull BackupPreference(key, StringPreferenceValue(stringValue))
}
// end j2k fork differences
when (value) {
is Int -> BackupPreference(key, IntPreferenceValue(value))
is Long -> BackupPreference(key, LongPreferenceValue(value))

View file

@ -28,6 +28,7 @@ import eu.kanade.tachiyomi.data.preference.PreferenceStore
import eu.kanade.tachiyomi.extension.ExtensionUpdateJob
import eu.kanade.tachiyomi.source.model.SChapter
import eu.kanade.tachiyomi.source.sourcePreferences
import eu.kanade.tachiyomi.ui.library.LibrarySort
import eu.kanade.tachiyomi.util.BackupUtil
import eu.kanade.tachiyomi.util.system.createFileInCacheDir
import kotlinx.coroutines.coroutineScope
@ -385,6 +386,15 @@ class BackupRestorer(val context: Context, val notifier: BackupNotifier) {
) {
val prefs = preferenceStore.getAll()
toRestore.forEach { (key, value) ->
// j2k fork differences
if (key == "library_sorting_mode" && value is StringPreferenceValue &&
prefs[key] is Int?
) {
val intValue = LibrarySort.deserialize(value.value)
preferenceStore.getInt(key).set(intValue.mainValue)
return@forEach
}
// end j2k fork differences
when (value) {
is IntPreferenceValue -> {
if (prefs[key] is Int?) {

View file

@ -139,8 +139,6 @@ object PreferenceKeys {
const val showEmptyCategoriesFiltering = "show_empty_categories_filtering"
const val librarySortingMode = "library_sorting_mode"
const val automaticExtUpdates = "automatic_ext_updates"
const val installedExtensionsOrder = "installed_extensions_order"

View file

@ -287,7 +287,7 @@ class PreferencesHelper(val context: Context) {
fun showEmptyCategoriesWhileFiltering() = flowPrefs.getBoolean(Keys.showEmptyCategoriesFiltering, false)
fun librarySortingMode() = flowPrefs.getInt(Keys.librarySortingMode, 0)
fun librarySortingMode() = flowPrefs.getInt("library_sorting_mode", 0)
fun librarySortingAscending() = flowPrefs.getBoolean("library_sorting_ascending", true)

View file

@ -204,9 +204,7 @@ class LibraryPresenter(
preferences.lastUsedCategory().set(order)
val category = categories.find { it.order == order }?.id ?: return
currentCategory = category
view?.onNextLibraryUpdate(
sectionedLibraryItems[currentCategory] ?: blankItem(),
)
view?.onNextLibraryUpdate(sectionedLibraryItems[currentCategory] ?: blankItem())
}
fun blankItem(id: Int = currentCategory): List<LibraryItem> {

View file

@ -38,6 +38,19 @@ enum class LibrarySort(
val categoryValueDescending: Char
get() = if (this == DragAndDrop) 'D' else 'b' + catValue * 2
fun serialize(): String {
val type = when (this) {
LastRead -> "LAST_READ"
Unread -> "UNREAD_COUNT"
TotalChapters -> "TOTAL_CHAPTERS"
LatestChapter -> "LATEST_CHAPTER"
DateFetched -> "CHAPTER_FETCH_DATE"
DateAdded -> "DATE_ADDED"
else -> "ALPHABETICAL"
}
return "$type,ASCENDING"
}
@StringRes
fun stringRes(isDynamic: Boolean) = if (isDynamic) dynamicStringRes else stringRes
@ -56,6 +69,26 @@ enum class LibrarySort(
}
companion object {
fun deserialize(serialized: String): LibrarySort {
if (serialized.isEmpty()) return Title
return try {
val values = serialized.split(",")
when (values[0]) {
"ALPHABETICAL" -> Title
"LAST_READ" -> LastRead
"LAST_MANGA_UPDATE" -> LatestChapter
"UNREAD_COUNT" -> Unread
"TOTAL_CHAPTERS" -> TotalChapters
"LATEST_CHAPTER" -> LatestChapter
"CHAPTER_FETCH_DATE" -> DateFetched
"DATE_ADDED" -> DateAdded
else -> Title
}
} catch (e: Exception) {
Title
}
}
fun valueOf(value: Int) = entries.find { it.mainValue == value }
fun valueOf(char: Char?) = entries.find { it.categoryValue == char || it.categoryValueDescending == char }
}