2020-04-21 23:37:03 +02:00
|
|
|
package exh.ui.smartsearch
|
2019-07-29 08:12:30 +02:00
|
|
|
|
2019-07-29 19:27:33 +02:00
|
|
|
import android.os.Bundle
|
|
|
|
|
import eu.kanade.tachiyomi.data.database.models.Manga
|
|
|
|
|
import eu.kanade.tachiyomi.source.CatalogueSource
|
2022-05-15 23:03:57 +02:00
|
|
|
import eu.kanade.tachiyomi.ui.browse.source.SourcesController
|
2021-12-12 23:59:29 +01:00
|
|
|
import eu.kanade.tachiyomi.util.lang.launchIO
|
2020-04-21 23:37:03 +02:00
|
|
|
import exh.smartsearch.SmartSearchEngine
|
2020-12-26 06:06:52 +01:00
|
|
|
import exh.ui.base.CoroutinePresenter
|
2020-04-04 22:30:05 +02:00
|
|
|
import kotlinx.coroutines.CancellationException
|
2020-12-26 06:06:52 +01:00
|
|
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
2021-12-12 23:59:29 +01:00
|
|
|
import kotlinx.coroutines.flow.asSharedFlow
|
2019-07-29 08:12:30 +02:00
|
|
|
|
2022-05-15 23:03:57 +02:00
|
|
|
class SmartSearchPresenter(private val source: CatalogueSource, private val config: SourcesController.SmartSearchConfig) :
|
2020-12-26 06:06:52 +01:00
|
|
|
CoroutinePresenter<SmartSearchController>() {
|
2019-07-29 19:27:33 +02:00
|
|
|
|
2021-12-12 23:59:29 +01:00
|
|
|
private val _smartSearchFlow = MutableSharedFlow<SearchResults>()
|
|
|
|
|
val smartSearchFlow = _smartSearchFlow.asSharedFlow()
|
2019-07-29 19:27:33 +02:00
|
|
|
|
2020-08-24 23:25:10 +02:00
|
|
|
private val smartSearchEngine = SmartSearchEngine()
|
2019-07-31 09:39:51 +02:00
|
|
|
|
2019-07-29 19:27:33 +02:00
|
|
|
override fun onCreate(savedState: Bundle?) {
|
|
|
|
|
super.onCreate(savedState)
|
|
|
|
|
|
2022-06-25 17:03:48 +02:00
|
|
|
presenterScope.launchIO {
|
2020-12-26 06:06:52 +01:00
|
|
|
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
|
2019-07-29 19:27:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-12 23:59:29 +01:00
|
|
|
_smartSearchFlow.emit(result)
|
2020-12-26 06:06:52 +01:00
|
|
|
}
|
2019-07-29 19:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sealed class SearchResults {
|
2020-04-04 22:30:05 +02:00
|
|
|
data class Found(val manga: Manga) : SearchResults()
|
|
|
|
|
object NotFound : SearchResults()
|
|
|
|
|
object Error : SearchResults()
|
2019-07-29 19:27:33 +02:00
|
|
|
}
|
2020-04-04 22:30:05 +02:00
|
|
|
}
|