From cd7cf3583e7c3b5379dcc7507675de90dff44467 Mon Sep 17 00:00:00 2001 From: Gauthier Date: Sun, 23 Jan 2022 03:25:05 +0800 Subject: [PATCH] fix: handle Komga tracks during manga migration (#6463) * fix: handle Komga tracks during manga migration closes #6354 * refactor: remove Komga direct reference --- .../data/track/EnhancedTrackService.kt | 11 ++++++++++ .../tachiyomi/data/track/komga/Komga.kt | 11 ++++++++++ .../migration/search/SearchPresenter.kt | 20 +++++++++++++++---- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/EnhancedTrackService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/EnhancedTrackService.kt index 4fefaf2b54..21ee0e7e42 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/EnhancedTrackService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/EnhancedTrackService.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.data.track import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.track.model.TrackSearch import eu.kanade.tachiyomi.source.Source @@ -25,4 +26,14 @@ interface EnhancedTrackService { * match is similar to TrackService.search, but only return zero or one match. */ suspend fun match(manga: Manga): TrackSearch? + + /** + * Checks whether the provided source/track/manga triplet is from this TrackService + */ + fun isTrackFrom(track: Track, manga: Manga, source: Source): Boolean + + /** + * Migrates the given track for the manga to the newSource, if possible + */ + fun migrateTrack(track: Track, manga: Manga, newSource: Source): Track? } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/komga/Komga.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/komga/Komga.kt index 19029ef9ba..03afcb2134 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/komga/Komga.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/komga/Komga.kt @@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.data.track.EnhancedTrackService import eu.kanade.tachiyomi.data.track.NoLoginTrackService import eu.kanade.tachiyomi.data.track.TrackService import eu.kanade.tachiyomi.data.track.model.TrackSearch +import eu.kanade.tachiyomi.source.Source import okhttp3.Dns import okhttp3.OkHttpClient @@ -103,4 +104,14 @@ class Komga(private val context: Context, id: Int) : TrackService(id), EnhancedT } catch (e: Exception) { null } + + override fun isTrackFrom(track: Track, manga: Manga, source: Source): Boolean = + accept(source) && track.tracking_url == manga.url + + override fun migrateTrack(track: Track, manga: Manga, newSource: Source): Track? = + if (accept(newSource)) { + track.also { track.tracking_url = manga.url } + } else { + null + } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt index cd822948f1..82446c59ce 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt @@ -5,6 +5,8 @@ import com.jakewharton.rxrelay.BehaviorRelay import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.MangaCategory import eu.kanade.tachiyomi.data.database.models.toMangaInfo +import eu.kanade.tachiyomi.data.track.EnhancedTrackService +import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.model.SChapter @@ -19,6 +21,8 @@ import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.launchUI import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.system.toast +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get import java.util.Date class SearchPresenter( @@ -28,6 +32,8 @@ class SearchPresenter( private val replacingMangaRelay = BehaviorRelay.create>() + private val enhancedServices by lazy { Injekt.get().services.filterIsInstance() } + override fun onCreate(savedState: Bundle?) { super.onCreate(savedState) @@ -57,6 +63,7 @@ class SearchPresenter( } fun migrateManga(prevManga: Manga, manga: Manga, replace: Boolean) { + val prevSource = sourceManager.get(prevManga.source) ?: return val source = sourceManager.get(manga.source) ?: return replacingMangaRelay.call(Pair(true, null)) @@ -66,7 +73,7 @@ class SearchPresenter( val chapters = source.getChapterList(manga.toMangaInfo()) .map { it.toSChapter() } - migrateMangaInternal(source, chapters, prevManga, manga, replace) + migrateMangaInternal(prevSource, source, chapters, prevManga, manga, replace) } catch (e: Throwable) { withUIContext { view?.applicationContext?.toast(e.message) } } @@ -76,6 +83,7 @@ class SearchPresenter( } private fun migrateMangaInternal( + prevSource: Source, source: Source, sourceChapters: List, prevManga: Manga, @@ -135,12 +143,16 @@ class SearchPresenter( // Update track if (migrateTracks) { - val tracks = db.getTracks(prevManga).executeAsBlocking() - for (track in tracks) { + val tracksToUpdate = db.getTracks(prevManga).executeAsBlocking().mapNotNull { track -> track.id = null track.manga_id = manga.id!! + + val service = enhancedServices + .firstOrNull { it.isTrackFrom(track, prevManga, prevSource) } + if (service != null) service.migrateTrack(track, manga, source) + else track } - db.insertTracks(tracks).executeAsBlocking() + db.insertTracks(tracksToUpdate).executeAsBlocking() } // Update favorite status