Convert SmartSearch channel to SharedFlow

This commit is contained in:
Jobobby04 2020-12-26 00:06:52 -05:00
parent a7b05f372b
commit 78f48d28c3
2 changed files with 31 additions and 52 deletions

View File

@ -14,8 +14,9 @@ import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourceController
import eu.kanade.tachiyomi.ui.manga.MangaController import eu.kanade.tachiyomi.ui.manga.MangaController
import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.system.toast
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.launch import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.plus
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
@ -34,7 +35,7 @@ class SmartSearchController(bundle: Bundle? = null) : NucleusController<EhSmartS
override fun getTitle() = source?.name.orEmpty() override fun getTitle() = source?.name.orEmpty()
override fun createPresenter() = SmartSearchPresenter(source, smartSearchConfig) override fun createPresenter() = SmartSearchPresenter(source!!, smartSearchConfig!!)
override fun onViewCreated(view: View) { override fun onViewCreated(view: View) {
super.onViewCreated(view) super.onViewCreated(view)
@ -47,18 +48,15 @@ class SmartSearchController(bundle: Bundle? = null) : NucleusController<EhSmartS
return return
} }
// Init presenter now to resolve threading issues presenter.smartSearchFlow
presenter .onEach { results ->
if (results is SmartSearchPresenter.SearchResults.Found) {
scope.launch(Dispatchers.Default) { val transaction = MangaController(results.manga, true, smartSearchConfig).withFadeTransaction()
for (event in presenter.smartSearchChannel) {
if (event is SmartSearchPresenter.SearchResults.Found) {
val transaction = MangaController(event.manga, true, smartSearchConfig).withFadeTransaction()
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
router.replaceTopController(transaction) router.replaceTopController(transaction)
} }
} else { } else {
if (event is SmartSearchPresenter.SearchResults.NotFound) { if (results is SmartSearchPresenter.SearchResults.NotFound) {
applicationContext?.toast("Couldn't find the manga in the source!") applicationContext?.toast("Couldn't find the manga in the source!")
} else { } else {
applicationContext?.toast("Error performing automatic search!") applicationContext?.toast("Error performing automatic search!")
@ -70,13 +68,7 @@ class SmartSearchController(bundle: Bundle? = null) : NucleusController<EhSmartS
} }
} }
} }
} .launchIn(scope + Dispatchers.IO)
}
override fun onDestroy() {
super.onDestroy()
scope.cancel()
} }
companion object { companion object {

View File

@ -3,58 +3,45 @@ package exh.ui.smartsearch
import android.os.Bundle import android.os.Bundle
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.CatalogueSource
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
import eu.kanade.tachiyomi.ui.browse.source.SourceController import eu.kanade.tachiyomi.ui.browse.source.SourceController
import exh.smartsearch.SmartSearchEngine import exh.smartsearch.SmartSearchEngine
import exh.ui.base.CoroutinePresenter
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.cancel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class SmartSearchPresenter(private val source: CatalogueSource?, private val config: SourceController.SmartSearchConfig?) : class SmartSearchPresenter(private val source: CatalogueSource, private val config: SourceController.SmartSearchConfig) :
BasePresenter<SmartSearchController>() { CoroutinePresenter<SmartSearchController>() {
val scope = CoroutineScope(Job() + Dispatchers.Default) val smartSearchFlow = MutableSharedFlow<SearchResults>()
val smartSearchChannel = Channel<SearchResults>()
private val smartSearchEngine = SmartSearchEngine() private val smartSearchEngine = SmartSearchEngine()
override fun onCreate(savedState: Bundle?) { override fun onCreate(savedState: Bundle?) {
super.onCreate(savedState) super.onCreate(savedState)
if (source != null && config != null) { scope.launch(Dispatchers.IO) {
scope.launch { val result = try {
val result = try { val resultManga = smartSearchEngine.smartSearch(source, config.origTitle)
val resultManga = smartSearchEngine.smartSearch(source, config.origTitle) if (resultManga != null) {
if (resultManga != null) { val localManga = smartSearchEngine.networkToLocalManga(resultManga, source.id)
val localManga = smartSearchEngine.networkToLocalManga(resultManga, source.id) SearchResults.Found(localManga)
SearchResults.Found(localManga) } else {
} else { SearchResults.NotFound
SearchResults.NotFound }
} } catch (e: Exception) {
} catch (e: Exception) { if (e is CancellationException) {
if (e is CancellationException) { throw e
throw e } else {
} else { SearchResults.Error
SearchResults.Error
}
} }
smartSearchChannel.send(result)
} }
smartSearchFlow.emit(result)
} }
} }
override fun onDestroy() {
super.onDestroy()
scope.cancel()
}
sealed class SearchResults { sealed class SearchResults {
data class Found(val manga: Manga) : SearchResults() data class Found(val manga: Manga) : SearchResults()
object NotFound : SearchResults() object NotFound : SearchResults()