Skip to content

Commit

Permalink
sync: Remove leftover SY changes
Browse files Browse the repository at this point in the history
* remove leftover SY code
  • Loading branch information
Caio99BR committed Jun 25, 2024
1 parent 8d21199 commit 6027314
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,10 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
return false
}

val inputData = workDataOf(
KEY_CATEGORY to category?.id,
)

val syncPreferences: SyncPreferences = Injekt.get()

// Always sync the data before library update if syncing is enabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import eu.kanade.tachiyomi.data.backup.create.BackupOptions
import eu.kanade.tachiyomi.data.backup.models.Backup
import eu.kanade.tachiyomi.data.backup.models.BackupChapter
import eu.kanade.tachiyomi.data.backup.models.BackupManga
import eu.kanade.tachiyomi.data.backup.models.BackupSerializer
import eu.kanade.tachiyomi.data.backup.restore.BackupRestoreJob
import eu.kanade.tachiyomi.data.backup.restore.RestoreOptions
import eu.kanade.tachiyomi.data.backup.restore.restorers.MangaRestorer
Expand Down Expand Up @@ -202,7 +201,7 @@ class SyncManager(
val cacheFile = File(context.cacheDir, "tachiyomi_sync_data.proto.gz")
return try {
cacheFile.outputStream().use { output ->
output.write(ProtoBuf.encodeToByteArray(BackupSerializer, backup))
output.write(ProtoBuf.encodeToByteArray(Backup.serializer(), backup))
Uri.fromFile(cacheFile)
}
} catch (e: IOException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import eu.kanade.tachiyomi.data.backup.models.BackupCategory
import eu.kanade.tachiyomi.data.backup.models.BackupChapter
import eu.kanade.tachiyomi.data.backup.models.BackupManga
import eu.kanade.tachiyomi.data.backup.models.BackupPreference
import eu.kanade.tachiyomi.data.backup.models.BackupSavedSearch
import eu.kanade.tachiyomi.data.backup.models.BackupSource
import eu.kanade.tachiyomi.data.backup.models.BackupSourcePreferences
import kotlinx.serialization.Serializable
Expand Down Expand Up @@ -55,12 +54,6 @@ abstract class SyncService(
remoteSyncData.backup?.backupSourcePreferences,
)

// SY -->
val mergedSavedSearchesList = mergeSavedSearchesLists(
localSyncData.backup?.backupSavedSearches,
remoteSyncData.backup?.backupSavedSearches,
)
// SY <--

// Create the merged Backup object
val mergedBackup = Backup(
Expand All @@ -70,9 +63,6 @@ abstract class SyncService(
backupPreferences = mergedPreferencesList,
backupSourcePreferences = mergedSourcePreferencesList,

// SY -->
backupSavedSearches = mergedSavedSearchesList,
// SY <--
)

// Create the merged SData object
Expand Down Expand Up @@ -455,61 +445,4 @@ abstract class SyncService(
val mergedPrefsMap = (localPrefs + remotePrefs).associateBy { it.key }
return mergedPrefsMap.values.toList()
}

// SY -->
private fun mergeSavedSearchesLists(
localSearches: List<BackupSavedSearch>?,
remoteSearches: List<BackupSavedSearch>?
): List<BackupSavedSearch> {
val logTag = "MergeSavedSearches"

// Define a function to create a composite key from a BackupSavedSearch
fun searchCompositeKey(search: BackupSavedSearch): String {
return "${search.name}|${search.source}"
}

// Create maps using the composite key
val localSearchMap = localSearches?.associateBy { searchCompositeKey(it) } ?: emptyMap()
val remoteSearchMap = remoteSearches?.associateBy { searchCompositeKey(it) } ?: emptyMap()

logcat(LogPriority.DEBUG, logTag) {
"Starting saved searches merge. Local saved searches: ${localSearches?.size}, " +
"Remote saved searches: ${remoteSearches?.size}"
}

// Merge both saved searches maps
val mergedSearches = (localSearchMap.keys + remoteSearchMap.keys).distinct().mapNotNull { compositeKey ->
val localSearch = localSearchMap[compositeKey]
val remoteSearch = remoteSearchMap[compositeKey]

logcat(LogPriority.DEBUG, logTag) {
"Processing saved search key: $compositeKey. Local search: ${localSearch != null}, " +
"Remote search: ${remoteSearch != null}"
}

when {
localSearch != null && remoteSearch == null -> {
logcat(LogPriority.DEBUG, logTag) { "Using local saved search: ${localSearch.name}." }
localSearch
}
remoteSearch != null && localSearch == null -> {
logcat(LogPriority.DEBUG, logTag) { "Using remote saved search: ${remoteSearch.name}." }
remoteSearch
}
else -> {
logcat(LogPriority.DEBUG, logTag) {
"No saved search found for composite key: $compositeKey. Skipping."
}
null
}
}
}

logcat(LogPriority.DEBUG, logTag) {
"Saved searches merge completed. Total merged saved searches: ${mergedSearches.size}"
}

return mergedSearches
}
// SY <--
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.data.sync.service
import android.content.Context
import eu.kanade.domain.sync.SyncPreferences
import eu.kanade.tachiyomi.data.backup.models.Backup
import eu.kanade.tachiyomi.data.backup.models.BackupSerializer
import eu.kanade.tachiyomi.data.sync.SyncNotifier
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.PUT
Expand Down Expand Up @@ -104,7 +103,7 @@ class SyncYomiSyncService(
}

return try {
val backup = protoBuf.decodeFromByteArray(BackupSerializer, byteArray)
val backup = ProtoBuf.decodeFromByteArray(Backup.serializer(), byteArray)
return Pair(SyncData(backup = backup), newETag)
} catch (_: SerializationException) {
logcat(LogPriority.INFO) {
Expand Down Expand Up @@ -147,7 +146,7 @@ class SyncYomiSyncService(
.writeTimeout(timeout, TimeUnit.SECONDS)
.build()

val byteArray = protoBuf.encodeToByteArray(BackupSerializer, backup)
val byteArray = ProtoBuf.encodeToByteArray(Backup.serializer(), backup)
if (byteArray.isEmpty()) {
throw IllegalStateException(context.stringResource(MR.strings.empty_backup_error))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,6 @@ fun PUT(
.cacheControl(cache)
.build()
}
fun PATCH(
url: String,
headers: Headers = DEFAULT_HEADERS,
body: RequestBody = DEFAULT_BODY,
cache: CacheControl = DEFAULT_CACHE_CONTROL,
): Request {
return Request.Builder()
.url(url)
.patch(body)
.headers(headers)
.cacheControl(cache)
.build()
}

fun DELETE(
url: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@ import logcat.logcat
inline fun Any.logcat(
priority: LogPriority = LogPriority.DEBUG,
throwable: Throwable? = null,
tag: String? = null,
message: () -> String = { "" },
) = logcat(priority = priority) {
var msg = message()
val logMessage = StringBuilder()

if (!tag.isNullOrEmpty()) {
logMessage.append("[$tag] ")
}

val msg = message()
logMessage.append(msg)

if (throwable != null) {
if (msg.isNotBlank()) msg += "\n"
msg += throwable.asLog()
if (msg.isNotBlank()) logMessage.append("\n")
logMessage.append(throwable.asLog())
}
msg

logMessage.toString()
}

0 comments on commit 6027314

Please sign in to comment.