use chapter_number instead of ordinal index for syncChaptersWithTrackServiceTwoWay (#5846)

use v2 api for Komga tracker for series
This commit is contained in:
Gauthier 2021-09-10 09:07:16 +08:00 committed by GitHub
parent b45c322729
commit 6151318ac1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 18 deletions

View file

@ -38,9 +38,11 @@ class KomgaApi(private val client: OkHttpClient) {
}
val progress = client
.newCall(GET("$url/read-progress/tachiyomi"))
.await()
.parseAs<ReadProgressDto>()
.newCall(GET("${url.replace("/api/v1/series/", "/api/v2/series/")}/read-progress/tachiyomi"))
.await().let {
if (url.contains("/api/v1/series/")) it.parseAs<ReadProgressV2Dto>()
else it.parseAs<ReadProgressDto>().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()
)

View file

@ -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,
)

View file

@ -18,22 +18,15 @@ import timber.log.Timber
fun syncChaptersWithTrackServiceTwoWay(db: DatabaseHelper, chapters: List<Chapter>, 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 {