Reimplement MAL start/end date support

This commit is contained in:
arkon 2021-01-14 17:46:31 -05:00
parent 0cd551d4fd
commit 878008e93b
2 changed files with 34 additions and 3 deletions

View File

@ -33,6 +33,8 @@ class MyAnimeList(private val context: Context, id: Int) : TrackService(id) {
override val name: String override val name: String
get() = "MyAnimeList" get() = "MyAnimeList"
override val supportsReadingDates: Boolean = true
override fun getLogo() = R.drawable.ic_tracker_mal override fun getLogo() = R.drawable.ic_tracker_mal
override fun getLogoColor() = Color.rgb(46, 81, 162) override fun getLogoColor() = Color.rgb(46, 81, 162)

View File

@ -132,6 +132,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
val formBody: RequestBody = FormBody.Builder() val formBody: RequestBody = FormBody.Builder()
.add("status", "reading") .add("status", "reading")
.add("score", "0") .add("score", "0")
.add("start_date", convertToIsoDate(System.currentTimeMillis())!!)
.build() .build()
val request = Request.Builder() val request = Request.Builder()
.url(mangaUrl(track.media_id).toString()) .url(mangaUrl(track.media_id).toString())
@ -146,15 +147,21 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
suspend fun updateItem(track: Track): Track { suspend fun updateItem(track: Track): Track {
return withIOContext { return withIOContext {
val formBody: RequestBody = FormBody.Builder() val formBodyBuilder = FormBody.Builder()
.add("status", track.toMyAnimeListStatus() ?: "reading") .add("status", track.toMyAnimeListStatus() ?: "reading")
.add("is_rereading", (track.status == MyAnimeList.REREADING).toString()) .add("is_rereading", (track.status == MyAnimeList.REREADING).toString())
.add("score", track.score.toString()) .add("score", track.score.toString())
.add("num_chapters_read", track.last_chapter_read.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() val request = Request.Builder()
.url(mangaUrl(track.media_id).toString()) .url(mangaUrl(track.media_id).toString())
.put(formBody) .put(formBodyBuilder.build())
.build() .build()
authClient.newCall(request) authClient.newCall(request)
.await() .await()
@ -233,6 +240,28 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
status = if (isRereading) MyAnimeList.REREADING else getStatus(obj["status"]!!.jsonPrimitive.content) status = if (isRereading) MyAnimeList.REREADING else getStatus(obj["status"]!!.jsonPrimitive.content)
last_chapter_read = obj["num_chapters_read"]!!.jsonPrimitive.int last_chapter_read = obj["num_chapters_read"]!!.jsonPrimitive.int
score = obj["score"]!!.jsonPrimitive.int.toFloat() 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
} }
} }