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.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<EhSmartS
override fun getTitle() = source?.name.orEmpty()
override fun createPresenter() = SmartSearchPresenter(source, smartSearchConfig)
override fun createPresenter() = SmartSearchPresenter(source!!, smartSearchConfig!!)
override fun onViewCreated(view: View) {
super.onViewCreated(view)
@ -47,18 +48,15 @@ class SmartSearchController(bundle: Bundle? = null) : NucleusController<EhSmartS
return
}
// Init presenter now to resolve threading issues
presenter
scope.launch(Dispatchers.Default) {
for (event in presenter.smartSearchChannel) {
if (event is SmartSearchPresenter.SearchResults.Found) {
val transaction = MangaController(event.manga, true, smartSearchConfig).withFadeTransaction()
presenter.smartSearchFlow
.onEach { results ->
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<EhSmartS
}
}
}
}
}
override fun onDestroy() {
super.onDestroy()
scope.cancel()
.launchIn(scope + Dispatchers.IO)
}
companion object {

View File

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