From 878008e93badbb1f61704f23a5e810fbff90d2f4 Mon Sep 17 00:00:00 2001 From: arkon Date: Thu, 14 Jan 2021 17:46:31 -0500 Subject: [PATCH] Reimplement MAL start/end date support --- .../data/track/myanimelist/MyAnimeList.kt | 2 ++ .../data/track/myanimelist/MyAnimeListApi.kt | 35 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 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 539777be44..bae8eda31a 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 @@ -33,6 +33,8 @@ class MyAnimeList(private val context: Context, id: Int) : TrackService(id) { override val name: String get() = "MyAnimeList" + override val supportsReadingDates: Boolean = true + override fun getLogo() = R.drawable.ic_tracker_mal override fun getLogoColor() = Color.rgb(46, 81, 162) 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 d7bc9986ee..8f2a97a0d7 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 @@ -132,6 +132,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI val formBody: RequestBody = FormBody.Builder() .add("status", "reading") .add("score", "0") + .add("start_date", convertToIsoDate(System.currentTimeMillis())!!) .build() val request = Request.Builder() .url(mangaUrl(track.media_id).toString()) @@ -146,15 +147,21 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI suspend fun updateItem(track: Track): Track { return withIOContext { - val formBody: RequestBody = FormBody.Builder() + val formBodyBuilder = FormBody.Builder() .add("status", track.toMyAnimeListStatus() ?: "reading") .add("is_rereading", (track.status == MyAnimeList.REREADING).toString()) .add("score", track.score.toString()) .add("num_chapters_read", track.last_chapter_read.toString()) - .build() + convertToIsoDate(track.started_reading_date)?.let { + formBodyBuilder.add("start_date", it) + } + convertToIsoDate(track.finished_reading_date)?.let { + formBodyBuilder.add("finish_date", it) + } + val request = Request.Builder() .url(mangaUrl(track.media_id).toString()) - .put(formBody) + .put(formBodyBuilder.build()) .build() authClient.newCall(request) .await() @@ -233,6 +240,28 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI status = if (isRereading) MyAnimeList.REREADING else getStatus(obj["status"]!!.jsonPrimitive.content) last_chapter_read = obj["num_chapters_read"]!!.jsonPrimitive.int score = obj["score"]!!.jsonPrimitive.int.toFloat() + obj["start_date"]?.let { + started_reading_date = parseDate(it.jsonPrimitive.content) + } + obj["finish_date"]?.let { + finished_reading_date = parseDate(it.jsonPrimitive.content) + } + } + } + + private fun parseDate(isoDate: String): Long { + return SimpleDateFormat("yyyy-MM-dd", Locale.US).parse(isoDate)?.time ?: 0L + } + + private fun convertToIsoDate(epochTime: Long): String? { + if (epochTime == 0L) { + return "" + } + return try { + val outputDf = SimpleDateFormat("yyyy-MM-dd", Locale.US) + outputDf.format(epochTime) + } catch (e: Exception) { + null } }