diff --git a/app/src/main/java/exh/ui/smartsearch/SmartSearchController.kt b/app/src/main/java/exh/ui/smartsearch/SmartSearchController.kt index bfcdb700a..2b9a22c31 100644 --- a/app/src/main/java/exh/ui/smartsearch/SmartSearchController.kt +++ b/app/src/main/java/exh/ui/smartsearch/SmartSearchController.kt @@ -14,8 +14,9 @@ import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourceController import eu.kanade.tachiyomi.ui.manga.MangaController import eu.kanade.tachiyomi.util.system.toast import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.cancel -import kotlinx.coroutines.launch +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.plus import kotlinx.coroutines.withContext import uy.kohesive.injekt.injectLazy @@ -34,7 +35,7 @@ class SmartSearchController(bundle: Bundle? = null) : NucleusController + if (results is SmartSearchPresenter.SearchResults.Found) { + val transaction = MangaController(results.manga, true, smartSearchConfig).withFadeTransaction() withContext(Dispatchers.Main) { router.replaceTopController(transaction) } } else { - if (event is SmartSearchPresenter.SearchResults.NotFound) { + if (results is SmartSearchPresenter.SearchResults.NotFound) { applicationContext?.toast("Couldn't find the manga in the source!") } else { applicationContext?.toast("Error performing automatic search!") @@ -70,13 +68,7 @@ class SmartSearchController(bundle: Bundle? = null) : NucleusController() { +class SmartSearchPresenter(private val source: CatalogueSource, private val config: SourceController.SmartSearchConfig) : + CoroutinePresenter() { - val scope = CoroutineScope(Job() + Dispatchers.Default) - - val smartSearchChannel = Channel() + val smartSearchFlow = MutableSharedFlow() private val smartSearchEngine = SmartSearchEngine() override fun onCreate(savedState: Bundle?) { super.onCreate(savedState) - if (source != null && config != null) { - scope.launch { - val result = try { - val resultManga = smartSearchEngine.smartSearch(source, config.origTitle) - if (resultManga != null) { - val localManga = smartSearchEngine.networkToLocalManga(resultManga, source.id) - SearchResults.Found(localManga) - } else { - SearchResults.NotFound - } - } catch (e: Exception) { - if (e is CancellationException) { - throw e - } else { - SearchResults.Error - } + scope.launch(Dispatchers.IO) { + val result = try { + val resultManga = smartSearchEngine.smartSearch(source, config.origTitle) + if (resultManga != null) { + val localManga = smartSearchEngine.networkToLocalManga(resultManga, source.id) + SearchResults.Found(localManga) + } else { + SearchResults.NotFound + } + } catch (e: Exception) { + if (e is CancellationException) { + throw e + } else { + SearchResults.Error } - - smartSearchChannel.send(result) } + + smartSearchFlow.emit(result) } } - override fun onDestroy() { - super.onDestroy() - - scope.cancel() - } - sealed class SearchResults { data class Found(val manga: Manga) : SearchResults() object NotFound : SearchResults()