diff --git a/app/src/main/java/eu/kanade/tachiyomi/source/online/all/MergedSource.kt b/app/src/main/java/eu/kanade/tachiyomi/source/online/all/MergedSource.kt index ea677d698..21fe778db 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/source/online/all/MergedSource.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/source/online/all/MergedSource.kt @@ -136,7 +136,7 @@ class MergedSource : SuspendHttpSource() { val ifDownloadNewChapters = downloadChapters && manga.shouldDownloadNewChapters(db, preferences) return mangaReferences.filter { it.mangaSourceId != MERGED_SOURCE_ID }.map { - load(db, sourceManager, it) + it.load(db, sourceManager) }.mapNotNull { loadedManga -> withContext(Dispatchers.IO) { if (loadedManga.manga != null && loadedManga.reference.getChapterUpdates) { @@ -165,23 +165,23 @@ class MergedSource : SuspendHttpSource() { } } - suspend fun load(db: DatabaseHelper, sourceManager: SourceManager, reference: MergedMangaReference): LoadedMangaSource { - var manga = db.getManga(reference.mangaUrl, reference.mangaSourceId).await() - val source = sourceManager.getOrStub(manga?.source ?: reference.mangaSourceId) + suspend fun MergedMangaReference.load(db: DatabaseHelper, sourceManager: SourceManager): LoadedMangaSource { + var manga = db.getManga(mangaUrl, mangaSourceId).await() + val source = sourceManager.getOrStub(manga?.source ?: mangaSourceId) if (manga == null) { - manga = Manga.create(reference.mangaSourceId).apply { - url = reference.mangaUrl + manga = Manga.create(mangaSourceId).apply { + url = mangaUrl } manga.copyFrom(source.getMangaDetails(manga.toMangaInfo()).toSManga()) try { manga.id = db.insertManga(manga).await().insertedId() - reference.mangaId = manga.id - db.insertNewMergedMangaId(reference).await() + mangaId = manga.id + db.insertNewMergedMangaId(this).await() } catch (e: Exception) { XLog.tag("MergedSource").enableStackTrace(e.stackTrace.contentToString(), 5) } } - return LoadedMangaSource(source, manga, reference) + return LoadedMangaSource(source, manga, this) } data class LoadedMangaSource(val source: Source, val manga: Manga?, val reference: MergedMangaReference) diff --git a/app/src/main/java/exh/ui/base/CoroutinePresenter.kt b/app/src/main/java/exh/ui/base/CoroutinePresenter.kt index 2ad6238b2..d6cb5f026 100644 --- a/app/src/main/java/exh/ui/base/CoroutinePresenter.kt +++ b/app/src/main/java/exh/ui/base/CoroutinePresenter.kt @@ -1,41 +1,60 @@ package exh.ui.base import androidx.annotation.CallSuper +import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.cancel import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import nucleus.presenter.Presenter +import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.EmptyCoroutineContext -@Suppress("DEPRECATION") -open class CoroutinePresenter : Presenter() { - val scope = CoroutineScope(Job() + Dispatchers.Main) - +@Suppress("DEPRECATION", "unused") +open class CoroutinePresenter( + scope: CoroutineScope = CoroutineScope(Job() + Dispatchers.Main) +) : Presenter(), + CoroutineScope by scope +{ @Suppress("DeprecatedCallableAddReplaceWith") - @Deprecated("Use launchInView") + @Deprecated("Use launchInView, Flow.inView, Flow.mapView") override fun getView(): V? { return super.getView() } - fun launchInView(block: (CoroutineScope, V) -> Unit) = scope.launch(Dispatchers.Main) { + fun launchInView(block: (CoroutineScope, V) -> Unit) = launch(Dispatchers.Main) { view?.let { block.invoke(this, it) } } - fun Flow.onEachView(block: (V, F) -> Unit) = onEach { - view?.let { view -> block(view, it) } + inline fun Flow.inView(crossinline block: (V, F) -> Unit) = onEach { + withContext(Dispatchers.Main) { + view?.let { view -> block(view, it) } + } } - fun Flow.mapView(block: (V, F) -> P): Flow

= mapNotNull { - view?.let { view -> block(view, it) } + inline fun Flow.mapView(crossinline block: (V, F) -> P): Flow

{ + return mapNotNull { + withContext(Dispatchers.Main) { + view?.let { view -> block(view, it) } + } + } } + fun Flow<*>.launchUnderContext(context: CoroutineContext = EmptyCoroutineContext) = + launch(this + context) { this@launchInHere.collect() } + + fun Flow<*>.launch() = launchIn(this) + @CallSuper override fun destroy() { super.destroy() - scope.cancel() + cancel() } } diff --git a/app/src/main/java/exh/ui/metadata/MetadataViewPresenter.kt b/app/src/main/java/exh/ui/metadata/MetadataViewPresenter.kt index a040e9fd3..483b19612 100644 --- a/app/src/main/java/exh/ui/metadata/MetadataViewPresenter.kt +++ b/app/src/main/java/exh/ui/metadata/MetadataViewPresenter.kt @@ -41,13 +41,13 @@ class MetadataViewPresenter( meta.value = it.raise(mainSource.metaClass) } } - .launchIn(scope + Dispatchers.IO) + .launchUnderContext(Dispatchers.IO) meta - .onEachView { view, metadata -> + .inView { view, metadata -> view.onNextMangaInfo(metadata) } - .launchIn(scope) + .launch() } private fun getMangaMetaObservable(): Flow { diff --git a/app/src/main/java/exh/ui/smartsearch/SmartSearchPresenter.kt b/app/src/main/java/exh/ui/smartsearch/SmartSearchPresenter.kt index 9b62f39d2..b91af4c1a 100644 --- a/app/src/main/java/exh/ui/smartsearch/SmartSearchPresenter.kt +++ b/app/src/main/java/exh/ui/smartsearch/SmartSearchPresenter.kt @@ -21,7 +21,7 @@ class SmartSearchPresenter(private val source: CatalogueSource, private val conf override fun onCreate(savedState: Bundle?) { super.onCreate(savedState) - scope.launch(Dispatchers.IO) { + launch(Dispatchers.IO) { val result = try { val resultManga = smartSearchEngine.smartSearch(source, config.origTitle) if (resultManga != null) {