feat: add backupSource, backupPref, and "SY" backupSavedSearches.

I forgot to add the data into the merging logic, So remote always have empty value :(. Better late than never.
This commit is contained in:
KaiserBh 2024-01-12 09:26:42 +11:00
parent f5b10cd07f
commit 2f9f673326

View File

@ -5,6 +5,9 @@ import eu.kanade.tachiyomi.data.backup.models.Backup
import eu.kanade.tachiyomi.data.backup.models.BackupCategory import eu.kanade.tachiyomi.data.backup.models.BackupCategory
import eu.kanade.tachiyomi.data.backup.models.BackupChapter import eu.kanade.tachiyomi.data.backup.models.BackupChapter
import eu.kanade.tachiyomi.data.backup.models.BackupManga import eu.kanade.tachiyomi.data.backup.models.BackupManga
import eu.kanade.tachiyomi.data.backup.models.BackupPreference
import eu.kanade.tachiyomi.data.backup.models.BackupSource
import eu.kanade.tachiyomi.data.backup.models.BackupSourcePreferences
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import logcat.LogPriority import logcat.LogPriority
@ -62,17 +65,26 @@ abstract class SyncService(
* @param remoteSyncData The SData containing the remote sync data. * @param remoteSyncData The SData containing the remote sync data.
* @return The JSON string containing the merged sync data. * @return The JSON string containing the merged sync data.
*/ */
fun mergeSyncData(localSyncData: SyncData, remoteSyncData: SyncData): SyncData { private fun mergeSyncData(localSyncData: SyncData, remoteSyncData: SyncData): SyncData {
val mergedMangaList = mergeMangaLists(localSyncData.backup?.backupManga, remoteSyncData.backup?.backupManga) val mergedMangaList = mergeMangaLists(localSyncData.backup?.backupManga, remoteSyncData.backup?.backupManga)
val mergedCategoriesList = val mergedCategoriesList =
mergeCategoriesLists(localSyncData.backup?.backupCategories, remoteSyncData.backup?.backupCategories) mergeCategoriesLists(localSyncData.backup?.backupCategories, remoteSyncData.backup?.backupCategories)
val mergedSourcesList = mergeSourcesLists(localSyncData.backup?.backupSources, remoteSyncData.backup?.backupSources)
val mergedPreferencesList =
mergePreferencesLists(localSyncData.backup?.backupPreferences, remoteSyncData.backup?.backupPreferences)
val mergedSourcePreferencesList = mergeSourcePreferencesLists(
localSyncData.backup?.backupSourcePreferences,
remoteSyncData.backup?.backupSourcePreferences,
)
// Create the merged Backup object // Create the merged Backup object
val mergedBackup = Backup( val mergedBackup = Backup(
backupManga = mergedMangaList, backupManga = mergedMangaList,
backupCategories = mergedCategoriesList, backupCategories = mergedCategoriesList,
backupBrokenSources = localSyncData.backup?.backupBrokenSources ?: emptyList(), backupSources = mergedSourcesList,
backupSources = localSyncData.backup?.backupSources ?: emptyList(), backupPreferences = mergedPreferencesList,
backupSourcePreferences = mergedSourcePreferencesList,
) )
// Create the merged SData object // Create the merged SData object
@ -260,7 +272,9 @@ abstract class SyncService(
chosenChapter chosenChapter
} }
else -> { else -> {
logcat(LogPriority.DEBUG, logTag) { "No chapter found for composite key: $compositeKey. Skipping." } logcat(LogPriority.DEBUG, logTag) {
"No chapter found for composite key: $compositeKey. Skipping."
}
null null
} }
} }
@ -314,4 +328,160 @@ abstract class SyncService(
return mergedCategoriesMap.values.toList() return mergedCategoriesMap.values.toList()
} }
private fun mergeSourcesLists(
localSources: List<BackupSource>?,
remoteSources: List<BackupSource>?
): List<BackupSource> {
val logTag = "MergeSources"
// Create maps using sourceId as key
val localSourceMap = localSources?.associateBy { it.sourceId } ?: emptyMap()
val remoteSourceMap = remoteSources?.associateBy { it.sourceId } ?: emptyMap()
logcat(LogPriority.DEBUG, logTag) {
"Starting source merge. Local sources: ${localSources?.size}, Remote sources: ${remoteSources?.size}"
}
// Merge both source maps
val mergedSources = (localSourceMap.keys + remoteSourceMap.keys).distinct().mapNotNull { sourceId ->
val localSource = localSourceMap[sourceId]
val remoteSource = remoteSourceMap[sourceId]
logcat(LogPriority.DEBUG, logTag) {
"Processing source ID: $sourceId. Local source: ${localSource != null}, " +
"Remote source: ${remoteSource != null}"
}
when {
localSource != null && remoteSource == null -> {
logcat(LogPriority.DEBUG, logTag) { "Using local source: ${localSource.name}." }
localSource
}
remoteSource != null && localSource == null -> {
logcat(LogPriority.DEBUG, logTag) { "Using remote source: ${remoteSource.name}." }
remoteSource
}
else -> {
logcat(LogPriority.DEBUG, logTag) { "Remote and local is not empty: $sourceId. Skipping." }
null
}
}
}
logcat(LogPriority.DEBUG, logTag) { "Source merge completed. Total merged sources: ${mergedSources.size}" }
return mergedSources
}
private fun mergePreferencesLists(
localPreferences: List<BackupPreference>?,
remotePreferences: List<BackupPreference>?
): List<BackupPreference> {
val logTag = "MergePreferences"
// Create maps using key as the unique identifier
val localPreferencesMap = localPreferences?.associateBy { it.key } ?: emptyMap()
val remotePreferencesMap = remotePreferences?.associateBy { it.key } ?: emptyMap()
logcat(LogPriority.DEBUG, logTag) {
"Starting preferences merge. Local preferences: ${localPreferences?.size}, " +
"Remote preferences: ${remotePreferences?.size}"
}
// Merge both preferences maps
val mergedPreferences = (localPreferencesMap.keys + remotePreferencesMap.keys).distinct().mapNotNull { key ->
val localPreference = localPreferencesMap[key]
val remotePreference = remotePreferencesMap[key]
logcat(LogPriority.DEBUG, logTag) {
"Processing preference key: $key. Local preference: ${localPreference != null}, " +
"Remote preference: ${remotePreference != null}"
}
when {
localPreference != null && remotePreference == null -> {
logcat(LogPriority.DEBUG, logTag) { "Using local preference: ${localPreference.key}." }
localPreference
}
remotePreference != null && localPreference == null -> {
logcat(LogPriority.DEBUG, logTag) { "Using remote preference: ${remotePreference.key}." }
remotePreference
}
else -> {
logcat(LogPriority.DEBUG, logTag) { "Both remote and local have keys. Skipping: $key" }
null
}
}
}
logcat(LogPriority.DEBUG, logTag) {
"Preferences merge completed. Total merged preferences: ${mergedPreferences.size}"
}
return mergedPreferences
}
private fun mergeSourcePreferencesLists(
localPreferences: List<BackupSourcePreferences>?,
remotePreferences: List<BackupSourcePreferences>?
): List<BackupSourcePreferences> {
val logTag = "MergeSourcePreferences"
// Create maps using sourceKey as the unique identifier
val localPreferencesMap = localPreferences?.associateBy { it.sourceKey } ?: emptyMap()
val remotePreferencesMap = remotePreferences?.associateBy { it.sourceKey } ?: emptyMap()
logcat(LogPriority.DEBUG, logTag) {
"Starting source preferences merge. Local source preferences: ${localPreferences?.size}, " +
"Remote source preferences: ${remotePreferences?.size}"
}
// Merge both source preferences maps
val mergedSourcePreferences = (localPreferencesMap.keys + remotePreferencesMap.keys).distinct().mapNotNull { sourceKey ->
val localSourcePreference = localPreferencesMap[sourceKey]
val remoteSourcePreference = remotePreferencesMap[sourceKey]
logcat(LogPriority.DEBUG, logTag) {
"Processing source preference key: $sourceKey. " +
"Local source preference: ${localSourcePreference != null}, " +
"Remote source preference: ${remoteSourcePreference != null}"
}
when {
localSourcePreference != null && remoteSourcePreference == null -> {
logcat(LogPriority.DEBUG, logTag) {
"Using local source preference: ${localSourcePreference.sourceKey}."
}
localSourcePreference
}
remoteSourcePreference != null && localSourcePreference == null -> {
logcat(LogPriority.DEBUG, logTag) {
"Using remote source preference: ${remoteSourcePreference.sourceKey}."
}
remoteSourcePreference
}
localSourcePreference != null && remoteSourcePreference != null -> {
// Merge the individual preferences within the source preferences
val mergedPrefs = mergeIndividualPreferences(localSourcePreference.prefs, remoteSourcePreference.prefs)
BackupSourcePreferences(sourceKey, mergedPrefs)
}
else -> null
}
}
logcat(LogPriority.DEBUG, logTag) {
"Source preferences merge completed. Total merged source preferences: ${mergedSourcePreferences.size}"
}
return mergedSourcePreferences
}
private fun mergeIndividualPreferences(
localPrefs: List<BackupPreference>,
remotePrefs: List<BackupPreference>
): List<BackupPreference> {
val mergedPrefsMap = (localPrefs + remotePrefs).associateBy { it.key }
return mergedPrefsMap.values.toList()
}
} }