Improved search speed

This commit is contained in:
tom5079
2022-01-09 00:34:29 +09:00
parent 0fd2cf4fd7
commit 0902fdf981
9 changed files with 76 additions and 43 deletions

View File

@@ -77,8 +77,6 @@ suspend fun WebView.evaluatePromise(
val uid = UUID.randomUUID().toString()
evaluateJavascript((script + then).replace("%uid", "'$uid'"), null)
val flow: Flow<Pair<String, String?>> = webViewFlow.transformWhile { (currentUid, result) ->
if (currentUid == uid) {
emit(currentUid to result)
@@ -86,6 +84,10 @@ suspend fun WebView.evaluatePromise(
currentUid != uid
}
launch {
evaluateJavascript((script + then).replace("%uid", "'$uid'"), null)
}
flow.first().second
}
} catch (e: CancellationException) {
@@ -100,7 +102,7 @@ suspend fun WebView.evaluatePromise(
suspend fun getGalleryInfo(galleryID: Int): GalleryInfo {
val result = webView.evaluatePromise("get_gallery_info($galleryID)")
return json.decodeFromString(result!!)
return json.decodeFromString(result)
}
//common.js

View File

@@ -52,7 +52,7 @@ suspend fun getGalleryBlock(galleryID: Int) : GalleryBlock {
});
""".trimIndent(),
then = ""
)!!
)
val doc = Jsoup.parse(html)

View File

@@ -16,6 +16,7 @@
package xyz.quaver.pupil.hitomi
import android.util.Log
import kotlinx.coroutines.*
import java.util.*
@@ -47,7 +48,7 @@ suspend fun doSearch(query: String, sortByPopularity: Boolean = false) : Set<Int
}
}
val negativeResults = negativeTerms.map {
val negativeResults = negativeTerms.mapIndexed { index, it ->
async {
runCatching {
getGalleryIDsForQuery(it)
@@ -55,21 +56,21 @@ suspend fun doSearch(query: String, sortByPopularity: Boolean = false) : Set<Int
}
}
var results = when {
val results = when {
sortByPopularity -> getGalleryIDsFromNozomi(null, "popular", "all")
positiveTerms.isEmpty() -> getGalleryIDsFromNozomi(null, "index", "all")
else -> emptySet()
}
}.toMutableSet()
fun filterPositive(newResults: Set<Int>) {
results = when {
results.isEmpty() -> newResults
else -> results intersect newResults
when {
results.isEmpty() -> results.addAll(newResults)
else -> results.retainAll(newResults)
}
}
fun filterNegative(newResults: Set<Int>) {
results = results subtract newResults
results.removeAll(newResults)
}
//positive results
@@ -78,7 +79,7 @@ suspend fun doSearch(query: String, sortByPopularity: Boolean = false) : Set<Int
}
//negative results
negativeResults.forEach {
negativeResults.forEachIndexed { index, it ->
filterNegative(it.await())
}

View File

@@ -27,7 +27,7 @@ const val extension = ".html"
@OptIn(ExperimentalSerializationApi::class)
suspend fun getGalleryIDsForQuery(query: String) : Set<Int> {
val result = webView.evaluatePromise("get_galleryids_for_query('$query')") ?: return emptySet()
val result = webView.evaluatePromise("get_galleryids_for_query('$query')")
return Json.decodeFromString(result)
}
@@ -37,7 +37,7 @@ data class Suggestion(val s: String, val t: Int, val u: String, val n: String)
@OptIn(ExperimentalSerializationApi::class)
suspend fun getSuggestionsForQuery(query: String) : List<Suggestion> {
val result = webView.evaluatePromise("get_suggestions_for_query('$query')") ?: return emptyList()
val result = webView.evaluatePromise("get_suggestions_for_query('$query')")
return Json.decodeFromString<List<List<Suggestion>?>>(result)[0] ?: return emptyList()
}