Clean up ComicInfo stuff a bit more

This commit is contained in:
arkon 2022-11-13 12:01:19 -05:00
parent 4f00af3173
commit 6017229d1b
2 changed files with 55 additions and 52 deletions

View File

@ -12,8 +12,7 @@ const val COMIC_INFO_FILE = "ComicInfo.xml"
/**
* Creates a ComicInfo instance based on the manga and chapter metadata.
*/
fun getComicInfo(manga: Manga, chapter: Chapter, chapterUrl: String): ComicInfo {
return ComicInfo(
fun getComicInfo(manga: Manga, chapter: Chapter, chapterUrl: String) = ComicInfo(
title = ComicInfo.Title(chapter.name),
series = ComicInfo.Series(manga.title),
web = ComicInfo.Web(chapterUrl),
@ -22,8 +21,8 @@ fun getComicInfo(manga: Manga, chapter: Chapter, chapterUrl: String): ComicInfo
penciller = manga.artist?.let { ComicInfo.Penciller(it) },
translator = chapter.scanlator?.let { ComicInfo.Translator(it) },
genre = manga.genre?.let { ComicInfo.Genre(it.joinToString()) },
publishingStatusTachiyomi = ComicInfo.PublishingStatusTachiyomi(
ComicInfoPublishingStatusMap.toComicInfoValue(manga.status),
publishingStatus = ComicInfo.PublishingStatusTachiyomi(
ComicInfoPublishingStatus.toComicInfoValue(manga.status),
),
inker = null,
colorist = null,
@ -31,6 +30,36 @@ fun getComicInfo(manga: Manga, chapter: Chapter, chapterUrl: String): ComicInfo
coverArtist = null,
tags = null,
)
fun SManga.copyFromComicInfo(comicInfo: ComicInfo) {
comicInfo.series?.let { title = it.value }
comicInfo.writer?.let { author = it.value }
comicInfo.summary?.let { description = it.value }
listOfNotNull(
comicInfo.genre?.value,
comicInfo.tags?.value,
)
.flatMap { it.split(", ") }
.distinct()
.joinToString(", ") { it.trim() }
.takeIf { it.isNotEmpty() }
?.let { genre = it }
listOfNotNull(
comicInfo.penciller?.value,
comicInfo.inker?.value,
comicInfo.colorist?.value,
comicInfo.letterer?.value,
comicInfo.coverArtist?.value,
)
.flatMap { it.split(", ") }
.distinct()
.joinToString(", ") { it.trim() }
.takeIf { it.isNotEmpty() }
?.let { artist = it }
status = ComicInfoPublishingStatus.toSMangaValue(comicInfo.publishingStatus?.value)
}
@Serializable
@ -49,7 +78,7 @@ data class ComicInfo(
val genre: Genre?,
val tags: Tags?,
val web: Web?,
val publishingStatusTachiyomi: PublishingStatusTachiyomi?,
val publishingStatus: PublishingStatusTachiyomi?,
) {
@Suppress("UNUSED")
@XmlElement(false)
@ -119,7 +148,7 @@ data class ComicInfo(
data class PublishingStatusTachiyomi(@XmlValue(true) val value: String = "")
}
enum class ComicInfoPublishingStatusMap(
private enum class ComicInfoPublishingStatus(
val comicInfoValue: String,
val sMangaModelValue: Int,
) {
@ -129,17 +158,18 @@ enum class ComicInfoPublishingStatusMap(
PUBLISHING_FINISHED("Publishing finished", SManga.PUBLISHING_FINISHED),
CANCELLED("Cancelled", SManga.CANCELLED),
ON_HIATUS("On hiatus", SManga.ON_HIATUS),
UNKNOWN("Unknown", SManga.UNKNOWN),
;
companion object {
fun toComicInfoValue(value: Long): String {
return values().firstOrNull { it.sMangaModelValue == value.toInt() }?.comicInfoValue
?: "Unknown"
?: UNKNOWN.comicInfoValue
}
fun toSMangaValue(value: String?): Int {
return values().firstOrNull { it.comicInfoValue == value }?.sMangaModelValue
?: SManga.UNKNOWN
?: UNKNOWN.sMangaModelValue
}
}
}

View File

@ -5,7 +5,7 @@ import com.github.junrar.Archive
import com.hippo.unifile.UniFile
import eu.kanade.domain.manga.model.COMIC_INFO_FILE
import eu.kanade.domain.manga.model.ComicInfo
import eu.kanade.domain.manga.model.ComicInfoPublishingStatusMap
import eu.kanade.domain.manga.model.copyFromComicInfo
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.source.model.Filter
import eu.kanade.tachiyomi.source.model.FilterList
@ -245,34 +245,7 @@ class LocalSource(
xml.decodeFromReader<ComicInfo>(it)
}
comicInfo.series?.let { manga.title = it.value }
comicInfo.writer?.let { manga.author = it.value }
comicInfo.summary?.let { manga.description = it.value }
listOfNotNull(
comicInfo.genre?.value,
comicInfo.tags?.value,
)
.flatMap { it.split(", ") }
.distinct()
.joinToString(", ") { it.trim() }
.takeIf { it.isNotEmpty() }
?.let { manga.genre = it }
listOfNotNull(
comicInfo.penciller?.value,
comicInfo.inker?.value,
comicInfo.colorist?.value,
comicInfo.letterer?.value,
comicInfo.coverArtist?.value,
)
.flatMap { it.split(", ") }
.distinct()
.joinToString(", ") { it.trim() }
.takeIf { it.isNotEmpty() }
?.let { manga.artist = it }
manga.status = ComicInfoPublishingStatusMap.toSMangaValue(comicInfo.publishingStatusTachiyomi?.value)
manga.copyFromComicInfo(comicInfo)
}
@Serializable