From 391c8ab649568d02f6e27c827a56a64f9da97523 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Tue, 14 Oct 2025 14:04:17 +0300 Subject: [PATCH] Fix sync issues --- .../data/model/FavouriteCategorySyncDto.kt | 2 +- .../kotatsu/sync/data/model/SyncDto.kt | 6 +++--- .../kotatsu/sync/domain/SyncHelper.kt | 21 +++++++------------ 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/sync/data/model/FavouriteCategorySyncDto.kt b/app/src/main/kotlin/org/koitharu/kotatsu/sync/data/model/FavouriteCategorySyncDto.kt index 5a06fd8e7..3781dc067 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/sync/data/model/FavouriteCategorySyncDto.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/sync/data/model/FavouriteCategorySyncDto.kt @@ -34,7 +34,7 @@ data class FavouriteCategorySyncDto( put("created_at", createdAt) put("sort_key", sortKey) put("title", title) - put("order", order) + put("`order`", order) put("track", track) put("show_in_lib", isVisibleInLibrary) put("deleted_at", deletedAt) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/sync/data/model/SyncDto.kt b/app/src/main/kotlin/org/koitharu/kotatsu/sync/data/model/SyncDto.kt index f810d6d84..3fba7c9e6 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/sync/data/model/SyncDto.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/sync/data/model/SyncDto.kt @@ -5,8 +5,8 @@ import kotlinx.serialization.Serializable @Serializable data class SyncDto( - @SerialName("history") val history: List?, - @SerialName("categories") val categories: List?, - @SerialName("favourites") val favourites: List?, + @SerialName("history") val history: List? = null, + @SerialName("categories") val categories: List? = null, + @SerialName("favourites") val favourites: List? = null, @SerialName("timestamp") val timestamp: Long, ) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/sync/domain/SyncHelper.kt b/app/src/main/kotlin/org/koitharu/kotatsu/sync/domain/SyncHelper.kt index ff96992eb..70a6771ee 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/sync/domain/SyncHelper.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/sync/domain/SyncHelper.kt @@ -22,7 +22,6 @@ import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.Response -import okhttp3.internal.closeQuietly import okio.IOException import org.jetbrains.annotations.Blocking import org.koitharu.kotatsu.BuildConfig @@ -89,12 +88,12 @@ class SyncHelper @AssistedInject constructor( val response = httpClient.newCall(request).execute().parseDtoOrNull() response?.categories?.let { categories -> val categoriesResult = upsertFavouriteCategories(categories) - stats.numDeletes += categoriesResult.first().count?.toLong() ?: 0L + stats.numDeletes += categoriesResult.firstOrNull()?.count?.toLong() ?: 0L stats.numInserts += categoriesResult.drop(1).sumOf { it.count?.toLong() ?: 0L } } response?.favourites?.let { favourites -> val favouritesResult = upsertFavourites(favourites) - stats.numDeletes += favouritesResult.first().count?.toLong() ?: 0L + stats.numDeletes += favouritesResult.firstOrNull()?.count?.toLong() ?: 0L stats.numInserts += favouritesResult.drop(1).sumOf { it.count?.toLong() ?: 0L } stats.numEntries += stats.numInserts + stats.numDeletes } @@ -119,7 +118,7 @@ class SyncHelper @AssistedInject constructor( val response = httpClient.newCall(request).execute().parseDtoOrNull() response?.history?.let { history -> val result = upsertHistory(history) - stats.numDeletes += result.first().count?.toLong() ?: 0L + stats.numDeletes += result.firstOrNull()?.count?.toLong() ?: 0L stats.numInserts += result.drop(1).sumOf { it.count?.toLong() ?: 0L } stats.numEntries += stats.numInserts + stats.numDeletes } @@ -286,15 +285,11 @@ class SyncHelper @AssistedInject constructor( private fun uri(authority: String, table: String) = "content://$authority/$table".toUri() - private fun Response.parseDtoOrNull(): SyncDto? { - return try { - when { - !isSuccessful -> throw IOException(body?.string()) - code == HttpURLConnection.HTTP_NO_CONTENT -> null - else -> Json.decodeFromString(body?.string() ?: return null) - } - } finally { - closeQuietly() + private fun Response.parseDtoOrNull(): SyncDto? = use { + when { + !isSuccessful -> throw IOException(body.string()) + code == HttpURLConnection.HTTP_NO_CONTENT -> null + else -> Json.decodeFromString(body.string()) } }