From f63573f25fe5361c9c1a49a77b2c371e68072736 Mon Sep 17 00:00:00 2001 From: Andreas Date: Wed, 3 May 2023 16:33:05 +0200 Subject: [PATCH] Remove SourceData and use StubSource directly for database (#9429) --- .../java/eu/kanade/domain/DomainModule.kt | 6 +-- .../tachiyomi/extension/ExtensionManager.kt | 11 +++--- .../extension/api/ExtensionGithubApi.kt | 23 +++++------- .../tachiyomi/extension/model/Extension.kt | 37 ++++++++++--------- .../tachiyomi/source/AndroidSourceManager.kt | 30 +++++++-------- .../tachiyomi/source/SourceExtensions.kt | 3 +- .../tachiyomi/data/source/SourceMapper.kt | 6 +-- ...oryImpl.kt => StubSourceRepositoryImpl.kt} | 14 +++---- .../domain/source/model/SourceData.kt | 10 ----- .../domain/source/model/StubSource.kt | 14 +++---- .../source/repository/SourceDataRepository.kt | 12 ------ .../source/repository/StubSourceRepository.kt | 12 ++++++ 12 files changed, 81 insertions(+), 97 deletions(-) rename data/src/main/java/tachiyomi/data/source/{SourceDataRepositoryImpl.kt => StubSourceRepositoryImpl.kt} (54%) delete mode 100644 domain/src/main/java/tachiyomi/domain/source/model/SourceData.kt delete mode 100644 domain/src/main/java/tachiyomi/domain/source/repository/SourceDataRepository.kt create mode 100644 domain/src/main/java/tachiyomi/domain/source/repository/StubSourceRepository.kt diff --git a/app/src/main/java/eu/kanade/domain/DomainModule.kt b/app/src/main/java/eu/kanade/domain/DomainModule.kt index 6bb2b941f..67ae1c68d 100644 --- a/app/src/main/java/eu/kanade/domain/DomainModule.kt +++ b/app/src/main/java/eu/kanade/domain/DomainModule.kt @@ -21,8 +21,8 @@ import tachiyomi.data.chapter.ChapterRepositoryImpl import tachiyomi.data.history.HistoryRepositoryImpl import tachiyomi.data.manga.MangaRepositoryImpl import tachiyomi.data.release.ReleaseServiceImpl -import tachiyomi.data.source.SourceDataRepositoryImpl import tachiyomi.data.source.SourceRepositoryImpl +import tachiyomi.data.source.StubSourceRepositoryImpl import tachiyomi.data.track.TrackRepositoryImpl import tachiyomi.data.updates.UpdatesRepositoryImpl import tachiyomi.domain.category.interactor.CreateCategoryWithName @@ -61,8 +61,8 @@ import tachiyomi.domain.release.interactor.GetApplicationRelease import tachiyomi.domain.release.service.ReleaseService import tachiyomi.domain.source.interactor.GetRemoteManga import tachiyomi.domain.source.interactor.GetSourcesWithNonLibraryManga -import tachiyomi.domain.source.repository.SourceDataRepository import tachiyomi.domain.source.repository.SourceRepository +import tachiyomi.domain.source.repository.StubSourceRepository import tachiyomi.domain.track.interactor.DeleteTrack import tachiyomi.domain.track.interactor.GetTracks import tachiyomi.domain.track.interactor.GetTracksPerManga @@ -139,7 +139,7 @@ class DomainModule : InjektModule { addFactory { GetUpdates(get()) } addSingletonFactory { SourceRepositoryImpl(get(), get()) } - addSingletonFactory { SourceDataRepositoryImpl(get()) } + addSingletonFactory { StubSourceRepositoryImpl(get()) } addFactory { GetEnabledSources(get(), get()) } addFactory { GetLanguagesWithSources(get(), get()) } addFactory { GetRemoteManga(get()) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt index b5f4d45c1..2db34368d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt @@ -5,7 +5,6 @@ import android.graphics.drawable.Drawable import eu.kanade.domain.source.service.SourcePreferences import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.extension.api.ExtensionGithubApi -import eu.kanade.tachiyomi.extension.model.AvailableSources import eu.kanade.tachiyomi.extension.model.Extension import eu.kanade.tachiyomi.extension.model.InstallStep import eu.kanade.tachiyomi.extension.model.LoadResult @@ -22,7 +21,7 @@ import rx.Observable import tachiyomi.core.util.lang.launchNow import tachiyomi.core.util.lang.withUIContext import tachiyomi.core.util.system.logcat -import tachiyomi.domain.source.model.SourceData +import tachiyomi.domain.source.model.StubSource import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get import java.util.Locale @@ -73,12 +72,12 @@ class ExtensionManager( private val _availableExtensionsFlow = MutableStateFlow(emptyList()) val availableExtensionsFlow = _availableExtensionsFlow.asStateFlow() - private var availableExtensionsSourcesData: Map = emptyMap() + private var availableExtensionsSourcesData: Map = emptyMap() private fun setupAvailableExtensionsSourcesDataMap(extensions: List) { if (extensions.isEmpty()) return availableExtensionsSourcesData = extensions - .flatMap { ext -> ext.sources.map { it.toSourceData() } } + .flatMap { ext -> ext.sources.map { it.toStubSource() } } .associateBy { it.id } } @@ -145,8 +144,8 @@ class ExtensionManager( // Use the source lang as some aren't present on the extension level. val availableLanguages = extensions .flatMap(Extension.Available::sources) - .distinctBy(AvailableSources::lang) - .map(AvailableSources::lang) + .distinctBy(Extension.Available.Source::lang) + .map(Extension.Available.Source::lang) val deviceLanguage = Locale.getDefault().language val defaultLanguages = preferences.enabledLanguages().defaultValue() diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt index c9e381dd1..9d871a7ac 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt @@ -2,7 +2,6 @@ package eu.kanade.tachiyomi.extension.api import android.content.Context import eu.kanade.tachiyomi.extension.ExtensionManager -import eu.kanade.tachiyomi.extension.model.AvailableSources import eu.kanade.tachiyomi.extension.model.Extension import eu.kanade.tachiyomi.extension.model.LoadResult import eu.kanade.tachiyomi.extension.util.ExtensionLoader @@ -124,24 +123,13 @@ internal class ExtensionGithubApi { isNsfw = it.nsfw == 1, hasReadme = it.hasReadme == 1, hasChangelog = it.hasChangelog == 1, - sources = it.sources?.toExtensionSources().orEmpty(), + sources = it.sources?.map(extensionSourceMapper).orEmpty(), apkName = it.apk, iconUrl = "${getUrlPrefix()}icon/${it.apk.replace(".apk", ".png")}", ) } } - private fun List.toExtensionSources(): List { - return this.map { - AvailableSources( - id = it.id, - lang = it.lang, - name = it.name, - baseUrl = it.baseUrl, - ) - } - } - fun getApkUrl(extension: Extension.Available): String { return "${getUrlPrefix()}apk/${extension.apkName}" } @@ -183,3 +171,12 @@ private data class ExtensionSourceJsonObject( val name: String, val baseUrl: String, ) + +private val extensionSourceMapper: (ExtensionSourceJsonObject) -> Extension.Available.Source = { + Extension.Available.Source( + id = it.id, + lang = it.lang, + name = it.name, + baseUrl = it.baseUrl, + ) +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/model/Extension.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/model/Extension.kt index 7c21ee0bc..92a80e237 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/model/Extension.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/model/Extension.kt @@ -2,7 +2,7 @@ package eu.kanade.tachiyomi.extension.model import android.graphics.drawable.Drawable import eu.kanade.tachiyomi.source.Source -import tachiyomi.domain.source.model.SourceData +import tachiyomi.domain.source.model.StubSource sealed class Extension { @@ -44,10 +44,26 @@ sealed class Extension { override val isNsfw: Boolean, override val hasReadme: Boolean, override val hasChangelog: Boolean, - val sources: List, + val sources: List, val apkName: String, val iconUrl: String, - ) : Extension() + ) : Extension() { + + data class Source( + val id: Long, + val lang: String, + val name: String, + val baseUrl: String, + ) { + fun toStubSource(): StubSource { + return StubSource( + id = this.id, + lang = this.lang, + name = this.name, + ) + } + } + } data class Untrusted( override val name: String, @@ -62,18 +78,3 @@ sealed class Extension { override val hasChangelog: Boolean = false, ) : Extension() } - -data class AvailableSources( - val id: Long, - val lang: String, - val name: String, - val baseUrl: String, -) { - fun toSourceData(): SourceData { - return SourceData( - id = this.id, - lang = this.lang, - name = this.name, - ) - } -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/source/AndroidSourceManager.kt b/app/src/main/java/eu/kanade/tachiyomi/source/AndroidSourceManager.kt index ade85146b..a8255ba6e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/source/AndroidSourceManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/source/AndroidSourceManager.kt @@ -13,9 +13,8 @@ import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking -import tachiyomi.domain.source.model.SourceData import tachiyomi.domain.source.model.StubSource -import tachiyomi.domain.source.repository.SourceDataRepository +import tachiyomi.domain.source.repository.StubSourceRepository import tachiyomi.domain.source.service.SourceManager import tachiyomi.source.local.LocalSource import uy.kohesive.injekt.Injekt @@ -26,7 +25,7 @@ import java.util.concurrent.ConcurrentHashMap class AndroidSourceManager( private val context: Context, private val extensionManager: ExtensionManager, - private val sourceRepository: SourceDataRepository, + private val sourceRepository: StubSourceRepository, ) : SourceManager { private val downloadManager: DownloadManager by injectLazy() @@ -55,7 +54,7 @@ class AndroidSourceManager( extensions.forEach { extension -> extension.sources.forEach { mutableMap[it.id] = it - registerStubSource(it.toSourceData()) + registerStubSource(it.toStubSource()) } } sourcesMapFlow.value = mutableMap @@ -67,7 +66,7 @@ class AndroidSourceManager( .collectLatest { sources -> val mutableMap = stubSourcesMap.toMutableMap() sources.forEach { - mutableMap[it.id] = StubSource(it) + mutableMap[it.id] = it } } } @@ -92,26 +91,25 @@ class AndroidSourceManager( return stubSourcesMap.values.filterNot { it.id in onlineSourceIds } } - private fun registerStubSource(sourceData: SourceData) { + private fun registerStubSource(source: StubSource) { scope.launch { - val (id, lang, name) = sourceData - val dbSourceData = sourceRepository.getSourceData(id) - if (dbSourceData == sourceData) return@launch - sourceRepository.upsertSourceData(id, lang, name) - if (dbSourceData != null) { - downloadManager.renameSource(StubSource(dbSourceData), StubSource(sourceData)) + val dbSource = sourceRepository.getStubSource(source.id) + if (dbSource == source) return@launch + sourceRepository.upsertStubSource(source.id, source.lang, source.name) + if (dbSource != null) { + downloadManager.renameSource(dbSource, source) } } } private suspend fun createStubSource(id: Long): StubSource { - sourceRepository.getSourceData(id)?.let { - return StubSource(it) + sourceRepository.getStubSource(id)?.let { + return it } extensionManager.getSourceData(id)?.let { registerStubSource(it) - return StubSource(it) + return it } - return StubSource(SourceData(id, "", "")) + return StubSource(id, "", "") } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/source/SourceExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/source/SourceExtensions.kt index 85e722d9e..8f4faaaa9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/source/SourceExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/source/SourceExtensions.kt @@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.source import android.graphics.drawable.Drawable import eu.kanade.domain.source.service.SourcePreferences import eu.kanade.tachiyomi.extension.ExtensionManager -import tachiyomi.domain.source.model.SourceData import tachiyomi.domain.source.model.StubSource import tachiyomi.source.local.isLocal import uy.kohesive.injekt.Injekt @@ -13,7 +12,7 @@ fun Source.icon(): Drawable? = Injekt.get().getAppIconForSourc fun Source.getPreferenceKey(): String = "source_$id" -fun Source.toSourceData(): SourceData = SourceData(id = id, lang = lang, name = name) +fun Source.toStubSource(): StubSource = StubSource(id = id, lang = lang, name = name) fun Source.getNameForMangaInfo(): String { val preferences = Injekt.get() diff --git a/data/src/main/java/tachiyomi/data/source/SourceMapper.kt b/data/src/main/java/tachiyomi/data/source/SourceMapper.kt index 58ad123e9..d3bbf3f49 100644 --- a/data/src/main/java/tachiyomi/data/source/SourceMapper.kt +++ b/data/src/main/java/tachiyomi/data/source/SourceMapper.kt @@ -1,7 +1,7 @@ package tachiyomi.data.source import tachiyomi.domain.source.model.Source -import tachiyomi.domain.source.model.SourceData +import tachiyomi.domain.source.model.StubSource val sourceMapper: (eu.kanade.tachiyomi.source.Source) -> Source = { source -> Source( @@ -13,6 +13,6 @@ val sourceMapper: (eu.kanade.tachiyomi.source.Source) -> Source = { source -> ) } -val sourceDataMapper: (Long, String, String) -> SourceData = { id, lang, name -> - SourceData(id, lang, name) +val sourceDataMapper: (Long, String, String) -> StubSource = { id, lang, name -> + StubSource(id, lang, name) } diff --git a/data/src/main/java/tachiyomi/data/source/SourceDataRepositoryImpl.kt b/data/src/main/java/tachiyomi/data/source/StubSourceRepositoryImpl.kt similarity index 54% rename from data/src/main/java/tachiyomi/data/source/SourceDataRepositoryImpl.kt rename to data/src/main/java/tachiyomi/data/source/StubSourceRepositoryImpl.kt index 1e0e3bf1a..9dd068139 100644 --- a/data/src/main/java/tachiyomi/data/source/SourceDataRepositoryImpl.kt +++ b/data/src/main/java/tachiyomi/data/source/StubSourceRepositoryImpl.kt @@ -2,22 +2,22 @@ package tachiyomi.data.source import kotlinx.coroutines.flow.Flow import tachiyomi.data.DatabaseHandler -import tachiyomi.domain.source.model.SourceData -import tachiyomi.domain.source.repository.SourceDataRepository +import tachiyomi.domain.source.model.StubSource +import tachiyomi.domain.source.repository.StubSourceRepository -class SourceDataRepositoryImpl( +class StubSourceRepositoryImpl( private val handler: DatabaseHandler, -) : SourceDataRepository { +) : StubSourceRepository { - override fun subscribeAll(): Flow> { + override fun subscribeAll(): Flow> { return handler.subscribeToList { sourcesQueries.findAll(sourceDataMapper) } } - override suspend fun getSourceData(id: Long): SourceData? { + override suspend fun getStubSource(id: Long): StubSource? { return handler.awaitOneOrNull { sourcesQueries.findOne(id, sourceDataMapper) } } - override suspend fun upsertSourceData(id: Long, lang: String, name: String) { + override suspend fun upsertStubSource(id: Long, lang: String, name: String) { handler.await { sourcesQueries.upsert(id, lang, name) } } } diff --git a/domain/src/main/java/tachiyomi/domain/source/model/SourceData.kt b/domain/src/main/java/tachiyomi/domain/source/model/SourceData.kt deleted file mode 100644 index 5fe097f27..000000000 --- a/domain/src/main/java/tachiyomi/domain/source/model/SourceData.kt +++ /dev/null @@ -1,10 +0,0 @@ -package tachiyomi.domain.source.model - -data class SourceData( - val id: Long, - val lang: String, - val name: String, -) { - - val isMissingInfo: Boolean = name.isBlank() || lang.isBlank() -} diff --git a/domain/src/main/java/tachiyomi/domain/source/model/StubSource.kt b/domain/src/main/java/tachiyomi/domain/source/model/StubSource.kt index cc6645eb0..af4e57d99 100644 --- a/domain/src/main/java/tachiyomi/domain/source/model/StubSource.kt +++ b/domain/src/main/java/tachiyomi/domain/source/model/StubSource.kt @@ -7,13 +7,13 @@ import eu.kanade.tachiyomi.source.model.SManga import rx.Observable @Suppress("OverridingDeprecatedMember") -class StubSource(private val sourceData: SourceData) : Source { +class StubSource( + override val id: Long, + override val name: String, + override val lang: String, +) : Source { - override val id: Long = sourceData.id - - override val name: String = sourceData.name.ifBlank { id.toString() } - - override val lang: String = sourceData.lang + val isInvalid: Boolean = name.isBlank() || lang.isBlank() override suspend fun getMangaDetails(manga: SManga): SManga { throw SourceNotInstalledException() @@ -43,7 +43,7 @@ class StubSource(private val sourceData: SourceData) : Source { } override fun toString(): String { - return if (sourceData.isMissingInfo.not()) "$name (${lang.uppercase()})" else id.toString() + return if (isInvalid.not()) "$name (${lang.uppercase()})" else id.toString() } } diff --git a/domain/src/main/java/tachiyomi/domain/source/repository/SourceDataRepository.kt b/domain/src/main/java/tachiyomi/domain/source/repository/SourceDataRepository.kt deleted file mode 100644 index 4c0874789..000000000 --- a/domain/src/main/java/tachiyomi/domain/source/repository/SourceDataRepository.kt +++ /dev/null @@ -1,12 +0,0 @@ -package tachiyomi.domain.source.repository - -import kotlinx.coroutines.flow.Flow -import tachiyomi.domain.source.model.SourceData - -interface SourceDataRepository { - fun subscribeAll(): Flow> - - suspend fun getSourceData(id: Long): SourceData? - - suspend fun upsertSourceData(id: Long, lang: String, name: String) -} diff --git a/domain/src/main/java/tachiyomi/domain/source/repository/StubSourceRepository.kt b/domain/src/main/java/tachiyomi/domain/source/repository/StubSourceRepository.kt new file mode 100644 index 000000000..367182637 --- /dev/null +++ b/domain/src/main/java/tachiyomi/domain/source/repository/StubSourceRepository.kt @@ -0,0 +1,12 @@ +package tachiyomi.domain.source.repository + +import kotlinx.coroutines.flow.Flow +import tachiyomi.domain.source.model.StubSource + +interface StubSourceRepository { + fun subscribeAll(): Flow> + + suspend fun getStubSource(id: Long): StubSource? + + suspend fun upsertStubSource(id: Long, lang: String, name: String) +}