From 77bb5c2fcd9688d958081abe1808142e40313ecf Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sat, 22 Jun 2024 13:14:03 +0300 Subject: [PATCH] Support UNKNOWN manga source --- .../kotatsu/core/model/MangaSource.kt | 2 +- .../model/parcelable/ParcelableChapter.kt | 2 +- .../kotatsu/core/parser/EmptyParser.kt | 40 +++++++++++++++++++ .../kotatsu/core/parser/MangaLinkResolver.kt | 2 +- .../kotatsu/core/parser/MangaParser.kt | 8 ++-- .../kotatsu/details/ui/DetailsActivity.kt | 2 +- .../explore/data/MangaSourcesRepository.kt | 1 + 7 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 app/src/main/kotlin/org/koitharu/kotatsu/core/parser/EmptyParser.kt diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/model/MangaSource.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/model/MangaSource.kt index 5fee8ec2f..1de70d7c2 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/model/MangaSource.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/model/MangaSource.kt @@ -21,7 +21,7 @@ fun MangaSource(name: String): MangaSource { MangaSource.entries.forEach { if (it.name == name) return it } - return MangaSource.DUMMY + return MangaSource.UNKNOWN } fun MangaSource.isNsfw() = contentType == ContentType.HENTAI diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/model/parcelable/ParcelableChapter.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/model/parcelable/ParcelableChapter.kt index 3eb99abdc..8d47965db 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/model/parcelable/ParcelableChapter.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/model/parcelable/ParcelableChapter.kt @@ -25,7 +25,7 @@ data class ParcelableChapter( scanlator = parcel.readString(), uploadDate = parcel.readLong(), branch = parcel.readString(), - source = parcel.readSerializableCompat() ?: MangaSource.DUMMY, + source = parcel.readSerializableCompat() ?: MangaSource.UNKNOWN, ) ) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/EmptyParser.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/EmptyParser.kt new file mode 100644 index 000000000..cfea2d7ff --- /dev/null +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/EmptyParser.kt @@ -0,0 +1,40 @@ +package org.koitharu.kotatsu.core.parser + +import org.koitharu.kotatsu.core.exceptions.UnsupportedSourceException +import org.koitharu.kotatsu.parsers.MangaLoaderContext +import org.koitharu.kotatsu.parsers.MangaParser +import org.koitharu.kotatsu.parsers.config.ConfigKey +import org.koitharu.kotatsu.parsers.model.Manga +import org.koitharu.kotatsu.parsers.model.MangaChapter +import org.koitharu.kotatsu.parsers.model.MangaListFilter +import org.koitharu.kotatsu.parsers.model.MangaPage +import org.koitharu.kotatsu.parsers.model.MangaSource +import org.koitharu.kotatsu.parsers.model.MangaTag +import org.koitharu.kotatsu.parsers.model.SortOrder +import java.util.EnumSet + +/** + * This parser is just for parser development, it should not be used in releases + */ +class EmptyParser(context: MangaLoaderContext) : MangaParser(context, MangaSource.DUMMY) { + + override val configKeyDomain: ConfigKey.Domain + get() = ConfigKey.Domain("localhost") + + override val availableSortOrders: Set + get() = EnumSet.allOf(SortOrder::class.java) + + override suspend fun getDetails(manga: Manga): Manga = stub(manga) + + override suspend fun getList(offset: Int, filter: MangaListFilter?): List = stub(null) + + override suspend fun getPages(chapter: MangaChapter): List = stub(null) + + override suspend fun getAvailableTags(): Set = stub(null) + + override suspend fun getRelatedManga(seed: Manga): List = stub(seed) + + private fun stub(manga: Manga?): Nothing { + throw UnsupportedSourceException("This manga source is not supported", manga) + } +} diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaLinkResolver.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaLinkResolver.kt index 483776ef6..f1474aaa1 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaLinkResolver.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaLinkResolver.kt @@ -36,7 +36,7 @@ class MangaLinkResolver @Inject constructor( require(uri.pathSegments.singleOrNull() == "manga") { "Invalid url" } val sourceName = requireNotNull(uri.getQueryParameter("source")) { "Source is not specified" } val source = MangaSource(sourceName) - require(source != MangaSource.DUMMY) { "Manga source $sourceName is not supported" } + require(source != MangaSource.UNKNOWN) { "Manga source $sourceName is not supported" } val repo = repositoryFactory.create(source) return repo.findExact( url = uri.getQueryParameter("url"), diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaParser.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaParser.kt index 769ca8e52..09262377c 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaParser.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaParser.kt @@ -5,9 +5,9 @@ import org.koitharu.kotatsu.parsers.MangaParser import org.koitharu.kotatsu.parsers.model.MangaSource fun MangaParser(source: MangaSource, loaderContext: MangaLoaderContext): MangaParser { - return if (source == MangaSource.DUMMY) { - DummyParser(loaderContext) - } else { - loaderContext.newParserInstance(source) + return when (source) { + MangaSource.UNKNOWN -> EmptyParser(loaderContext) + MangaSource.DUMMY -> DummyParser(loaderContext) + else -> loaderContext.newParserInstance(source) } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/DetailsActivity.kt b/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/DetailsActivity.kt index edba01271..c62e78451 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/DetailsActivity.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/DetailsActivity.kt @@ -463,7 +463,7 @@ class DetailsActivity : imageViewState.isVisible = false } - if (manga.source == MangaSource.LOCAL || manga.source == MangaSource.DUMMY) { + if (manga.source == MangaSource.LOCAL || manga.source == MangaSource.UNKNOWN) { infoLayout.chipSource.isVisible = false } else { infoLayout.chipSource.text = manga.source.title diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt index fe9ae9ca1..485dbc1cd 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt @@ -36,6 +36,7 @@ class MangaSourcesRepository @Inject constructor( private val remoteSources = EnumSet.allOf(MangaSource::class.java).apply { remove(MangaSource.LOCAL) + remove(MangaSource.UNKNOWN) if (!BuildConfig.DEBUG) { remove(MangaSource.DUMMY) }