2020-04-21 23:37:03 +02:00
|
|
|
package exh.ui.smartsearch
|
2019-07-29 08:12:30 +02:00
|
|
|
|
2022-11-26 19:36:06 +01:00
|
|
|
import cafe.adriel.voyager.core.model.StateScreenModel
|
|
|
|
|
import cafe.adriel.voyager.core.model.coroutineScope
|
2019-07-29 19:27:33 +02:00
|
|
|
import eu.kanade.tachiyomi.source.CatalogueSource
|
2022-12-03 06:19:24 +01:00
|
|
|
import eu.kanade.tachiyomi.ui.browse.source.SourcesScreen
|
2020-04-21 23:37:03 +02:00
|
|
|
import exh.smartsearch.SmartSearchEngine
|
2020-04-04 22:30:05 +02:00
|
|
|
import kotlinx.coroutines.CancellationException
|
2023-01-28 04:31:12 +01:00
|
|
|
import tachiyomi.core.util.lang.launchIO
|
2023-02-18 21:14:04 +01:00
|
|
|
import tachiyomi.domain.manga.interactor.NetworkToLocalManga
|
2023-01-22 16:54:28 +01:00
|
|
|
import tachiyomi.domain.manga.model.Manga
|
2023-03-05 18:38:31 +01:00
|
|
|
import tachiyomi.domain.source.service.SourceManager
|
2022-10-27 05:01:21 +02:00
|
|
|
import uy.kohesive.injekt.Injekt
|
|
|
|
|
import uy.kohesive.injekt.api.get
|
|
|
|
|
|
2022-11-26 19:36:06 +01:00
|
|
|
class SmartSearchScreenModel(
|
|
|
|
|
private val sourceId: Long,
|
2022-12-03 06:19:24 +01:00
|
|
|
private val config: SourcesScreen.SmartSearchConfig,
|
2022-10-27 05:01:21 +02:00
|
|
|
private val networkToLocalManga: NetworkToLocalManga = Injekt.get(),
|
2022-11-26 19:36:06 +01:00
|
|
|
private val sourceManager: SourceManager = Injekt.get(),
|
|
|
|
|
) : StateScreenModel<SmartSearchScreenModel.SearchResults?>(null) {
|
2020-08-24 23:25:10 +02:00
|
|
|
private val smartSearchEngine = SmartSearchEngine()
|
2019-07-31 09:39:51 +02:00
|
|
|
|
2022-11-26 19:36:06 +01:00
|
|
|
val source = sourceManager.get(sourceId) as CatalogueSource
|
2019-07-29 19:27:33 +02:00
|
|
|
|
2022-11-26 19:36:06 +01:00
|
|
|
init {
|
|
|
|
|
coroutineScope.launchIO {
|
2020-12-26 06:06:52 +01:00
|
|
|
val result = try {
|
|
|
|
|
val resultManga = smartSearchEngine.smartSearch(source, config.origTitle)
|
|
|
|
|
if (resultManga != null) {
|
2022-10-30 22:40:17 +01:00
|
|
|
val localManga = networkToLocalManga.await(resultManga)
|
2020-12-26 06:06:52 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-26 19:36:06 +01:00
|
|
|
mutableState.value = 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
|
|
|
}
|