Adapt source implementation for TachiyomiX 1.6 (#3243)

This commit is contained in:
AntsyLich
2026-06-14 02:34:31 +06:00
committed by GitHub
parent 430b13bb81
commit 4c37f4c764
38 changed files with 597 additions and 524 deletions
@@ -1,9 +1,7 @@
package mihon.domain.migration.usecases
import eu.kanade.domain.chapter.interactor.SyncChaptersWithSource
import eu.kanade.domain.manga.interactor.UpdateManga
import eu.kanade.domain.manga.model.hasCustomCover
import eu.kanade.domain.manga.model.toSManga
import eu.kanade.domain.source.service.SourcePreferences
import eu.kanade.tachiyomi.data.cache.CoverCache
import eu.kanade.tachiyomi.data.download.DownloadManager
@@ -11,6 +9,7 @@ import eu.kanade.tachiyomi.data.track.EnhancedTracker
import eu.kanade.tachiyomi.data.track.TrackerManager
import kotlinx.coroutines.CancellationException
import mihon.domain.migration.models.MigrationFlag
import mihon.domain.source.interactor.UpdateMangaFromRemote
import tachiyomi.domain.category.interactor.GetCategories
import tachiyomi.domain.category.interactor.SetMangaCategories
import tachiyomi.domain.chapter.interactor.GetChaptersByMangaId
@@ -30,13 +29,13 @@ class MigrateMangaUseCase(
private val downloadManager: DownloadManager,
private val updateManga: UpdateManga,
private val getChaptersByMangaId: GetChaptersByMangaId,
private val syncChaptersWithSource: SyncChaptersWithSource,
private val updateChapter: UpdateChapter,
private val getCategories: GetCategories,
private val setMangaCategories: SetMangaCategories,
private val getTracks: GetTracks,
private val insertTrack: InsertTrack,
private val coverCache: CoverCache,
private val updateMangaFromRemote: UpdateMangaFromRemote,
) {
private val enhancedServices by lazy { trackerManager.trackers.filterIsInstance<EnhancedTracker>() }
@@ -46,13 +45,7 @@ class MigrateMangaUseCase(
val flags = sourcePreferences.migrationFlags.get()
try {
val chapters = targetSource.getChapterList(target.toSManga())
try {
syncChaptersWithSource.await(chapters, target, targetSource)
} catch (_: Exception) {
// Worst case, chapters won't be synced
}
updateMangaFromRemote(target, fetchChapters = true).getOrThrow()
// Update chapters read, bookmark and dateFetch
if (MigrationFlag.CHAPTER in flags) {
@@ -0,0 +1,142 @@
package mihon.domain.source.interactor
import eu.kanade.domain.chapter.interactor.SyncChaptersWithSource
import eu.kanade.domain.chapter.model.toSChapter
import eu.kanade.domain.manga.model.hasCustomCover
import eu.kanade.domain.manga.model.toSManga
import eu.kanade.tachiyomi.data.cache.CoverCache
import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.model.SManga
import logcat.LogPriority
import mihon.domain.source.models.RemoteMangaUpdate
import tachiyomi.core.common.util.lang.withIOContext
import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.chapter.model.Chapter
import tachiyomi.domain.chapter.repository.ChapterRepository
import tachiyomi.domain.library.service.LibraryPreferences
import tachiyomi.domain.manga.model.Manga
import tachiyomi.domain.manga.model.MangaUpdate
import tachiyomi.domain.manga.repository.MangaRepository
import tachiyomi.domain.source.service.SourceManager
import tachiyomi.source.local.isLocal
import java.time.Instant
class UpdateMangaFromRemote(
private val sourceManager: SourceManager,
private val chapterRepository: ChapterRepository,
private val mangaRepository: MangaRepository,
private val syncChaptersWithSource: SyncChaptersWithSource,
private val coverCache: CoverCache,
private val libraryPreferences: LibraryPreferences,
private val downloadManager: DownloadManager,
) {
suspend operator fun invoke(
manga: Manga,
fetchDetails: Boolean = false,
fetchChapters: Boolean = false,
manualFetch: Boolean = false,
fetchWindow: Pair<Long, Long> = Pair(0, 0),
): Result<RemoteMangaUpdate> {
val source = sourceManager.getOrStub(manga.source)
return invoke(
source = source,
manga = manga,
fetchDetails = fetchDetails,
fetchChapters = fetchChapters,
manualFetch = manualFetch,
)
}
suspend operator fun invoke(
source: Source,
manga: Manga,
fetchDetails: Boolean = false,
fetchChapters: Boolean = false,
manualFetch: Boolean = false,
fetchWindow: Pair<Long, Long> = Pair(0, 0),
): Result<RemoteMangaUpdate> {
return try {
val chapters = chapterRepository.getChapterByMangaId(manga.id)
val update = withIOContext {
source.getMangaUpdate(
manga = manga.toSManga(),
chapters = chapters.map(Chapter::toSChapter),
fetchDetails = fetchDetails,
fetchChapters = fetchChapters,
)
}
awaitUpdateFromSource(manga, update.manga, manualFetch)
val newChapters = syncChaptersWithSource.await(
rawSourceChapters = update.chapters,
manga = manga,
source = source,
manualFetch = manualFetch,
fetchWindow = fetchWindow,
)
val updatedManga = mangaRepository.getMangaById(manga.id)
Result.success(RemoteMangaUpdate(manga = updatedManga, newChapters = newChapters))
} catch (e: Exception) {
logcat(LogPriority.ERROR, e)
Result.failure(e)
}
}
private suspend fun awaitUpdateFromSource(
localManga: Manga,
remoteManga: SManga,
manualFetch: Boolean,
): Boolean {
val remoteTitle = try {
remoteManga.title
} catch (_: UninitializedPropertyAccessException) {
""
}
// if the manga isn't a favorite (or 'update titles' preference is enabled), set its title from source and update in db
val title =
if (remoteTitle.isNotEmpty() && (!localManga.favorite || libraryPreferences.updateMangaTitles.get())) {
remoteTitle
} else {
null
}
val coverLastModified = when {
// Never refresh covers if the url is empty to avoid "losing" existing covers
remoteManga.thumbnail_url.isNullOrEmpty() -> null
!manualFetch && localManga.thumbnailUrl == remoteManga.thumbnail_url -> null
localManga.isLocal() -> Instant.now().toEpochMilli()
localManga.hasCustomCover(coverCache) -> {
coverCache.deleteFromCache(localManga, false)
null
}
else -> {
coverCache.deleteFromCache(localManga, false)
Instant.now().toEpochMilli()
}
}
val thumbnailUrl = remoteManga.thumbnail_url?.takeIf { it.isNotEmpty() }
val success = mangaRepository.update(
MangaUpdate(
id = localManga.id,
title = title,
coverLastModified = coverLastModified,
author = remoteManga.author,
artist = remoteManga.artist,
description = remoteManga.description,
genre = remoteManga.getGenres(),
thumbnailUrl = thumbnailUrl,
status = remoteManga.status.toLong(),
updateStrategy = remoteManga.update_strategy,
initialized = true,
),
)
if (success && title != null) {
downloadManager.renameManga(localManga, title)
}
return success
}
}
@@ -0,0 +1,9 @@
package mihon.domain.source.models
import tachiyomi.domain.chapter.model.Chapter
import tachiyomi.domain.manga.model.Manga
data class RemoteMangaUpdate(
val manga: Manga,
val newChapters: List<Chapter>,
)
@@ -344,7 +344,7 @@ class MigrationConfigScreen(private val mangaIds: Collection<Long>) : Screen() {
val includedSources = sourcePreferences.migrationSources.get()
val disabledSources = sourcePreferences.disabledSources.get()
.mapNotNull { it.toLongOrNull() }
val sources = sourceManager.getCatalogueSources()
val sources = sourceManager.getAll()
.asSequence()
.filterIsInstance<HttpSource>()
.filter { it.lang in languages }
@@ -4,10 +4,8 @@ import androidx.annotation.FloatRange
import cafe.adriel.voyager.core.model.StateScreenModel
import cafe.adriel.voyager.core.model.screenModelScope
import eu.kanade.domain.chapter.interactor.SyncChaptersWithSource
import eu.kanade.domain.manga.interactor.UpdateManga
import eu.kanade.domain.manga.model.toSManga
import eu.kanade.domain.source.service.SourcePreferences
import eu.kanade.tachiyomi.source.CatalogueSource
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.getNameForMangaInfo
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Job
@@ -24,6 +22,7 @@ import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit
import logcat.LogPriority
import mihon.domain.migration.usecases.MigrateMangaUseCase
import mihon.domain.source.interactor.UpdateMangaFromRemote
import mihon.feature.migration.list.models.MigratingManga
import mihon.feature.migration.list.models.MigratingManga.SearchResult
import mihon.feature.migration.list.search.SmartSourceSearchEngine
@@ -45,10 +44,10 @@ class MigrationListScreenModel(
private val sourceManager: SourceManager = Injekt.get(),
private val getManga: GetManga = Injekt.get(),
private val networkToLocalManga: NetworkToLocalManga = Injekt.get(),
private val updateManga: UpdateManga = Injekt.get(),
private val syncChaptersWithSource: SyncChaptersWithSource = Injekt.get(),
private val getChaptersByMangaId: GetChaptersByMangaId = Injekt.get(),
private val migrateManga: MigrateMangaUseCase = Injekt.get(),
private val updateMangaFromRemote: UpdateMangaFromRemote = Injekt.get(),
) : StateScreenModel<MigrationListScreenModel.State>(State()) {
private val smartSearchEngine = SmartSourceSearchEngine(extraSearchQuery)
@@ -110,7 +109,7 @@ class MigrationListScreenModel(
val deepSearchMode = preferences.migrationDeepSearchMode.get()
val sources = preferences.migrationSources.get()
.mapNotNull { sourceManager.get(it) as? CatalogueSource }
.mapNotNull { sourceManager.get(it) }
for (manga in mangas) {
if (!currentCoroutineContext().isActive) break
@@ -148,8 +147,7 @@ class MigrationListScreenModel(
if (result != null && result.first.thumbnailUrl == null) {
try {
val newManga = sourceManager.getOrStub(result.first.source).getMangaDetails(result.first.toSManga())
updateManga.awaitUpdateFromSource(result.first, newManga, true)
updateMangaFromRemote(result.first, fetchDetails = true, manualFetch = true).getOrThrow().manga
} catch (e: CancellationException) {
throw e
} catch (_: Exception) {
@@ -174,7 +172,7 @@ class MigrationListScreenModel(
private suspend fun searchSource(
manga: Manga,
source: CatalogueSource,
source: Source,
deepSearchMode: Boolean,
): Pair<Manga, ChapterInfo>? {
return try {
@@ -188,8 +186,7 @@ class MigrationListScreenModel(
val localManga = networkToLocalManga(searchResult)
try {
val chapters = source.getChapterList(localManga.toSManga())
syncChaptersWithSource.await(chapters, localManga, source)
updateMangaFromRemote(localManga, fetchChapters = true).getOrThrow()
} catch (e: Exception) {
logcat(LogPriority.ERROR, e)
}
@@ -224,12 +221,10 @@ class MigrationListScreenModel(
val manga = getManga.await(target) ?: return@async null
try {
val source = sourceManager.get(manga.source)!!
val chapters = source.getChapterList(manga.toSManga())
syncChaptersWithSource.await(chapters, manga, source)
updateMangaFromRemote(source = source, manga = manga, fetchChapters = true).getOrThrow().manga
} catch (_: Exception) {
return@async null
null
}
manga
}
.await()
@@ -239,13 +234,6 @@ class MigrationListScreenModel(
return@launchIO
}
try {
val newManga = sourceManager.getOrStub(result.source).getMangaDetails(result.toSManga())
updateManga.awaitUpdateFromSource(result, newManga, true)
} catch (e: CancellationException) {
throw e
} catch (_: Exception) {
}
migratingManga.searchResult.value = result.toSuccessSearchResult()
updateMigrationProgress()
}
@@ -1,6 +1,6 @@
package mihon.feature.migration.list.search
import eu.kanade.tachiyomi.source.CatalogueSource
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.model.SManga
import mihon.domain.manga.model.toDomainManga
import tachiyomi.domain.manga.model.Manga
@@ -9,19 +9,19 @@ class SmartSourceSearchEngine(extraSearchParams: String?) : BaseSmartSearchEngin
override fun getTitle(result: SManga) = result.title
suspend fun regularSearch(source: CatalogueSource, title: String): Manga? {
suspend fun regularSearch(source: Source, title: String): Manga? {
return regularSearch(makeSearchAction(source), title).let {
it?.toDomainManga(source.id)
}
}
suspend fun deepSearch(source: CatalogueSource, title: String): Manga? {
suspend fun deepSearch(source: Source, title: String): Manga? {
return deepSearch(makeSearchAction(source), title).let {
it?.toDomainManga(source.id)
}
}
private fun makeSearchAction(source: CatalogueSource): SearchAction<SManga> = { query ->
private fun makeSearchAction(source: Source): SearchAction<SManga> = { query ->
source.getSearchManga(1, query, source.getFilterList()).mangas
}
}