can serialize Search Filters

This commit is contained in:
Aria Moradi
2021-08-29 22:19:44 +04:30
parent a8ef6cdd4f
commit c2cea7e797
2 changed files with 44 additions and 16 deletions

View File

@@ -12,7 +12,6 @@ import suwayomi.tachidesk.manga.impl.MangaList
import suwayomi.tachidesk.manga.impl.Search
import suwayomi.tachidesk.manga.impl.Source
import suwayomi.tachidesk.manga.impl.Source.SourcePreferenceChange
import suwayomi.tachidesk.server.JavalinSetup
import suwayomi.tachidesk.server.JavalinSetup.future
object SourceController {
@@ -63,9 +62,11 @@ object SourceController {
}
/** fetch filters of source with id `sourceId` */
fun filters(ctx: Context) { // TODO
fun filters(ctx: Context) {
val sourceId = ctx.pathParam("sourceId").toLong()
ctx.json(Search.sourceFilters(sourceId))
val reset = ctx.queryParam("reset", "false").toBoolean()
ctx.json(Search.getInitialFilterList(sourceId, reset))
}
/** single source search */
@@ -73,7 +74,7 @@ object SourceController {
val sourceId = ctx.pathParam("sourceId").toLong()
val searchTerm = ctx.pathParam("searchTerm")
val pageNum = ctx.pathParam("pageNum").toInt()
ctx.json(JavalinSetup.future { Search.sourceSearch(sourceId, searchTerm, pageNum) })
ctx.json(future { Search.sourceSearch(sourceId, searchTerm, pageNum) })
}
/** all source search */

View File

@@ -7,6 +7,8 @@ package suwayomi.tachidesk.manga.impl
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import eu.kanade.tachiyomi.source.model.Filter
import eu.kanade.tachiyomi.source.model.FilterList
import suwayomi.tachidesk.manga.impl.MangaList.processEntries
import suwayomi.tachidesk.manga.impl.util.GetHttpSource.getHttpSource
import suwayomi.tachidesk.manga.impl.util.lang.awaitSingle
@@ -15,28 +17,53 @@ import suwayomi.tachidesk.manga.model.dataclass.PagedMangaListDataClass
object Search {
suspend fun sourceSearch(sourceId: Long, searchTerm: String, pageNum: Int): PagedMangaListDataClass {
val source = getHttpSource(sourceId)
val searchManga = source.fetchSearchManga(pageNum, searchTerm, source.getFilterList()).awaitSingle()
val searchManga = source.fetchSearchManga(pageNum, searchTerm, getFilterListOf(sourceId)).awaitSingle()
return searchManga.processEntries(sourceId)
}
// TODO
@Suppress("UNUSED_PARAMETER", "UNUSED_VARIABLE")
fun sourceFilters(sourceId: Long) {
val source = getHttpSource(sourceId)
// source.getFilterList().toItems()
private val filterListCache = mutableMapOf<Long, FilterList>()
private fun getFilterListOf(sourceId: Long, reset: Boolean = false): FilterList {
if (reset || !filterListCache.containsKey(sourceId)) {
filterListCache[sourceId] = getHttpSource(sourceId).getFilterList()
}
return filterListCache[sourceId]!!
}
fun getInitialFilterList(sourceId: Long, reset: Boolean): List<FilterObject> {
return getFilterListOf(sourceId, reset).list.map {
FilterObject(
when (it) {
is Filter.Header -> "Header"
is Filter.Separator -> "Separator"
is Filter.CheckBox -> "CheckBox"
is Filter.TriState -> "TriState"
is Filter.Text -> "Text"
is Filter.Select<*> -> "Select"
is Filter.Group<*> -> "Group"
is Filter.Sort -> "Sort"
},
// when (it) {
// is Filter.Select<*> -> it.getValuesType()
// else -> null
// },
it
)
}
}
// private fun Filter.Select<*>.getValuesType(): String = values::class.java.componentType!!.simpleName
data class FilterObject(
val type: String,
val filter: Filter<*>
)
@Suppress("UNUSED_PARAMETER")
fun sourceGlobalSearch(searchTerm: String) {
// TODO
}
@Suppress("unused")
data class FilterWrapper(
val type: String,
val filter: Any
)
/**
* Note: Exhentai had a filter serializer (now in SY) that we might be able to steal
*/