Handle search action

This commit is contained in:
Koitharu
2025-05-24 21:26:40 +03:00
parent 19d0fe97a0
commit 12f1ffd019
4 changed files with 22 additions and 4 deletions

View File

@@ -141,7 +141,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), AppBarOwner, BottomNav
searchSuggestionViewModel.isIncognitoModeEnabled.observe(this, this::onIncognitoModeChanged)
viewBinding.bottomNav?.addOnLayoutChangeListener(this)
viewBinding.searchView.addTransitionListener(this)
initSearchSuggestions()
initSearch()
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
@@ -351,7 +351,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), AppBarOwner, BottomNav
viewBinding.recyclerViewSearch.setPadding(barsInsets.left, 0, barsInsets.right, barsInsets.bottom)
}
private fun initSearchSuggestions() {
private fun initSearch() {
val listener = SearchSuggestionListenerImpl(router, viewBinding.searchView, searchSuggestionViewModel)
val adapter = SearchSuggestionAdapter(listener)
viewBinding.searchView.toolbar.addMenuProvider(
@@ -359,6 +359,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), AppBarOwner, BottomNav
)
viewBinding.searchView.editText.addTextChangedListener(listener)
viewBinding.recyclerViewSearch.adapter = adapter
viewBinding.searchView.editText.setOnEditorActionListener(listener)
viewBinding.searchView.observeState()
.map { it >= SearchView.TransitionState.SHOWING }

View File

@@ -1,12 +1,13 @@
package org.koitharu.kotatsu.search.ui.suggestion
import android.text.TextWatcher
import android.widget.TextView
import org.koitharu.kotatsu.parsers.model.Manga
import org.koitharu.kotatsu.parsers.model.MangaSource
import org.koitharu.kotatsu.parsers.model.MangaTag
import org.koitharu.kotatsu.search.domain.SearchKind
interface SearchSuggestionListener : TextWatcher {
interface SearchSuggestionListener : TextWatcher, TextView.OnEditorActionListener {
fun onMangaClick(manga: Manga)

View File

@@ -1,6 +1,8 @@
package org.koitharu.kotatsu.search.ui.suggestion
import android.text.Editable
import android.view.KeyEvent
import android.widget.TextView
import androidx.core.net.toUri
import com.google.android.material.search.SearchView
import org.koitharu.kotatsu.core.nav.AppRouter
@@ -30,6 +32,7 @@ class SearchSuggestionListenerImpl(
viewModel.saveQuery(query)
}
}
searchView.hide()
} else {
searchView.setText(query)
}
@@ -54,4 +57,17 @@ class SearchSuggestionListenerImpl(
override fun afterTextChanged(s: Editable?) {
viewModel.onQueryChanged(s?.toString().orEmpty())
}
override fun onEditorAction(
v: TextView?,
actionId: Int,
event: KeyEvent?
): Boolean {
val query = v?.text?.toString()
if (query.isNullOrEmpty()) {
return false
}
onQueryClick(query, SearchKind.SIMPLE, true)
return true
}
}

View File

@@ -30,7 +30,7 @@ import org.koitharu.kotatsu.search.domain.MangaSearchRepository
import org.koitharu.kotatsu.search.ui.suggestion.model.SearchSuggestionItem
import javax.inject.Inject
private const val DEBOUNCE_TIMEOUT = 500L
private const val DEBOUNCE_TIMEOUT = 300L
private const val MAX_MANGA_ITEMS = 12
private const val MAX_QUERY_ITEMS = 16
private const val MAX_HINTS_ITEMS = 3