komikku/app/src/main/java/exh/ui/smartsearch/SmartSearchScreenModel.kt
Tim Schneeberger 9a31ffcc41
feat(recommendation): batch processing for recommendations & sort by relevancy (jobobby04/TachiyomiSY#1383)
* refactor: use NoResultsException

* refactor: cleanup RecommendationPagingSources

* refactor: turn wake/wifi lock functions into reusable extensions

* feat: implement batch recommendation (initial version)

* fix: serialization issues

* fix: wrong source id

* refactor: increase performance using virtual paging

* refactor: update string

* refactor: handle 404 of MD source correctly

* style: add newline

* refactor: create universal throttle manager

* refactor: throttle requests

* chore: remove unused strings

* feat: rank recommendations by match count

* feat: add badges indicating match count to batch recommendations

* fix: handle rec search with no results

* fix: validate flags in pre-search bottom sheet

* feat: implement 'hide library entries' for recommendation search using custom SmartSearchEngine for library items

* style: run spotless

* fix: cancel button

* fix: racing condition causing loss of state

* Avoid runBlocking in a property getter

* Remove unnecessary `synchronized`

* Fixes screen staying on in library tab. (jobobby04/TachiyomiSY#1451)

(cherry picked from commit 254980695bc39fe064e7aeb181ecdbf70c1eff95)

---------

Co-authored-by: Cuong-Tran <cuongtran.tm@gmail.com>
Co-authored-by: NGB-Was-Taken <76197326+NGB-Was-Taken@users.noreply.github.com>
2025-06-12 15:19:34 +07:00

53 lines
1.9 KiB
Kotlin

package exh.ui.smartsearch
import cafe.adriel.voyager.core.model.StateScreenModel
import cafe.adriel.voyager.core.model.screenModelScope
import eu.kanade.tachiyomi.source.CatalogueSource
import eu.kanade.tachiyomi.ui.browse.source.SourcesScreen
import exh.smartsearch.SmartSourceSearchEngine
import kotlinx.coroutines.CancellationException
import tachiyomi.core.common.util.lang.launchIO
import tachiyomi.domain.manga.interactor.NetworkToLocalManga
import tachiyomi.domain.manga.model.Manga
import tachiyomi.domain.source.service.SourceManager
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
class SmartSearchScreenModel(
sourceId: Long,
private val config: SourcesScreen.SmartSearchConfig,
private val networkToLocalManga: NetworkToLocalManga = Injekt.get(),
sourceManager: SourceManager = Injekt.get(),
) : StateScreenModel<SmartSearchScreenModel.SearchResults?>(null) {
private val smartSearchEngine = SmartSourceSearchEngine()
val source = sourceManager.get(sourceId) as CatalogueSource
init {
screenModelScope.launchIO {
val result = try {
val resultManga = smartSearchEngine.smartSearch(source, config.origTitle)
if (resultManga != null) {
val localManga = networkToLocalManga(resultManga)
SearchResults.Found(localManga)
} else {
SearchResults.NotFound
}
} catch (e: Exception) {
if (e is CancellationException) {
throw e
} else {
SearchResults.Error
}
}
mutableState.value = result
}
}
sealed class SearchResults {
data class Found(val manga: Manga) : SearchResults()
data object NotFound : SearchResults()
data object Error : SearchResults()
}
}