From 6151318ac18aa816bb01170e101f33a53c19ed70 Mon Sep 17 00:00:00 2001 From: Gauthier Date: Fri, 10 Sep 2021 09:07:16 +0800 Subject: [PATCH] use chapter_number instead of ordinal index for syncChaptersWithTrackServiceTwoWay (#5846) use v2 api for Komga tracker for series --- .../tachiyomi/data/track/komga/KomgaApi.kt | 19 ++++++++++------ .../tachiyomi/data/track/komga/KomgaModels.kt | 22 +++++++++++++++++++ .../util/chapter/ChapterTrackSync.kt | 15 ++++--------- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/komga/KomgaApi.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/komga/KomgaApi.kt index d59efdec3..d24234599 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/komga/KomgaApi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/komga/KomgaApi.kt @@ -38,9 +38,11 @@ class KomgaApi(private val client: OkHttpClient) { } val progress = client - .newCall(GET("$url/read-progress/tachiyomi")) - .await() - .parseAs() + .newCall(GET("${url.replace("/api/v1/series/", "/api/v2/series/")}/read-progress/tachiyomi")) + .await().let { + if (url.contains("/api/v1/series/")) it.parseAs() + else it.parseAs().toV2() + } track.apply { cover_url = "$url/thumbnail" @@ -51,7 +53,7 @@ class KomgaApi(private val client: OkHttpClient) { progress.booksReadCount -> Komga.COMPLETED else -> Komga.READING } - last_chapter_read = progress.lastReadContinuousIndex.toFloat() + last_chapter_read = progress.lastReadContinuousNumberSort } } catch (e: Exception) { Timber.w(e, "Could not get item: $url") @@ -60,11 +62,14 @@ class KomgaApi(private val client: OkHttpClient) { } suspend fun updateProgress(track: Track): Track { - val progress = ReadProgressUpdateDto(track.last_chapter_read.toInt()) - val payload = json.encodeToString(progress) + val payload = if (track.tracking_url.contains("/api/v1/series/")) { + json.encodeToString(ReadProgressUpdateV2Dto(track.last_chapter_read)) + } else { + json.encodeToString(ReadProgressUpdateDto(track.last_chapter_read.toInt())) + } client.newCall( Request.Builder() - .url("${track.tracking_url}/read-progress/tachiyomi") + .url("${track.tracking_url.replace("/api/v1/series/", "/api/v2/series/")}/read-progress/tachiyomi") .put(payload.toRequestBody("application/json".toMediaType())) .build() ) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/komga/KomgaModels.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/komga/KomgaModels.kt index d27af7f16..2618adf8b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/komga/KomgaModels.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/komga/KomgaModels.kt @@ -63,6 +63,11 @@ data class ReadProgressUpdateDto( val lastBookRead: Int, ) +@Serializable +data class ReadProgressUpdateV2Dto( + val lastBookNumberSortRead: Float, +) + @Serializable data class ReadListDto( val id: String, @@ -80,4 +85,21 @@ data class ReadProgressDto( val booksUnreadCount: Int, val booksInProgressCount: Int, val lastReadContinuousIndex: Int, +) { + fun toV2() = ReadProgressV2Dto( + booksCount, + booksReadCount, + booksUnreadCount, + booksInProgressCount, + lastReadContinuousIndex.toFloat() + ) +} + +@Serializable +data class ReadProgressV2Dto( + val booksCount: Int, + val booksReadCount: Int, + val booksUnreadCount: Int, + val booksInProgressCount: Int, + val lastReadContinuousNumberSort: Float, ) diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterTrackSync.kt b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterTrackSync.kt index 192ce0711..34af53388 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterTrackSync.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterTrackSync.kt @@ -18,22 +18,15 @@ import timber.log.Timber fun syncChaptersWithTrackServiceTwoWay(db: DatabaseHelper, chapters: List, remoteTrack: Track, service: TrackService) { val sortedChapters = chapters.sortedBy { it.chapter_number } sortedChapters - .filterIndexed { index, chapter -> index < remoteTrack.last_chapter_read && !chapter.read } + .filter { chapter -> chapter.chapter_number <= remoteTrack.last_chapter_read && !chapter.read } .forEach { it.read = true } db.updateChaptersProgress(sortedChapters).executeAsBlocking() - // this uses the ordinal index of chapters instead of the chapter_number - // it was done that way because Track.last_chapter_read was an Int at the time, and Komga - // could have Float for the chapter number - // this will be addressed later on - val localLastRead = when { - sortedChapters.all { it.read } -> sortedChapters.size - sortedChapters.any { !it.read } -> sortedChapters.indexOfFirst { !it.read } - else -> 0 - } + // only take into account continuous reading + val localLastRead = sortedChapters.takeWhile { it.read }.lastOrNull()?.chapter_number ?: 0F // update remote - remoteTrack.last_chapter_read = localLastRead.toFloat() + remoteTrack.last_chapter_read = localLastRead launchIO { try {