Convert SmartSearch channel to SharedFlow
This commit is contained in:
parent
a7b05f372b
commit
78f48d28c3
@ -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 {
|
||||
|
@ -3,58 +3,45 @@ 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 {
|
||||
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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user