From 430714e67fdfbe9da6fc535b8311321e2825aed6 Mon Sep 17 00:00:00 2001 From: arkon Date: Thu, 24 Dec 2020 15:46:29 -0500 Subject: [PATCH] Find existing entry in MAL list when binding --- .../data/track/myanimelist/MyAnimeList.kt | 12 +++++- .../data/track/myanimelist/MyAnimeListApi.kt | 41 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt index 8145133b15..e784e9a6f7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt @@ -71,8 +71,16 @@ class MyAnimeList(private val context: Context, id: Int) : TrackService(id) { } override fun bind(track: Track): Observable { - // TODO: change this to call add and update like the other trackers? - return runAsObservable({ api.getListItem(track) }) + return runAsObservable({ + val remoteTrack = api.findListItem(track) + if (remoteTrack != null) { + track.copyPersonalFrom(remoteTrack) + track.media_id = remoteTrack.media_id + update(track) + } else { + add(track) + } + }) } override fun search(query: String): Observable> { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListApi.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListApi.kt index fa1845bc60..2d5fe38961 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListApi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListApi.kt @@ -16,6 +16,7 @@ import kotlinx.coroutines.awaitAll import kotlinx.coroutines.withContext import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.boolean +import kotlinx.serialization.json.contentOrNull import kotlinx.serialization.json.int import kotlinx.serialization.json.jsonArray import kotlinx.serialization.json.jsonObject @@ -162,6 +163,46 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI } } + suspend fun findListItem(track: Track, offset: Int = 0): Track? { + return withContext(Dispatchers.IO) { + val urlBuilder = "$baseApiUrl/users/@me/mangalist".toUri().buildUpon() + .appendQueryParameter("fields", "list_status") + .appendQueryParameter("limit", "25") + if (offset > 0) { + urlBuilder.appendQueryParameter("offset", offset.toString()) + } + + val request = Request.Builder() + .url(urlBuilder.build().toString()) + .get() + .build() + authClient.newCall(request) + .await() + .parseAs() + .let { + val obj = it.jsonObject + val trackedManga = obj["data"]!!.jsonArray.find { data -> + data.jsonObject["node"]!!.jsonObject["id"]!!.jsonPrimitive.int == track.media_id + } + + when { + // Found the item in the list + trackedManga != null -> { + parseMangaItem(trackedManga.jsonObject["list_status"]!!.jsonObject, track) + } + // Check next page if there's more + !obj["paging"]!!.jsonObject["next"]?.jsonPrimitive?.contentOrNull.isNullOrBlank() -> { + findListItem(track, offset + 25) + } + // No more pages to check, item wasn't found + else -> { + null + } + } + } + } + } + private fun parseMangaItem(response: JsonObject, track: Track): Track { val obj = response.jsonObject return track.apply {