Cleanup SuggestionProvider

This commit is contained in:
tarsin committed 2023-08-01 08:54:46 +08:00
1 parent 5a5a5da2d9
commit ceb5424772
2 files changed
+15 -45

No files matched your search

@@ -255,33 +255,6 @@ class GalleryListScene : SearchBarScene() {
setEditTextHint(getString(if (EhUtils.isExHentai) R.string.gallery_list_search_bar_hint_exhentai else R.string.gallery_list_search_bar_hint_e_hentai))
}
private fun setSearchBarSuggestionProvider() {
setSuggestionProvider(object : SuggestionProvider {
override fun providerSuggestions(text: String): List<Suggestion>? {
val result1 = GalleryDetailUrlParser.parse(text, false)
if (result1 != null) {
return listOf<Suggestion>(
GalleryDetailUrlSuggestion(
result1.gid,
result1.token,
),
)
}
val result2 = GalleryPageUrlParser.parse(text, false)
if (result2 != null) {
return listOf<Suggestion>(
GalleryPageUrlSuggestion(
result2.gid,
result2.pToken,
result2.page,
),
)
}
return null
}
})
}
// Update search bar title, drawer checked item
private fun onUpdateUrlBuilder() {
_binding ?: return
@@ -440,7 +413,13 @@ class GalleryListScene : SearchBarScene() {
onApplySearch(query)
}
setSearchBarHint()
setSearchBarSuggestionProvider()
setSuggestionProvider { text ->
GalleryDetailUrlParser.parse(text, false)?.run {
GalleryDetailUrlSuggestion(gid, token)
} ?: GalleryPageUrlParser.parse(text, false)?.run {
GalleryPageUrlSuggestion(gid, pToken, page)
}
}
binding.fabLayout.setAutoCancel(true)
binding.fabLayout.isExpanded = false
binding.fabLayout.setHidePrimaryFab(false)
@@ -1,6 +1,5 @@
package com.hippo.ehviewer.ui.scene
import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.view.Gravity
@@ -44,9 +43,9 @@ import com.hippo.ehviewer.ui.setMD3Content
import com.jamal.composeprefs3.ui.ifNotNullThen
import com.jamal.composeprefs3.ui.ifTrueThen
import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.withUIContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.toList
abstract class SearchBarScene : BaseScene(), ToolBarScene {
private var _binding: SceneSearchbarBinding? = null
@@ -218,8 +217,8 @@ abstract class SearchBarScene : BaseScene(), ToolBarScene {
mAllowEmptySearch = allowEmptySearch
}
interface SuggestionProvider {
fun providerSuggestions(text: String): List<Suggestion>?
fun interface SuggestionProvider {
fun providerSuggestions(text: String): Suggestion?
}
private fun deleteKeyword(keyword: String) {
@@ -229,9 +228,7 @@ abstract class SearchBarScene : BaseScene(), ToolBarScene {
.setPositiveButton(R.string.delete) { _, _ ->
lifecycleScope.launchIO {
mSearchDatabase.deleteQuery(keyword)
withUIContext {
updateSuggestions(false)
}
updateSuggestions()
}
}
.show()
@@ -250,8 +247,7 @@ abstract class SearchBarScene : BaseScene(), ToolBarScene {
override val keyword: String,
) : Suggestion() {
override fun onClick() {
val edittext = binding.searchview.editText
edittext.let {
binding.searchview.editText.let {
var keywords = it.text.toString().substringBeforeLast(' ', "")
if (keywords.isNotEmpty()) keywords += ' '
keywords += wrapTagKeyword(keyword)
@@ -274,15 +270,10 @@ abstract class SearchBarScene : BaseScene(), ToolBarScene {
}
}
@SuppressLint("NotifyDataSetChanged")
private fun updateSuggestions(scrollToTop: Boolean = true) {
private fun updateSuggestions() {
_binding ?: return
viewLifecycleOwner.lifecycleScope.launchIO {
val suggestions = mutableListOf<Suggestion>()
mergedSuggestionFlow().collect {
suggestions.add(it)
}
mSuggestionList = suggestions
mSuggestionList = mergedSuggestionFlow().toList()
}
}
@@ -290,7 +281,7 @@ abstract class SearchBarScene : BaseScene(), ToolBarScene {
private fun mergedSuggestionFlow(): Flow<Suggestion> = flow {
binding.searchview.editText.text?.toString()?.let { text ->
mSuggestionProvider?.run { providerSuggestions(text)?.forEach { emit(it) } }
mSuggestionProvider?.run { providerSuggestions(text)?.let { emit(it) } }
mSearchDatabase.suggestions(text, 128).forEach { emit(KeywordSuggestion(it)) }
EhTagDatabase.takeIf { it.initialized }?.run {
if (text.isNotEmpty() && !text.endsWith(' ')) {