From 8f38b4fe3027c10feefa6d0c276db245947ec29a Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sun, 7 Sep 2025 10:31:13 +0300 Subject: [PATCH] Replace DummyParser with TestMangaRepository --- .../core/parser/TestMangaRepository.kt | 57 +++++++++++++++++++ .../kotatsu/settings/DebugSettingsFragment.kt | 8 +++ app/src/debug/res/xml/pref_debug.xml | 12 +++- .../kotatsu/core/model/MangaSource.kt | 7 ++- .../kotatsu/core/parser/DummyParser.kt | 46 --------------- .../core/parser/EmptyMangaRepository.kt | 2 +- .../kotatsu/core/parser/MangaParser.kt | 12 ---- .../kotatsu/core/parser/MangaRepository.kt | 8 ++- .../explore/data/MangaSourcesRepository.kt | 6 +- app/src/main/res/values/strings.xml | 1 + .../core/parser/TestMangaRepository.kt | 11 ++++ .../core/parser/TestMangaRepository.kt | 11 ++++ .../kotatsu/reader/domain/ChapterPagesTest.kt | 3 +- gradle/libs.versions.toml | 2 +- 14 files changed, 115 insertions(+), 71 deletions(-) create mode 100644 app/src/debug/kotlin/org/koitharu/kotatsu/core/parser/TestMangaRepository.kt delete mode 100644 app/src/main/kotlin/org/koitharu/kotatsu/core/parser/DummyParser.kt delete mode 100644 app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaParser.kt create mode 100644 app/src/nightly/kotlin/org/koitharu/kotatsu/core/parser/TestMangaRepository.kt create mode 100644 app/src/release/kotlin/org/koitharu/kotatsu/core/parser/TestMangaRepository.kt diff --git a/app/src/debug/kotlin/org/koitharu/kotatsu/core/parser/TestMangaRepository.kt b/app/src/debug/kotlin/org/koitharu/kotatsu/core/parser/TestMangaRepository.kt new file mode 100644 index 000000000..74ecf6071 --- /dev/null +++ b/app/src/debug/kotlin/org/koitharu/kotatsu/core/parser/TestMangaRepository.kt @@ -0,0 +1,57 @@ +package org.koitharu.kotatsu.core.parser + +import org.koitharu.kotatsu.core.cache.MemoryContentCache +import org.koitharu.kotatsu.core.model.TestMangaSource +import org.koitharu.kotatsu.parsers.MangaLoaderContext +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.MangaListFilterCapabilities +import org.koitharu.kotatsu.parsers.model.MangaListFilterOptions +import org.koitharu.kotatsu.parsers.model.MangaPage +import org.koitharu.kotatsu.parsers.model.SortOrder +import java.util.EnumSet + +/* + This class is for parser development and testing purposes + You can open it in the app via Settings -> Debug + */ +class TestMangaRepository( + @Suppress("unused") private val loaderContext: MangaLoaderContext, + cache: MemoryContentCache +) : CachingMangaRepository(cache) { + + override val source = TestMangaSource + + override val sortOrders: Set = EnumSet.allOf(SortOrder::class.java) + + override var defaultSortOrder: SortOrder + get() = sortOrders.first() + set(value) = Unit + + override val filterCapabilities = MangaListFilterCapabilities() + + override suspend fun getFilterOptions() = MangaListFilterOptions() + + override suspend fun getList( + offset: Int, + order: SortOrder?, + filter: MangaListFilter? + ): List = TODO("Get manga list by filter") + + override suspend fun getDetailsImpl( + manga: Manga + ): Manga = TODO("Fetch manga details") + + override suspend fun getPagesImpl( + chapter: MangaChapter + ): List = TODO("Get pages for specific chapter") + + override suspend fun getPageUrl( + page: MangaPage + ): String = TODO("Return direct url of page image or page.url if it is already a direct url") + + override suspend fun getRelatedMangaImpl( + seed: Manga + ): List = TODO("Get list of related manga. This method is optional and parser library has a default implementation") +} diff --git a/app/src/debug/kotlin/org/koitharu/kotatsu/settings/DebugSettingsFragment.kt b/app/src/debug/kotlin/org/koitharu/kotatsu/settings/DebugSettingsFragment.kt index 317c3fcf2..bbd60b99e 100644 --- a/app/src/debug/kotlin/org/koitharu/kotatsu/settings/DebugSettingsFragment.kt +++ b/app/src/debug/kotlin/org/koitharu/kotatsu/settings/DebugSettingsFragment.kt @@ -5,6 +5,8 @@ import androidx.preference.Preference import leakcanary.LeakCanary import org.koitharu.kotatsu.KotatsuApp import org.koitharu.kotatsu.R +import org.koitharu.kotatsu.core.model.TestMangaSource +import org.koitharu.kotatsu.core.nav.router import org.koitharu.kotatsu.core.ui.BasePreferenceFragment import org.koitharu.kotatsu.settings.utils.SplitSwitchPreference import org.koitharu.workinspector.WorkInspector @@ -35,6 +37,11 @@ class DebugSettingsFragment : BasePreferenceFragment(R.string.debug), Preference true } + KEY_TEST_PARSER -> { + router.openList(TestMangaSource, null, null) + true + } + else -> super.onPreferenceTreeClick(preference) } @@ -60,5 +67,6 @@ class DebugSettingsFragment : BasePreferenceFragment(R.string.debug), Preference const val KEY_LEAK_CANARY = "leak_canary" const val KEY_WORK_INSPECTOR = "work_inspector" + const val KEY_TEST_PARSER = "test_parser" } } diff --git a/app/src/debug/res/xml/pref_debug.xml b/app/src/debug/res/xml/pref_debug.xml index b67896596..b0b858f64 100644 --- a/app/src/debug/res/xml/pref_debug.xml +++ b/app/src/debug/res/xml/pref_debug.xml @@ -1,17 +1,23 @@ + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto"> + + + 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 c63423806..0697f32c2 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 @@ -28,11 +28,15 @@ data object UnknownMangaSource : MangaSource { override val name = "UNKNOWN" } +data object TestMangaSource : MangaSource { + override val name = "TEST" +} + fun MangaSource(name: String?): MangaSource { when (name ?: return UnknownMangaSource) { UnknownMangaSource.name -> return UnknownMangaSource - LocalMangaSource.name -> return LocalMangaSource + TestMangaSource.name -> return TestMangaSource } if (name.startsWith("content:")) { val parts = name.substringAfter(':').splitTwoParts('/') ?: return UnknownMangaSource @@ -92,6 +96,7 @@ fun MangaSource.getSummary(context: Context): String? = when (val source = unwra fun MangaSource.getTitle(context: Context): String = when (val source = unwrap()) { is MangaParserSource -> source.title LocalMangaSource -> context.getString(R.string.local_storage) + TestMangaSource -> context.getString(R.string.test_parser) is ExternalMangaSource -> source.resolveName(context) else -> context.getString(R.string.unknown) } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/DummyParser.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/DummyParser.kt deleted file mode 100644 index 1d918e6f8..000000000 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/DummyParser.kt +++ /dev/null @@ -1,46 +0,0 @@ -package org.koitharu.kotatsu.core.parser - -import org.koitharu.kotatsu.core.exceptions.UnsupportedSourceException -import org.koitharu.kotatsu.parsers.MangaLoaderContext -import org.koitharu.kotatsu.parsers.config.ConfigKey -import org.koitharu.kotatsu.parsers.core.AbstractMangaParser -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.MangaListFilterCapabilities -import org.koitharu.kotatsu.parsers.model.MangaListFilterOptions -import org.koitharu.kotatsu.parsers.model.MangaPage -import org.koitharu.kotatsu.parsers.model.MangaParserSource -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 DummyParser(context: MangaLoaderContext) : AbstractMangaParser(context, MangaParserSource.DUMMY) { - - override val configKeyDomain: ConfigKey.Domain - get() = ConfigKey.Domain("localhost") - - override val availableSortOrders: Set - get() = EnumSet.allOf(SortOrder::class.java) - - override val filterCapabilities: MangaListFilterCapabilities - get() = MangaListFilterCapabilities() - - override suspend fun getDetails(manga: Manga): Manga = stub(manga) - - override suspend fun getFilterOptions(): MangaListFilterOptions = stub(null) - - override suspend fun getList( - offset: Int, - order: SortOrder, - filter: MangaListFilter - ): List = stub(null) - - override suspend fun getPages(chapter: MangaChapter): List = stub(null) - - private fun stub(manga: Manga?): Nothing { - throw UnsupportedSourceException("Usage of Dummy parser", manga) - } -} diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/EmptyMangaRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/EmptyMangaRepository.kt index af90994f8..c1252ce7b 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/EmptyMangaRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/EmptyMangaRepository.kt @@ -11,7 +11,7 @@ import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.parsers.model.SortOrder import java.util.EnumSet -class EmptyMangaRepository(override val source: MangaSource) : MangaRepository { +open class EmptyMangaRepository(override val source: MangaSource) : MangaRepository { override val sortOrders: Set get() = EnumSet.allOf(SortOrder::class.java) 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 deleted file mode 100644 index 3d12f5ecd..000000000 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaParser.kt +++ /dev/null @@ -1,12 +0,0 @@ -package org.koitharu.kotatsu.core.parser - -import org.koitharu.kotatsu.parsers.MangaLoaderContext -import org.koitharu.kotatsu.parsers.MangaParser -import org.koitharu.kotatsu.parsers.model.MangaParserSource - -fun MangaParser(source: MangaParserSource, loaderContext: MangaLoaderContext): MangaParser { - return when (source) { - MangaParserSource.DUMMY -> DummyParser(loaderContext) - else -> loaderContext.newParserInstance(source) - } -} diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaRepository.kt index 3b6431035..2e37bf5ab 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaRepository.kt @@ -7,6 +7,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext import org.koitharu.kotatsu.core.cache.MemoryContentCache import org.koitharu.kotatsu.core.model.LocalMangaSource import org.koitharu.kotatsu.core.model.MangaSourceInfo +import org.koitharu.kotatsu.core.model.TestMangaSource import org.koitharu.kotatsu.core.model.UnknownMangaSource import org.koitharu.kotatsu.core.parser.external.ExternalMangaRepository import org.koitharu.kotatsu.core.parser.external.ExternalMangaSource @@ -85,11 +86,16 @@ interface MangaRepository { private fun createRepository(source: MangaSource): MangaRepository? = when (source) { is MangaParserSource -> ParserMangaRepository( - parser = MangaParser(source, loaderContext), + parser = loaderContext.newParserInstance(source), cache = contentCache, mirrorSwitcher = mirrorSwitcher, ) + TestMangaSource -> TestMangaRepository( + loaderContext = loaderContext, + cache = contentCache, + ) + is ExternalMangaSource -> if (source.isAvailable(context)) { ExternalMangaRepository( contentResolver = context.contentResolver, 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 6f01a768b..79ce4661e 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 @@ -53,11 +53,7 @@ class MangaSourcesRepository @Inject constructor( get() = db.getSourcesDao() val allMangaSources: Set = Collections.unmodifiableSet( - EnumSet.allOf(MangaParserSource::class.java).apply { - if (!BuildConfig.DEBUG) { - remove(MangaParserSource.DUMMY) - } - }, + EnumSet.allOf(MangaParserSource::class.java) ) suspend fun getEnabledSources(): List { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7c56dd671..85bcb54af 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -879,4 +879,5 @@ This manga does not contain any chapters Failed to load chapter list Telegram integration + Test manga source diff --git a/app/src/nightly/kotlin/org/koitharu/kotatsu/core/parser/TestMangaRepository.kt b/app/src/nightly/kotlin/org/koitharu/kotatsu/core/parser/TestMangaRepository.kt new file mode 100644 index 000000000..3f4b072ed --- /dev/null +++ b/app/src/nightly/kotlin/org/koitharu/kotatsu/core/parser/TestMangaRepository.kt @@ -0,0 +1,11 @@ +package org.koitharu.kotatsu.core.parser + +import org.koitharu.kotatsu.core.cache.MemoryContentCache +import org.koitharu.kotatsu.core.model.TestMangaSource +import org.koitharu.kotatsu.parsers.MangaLoaderContext + +@Suppress("unused") +class TestMangaRepository( + private val loaderContext: MangaLoaderContext, + cache: MemoryContentCache +) : EmptyMangaRepository(TestMangaSource) diff --git a/app/src/release/kotlin/org/koitharu/kotatsu/core/parser/TestMangaRepository.kt b/app/src/release/kotlin/org/koitharu/kotatsu/core/parser/TestMangaRepository.kt new file mode 100644 index 000000000..3f4b072ed --- /dev/null +++ b/app/src/release/kotlin/org/koitharu/kotatsu/core/parser/TestMangaRepository.kt @@ -0,0 +1,11 @@ +package org.koitharu.kotatsu.core.parser + +import org.koitharu.kotatsu.core.cache.MemoryContentCache +import org.koitharu.kotatsu.core.model.TestMangaSource +import org.koitharu.kotatsu.parsers.MangaLoaderContext + +@Suppress("unused") +class TestMangaRepository( + private val loaderContext: MangaLoaderContext, + cache: MemoryContentCache +) : EmptyMangaRepository(TestMangaSource) diff --git a/app/src/test/kotlin/org/koitharu/kotatsu/reader/domain/ChapterPagesTest.kt b/app/src/test/kotlin/org/koitharu/kotatsu/reader/domain/ChapterPagesTest.kt index 6829c7b4c..7af0389aa 100644 --- a/app/src/test/kotlin/org/koitharu/kotatsu/reader/domain/ChapterPagesTest.kt +++ b/app/src/test/kotlin/org/koitharu/kotatsu/reader/domain/ChapterPagesTest.kt @@ -3,6 +3,7 @@ package org.koitharu.kotatsu.reader.domain import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Test +import org.koitharu.kotatsu.core.model.TestMangaSource import org.koitharu.kotatsu.parsers.model.MangaParserSource import org.koitharu.kotatsu.reader.ui.pager.ReaderPage import kotlin.random.Random @@ -73,6 +74,6 @@ class ChapterPagesTest { preview = null, chapterId = chapterId, index = Random.nextInt(), - source = MangaParserSource.DUMMY, + source = TestMangaSource, ) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 37f46641f..60c1f1d11 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -35,7 +35,7 @@ material = "1.14.0-alpha04" moshi = "1.15.2" okhttp = "5.1.0" okio = "3.16.0" -parsers = "19567f9642" +parsers = "fe5534b006" preference = "1.2.1" recyclerview = "1.4.0" room = "2.7.2"