Handle search action
This commit is contained in:
@@ -141,7 +141,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), AppBarOwner, BottomNav
|
|||||||
searchSuggestionViewModel.isIncognitoModeEnabled.observe(this, this::onIncognitoModeChanged)
|
searchSuggestionViewModel.isIncognitoModeEnabled.observe(this, this::onIncognitoModeChanged)
|
||||||
viewBinding.bottomNav?.addOnLayoutChangeListener(this)
|
viewBinding.bottomNav?.addOnLayoutChangeListener(this)
|
||||||
viewBinding.searchView.addTransitionListener(this)
|
viewBinding.searchView.addTransitionListener(this)
|
||||||
initSearchSuggestions()
|
initSearch()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
|
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)
|
viewBinding.recyclerViewSearch.setPadding(barsInsets.left, 0, barsInsets.right, barsInsets.bottom)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initSearchSuggestions() {
|
private fun initSearch() {
|
||||||
val listener = SearchSuggestionListenerImpl(router, viewBinding.searchView, searchSuggestionViewModel)
|
val listener = SearchSuggestionListenerImpl(router, viewBinding.searchView, searchSuggestionViewModel)
|
||||||
val adapter = SearchSuggestionAdapter(listener)
|
val adapter = SearchSuggestionAdapter(listener)
|
||||||
viewBinding.searchView.toolbar.addMenuProvider(
|
viewBinding.searchView.toolbar.addMenuProvider(
|
||||||
@@ -359,6 +359,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), AppBarOwner, BottomNav
|
|||||||
)
|
)
|
||||||
viewBinding.searchView.editText.addTextChangedListener(listener)
|
viewBinding.searchView.editText.addTextChangedListener(listener)
|
||||||
viewBinding.recyclerViewSearch.adapter = adapter
|
viewBinding.recyclerViewSearch.adapter = adapter
|
||||||
|
viewBinding.searchView.editText.setOnEditorActionListener(listener)
|
||||||
|
|
||||||
viewBinding.searchView.observeState()
|
viewBinding.searchView.observeState()
|
||||||
.map { it >= SearchView.TransitionState.SHOWING }
|
.map { it >= SearchView.TransitionState.SHOWING }
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
package org.koitharu.kotatsu.search.ui.suggestion
|
package org.koitharu.kotatsu.search.ui.suggestion
|
||||||
|
|
||||||
import android.text.TextWatcher
|
import android.text.TextWatcher
|
||||||
|
import android.widget.TextView
|
||||||
import org.koitharu.kotatsu.parsers.model.Manga
|
import org.koitharu.kotatsu.parsers.model.Manga
|
||||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||||
import org.koitharu.kotatsu.parsers.model.MangaTag
|
import org.koitharu.kotatsu.parsers.model.MangaTag
|
||||||
import org.koitharu.kotatsu.search.domain.SearchKind
|
import org.koitharu.kotatsu.search.domain.SearchKind
|
||||||
|
|
||||||
interface SearchSuggestionListener : TextWatcher {
|
interface SearchSuggestionListener : TextWatcher, TextView.OnEditorActionListener {
|
||||||
|
|
||||||
fun onMangaClick(manga: Manga)
|
fun onMangaClick(manga: Manga)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package org.koitharu.kotatsu.search.ui.suggestion
|
package org.koitharu.kotatsu.search.ui.suggestion
|
||||||
|
|
||||||
import android.text.Editable
|
import android.text.Editable
|
||||||
|
import android.view.KeyEvent
|
||||||
|
import android.widget.TextView
|
||||||
import androidx.core.net.toUri
|
import androidx.core.net.toUri
|
||||||
import com.google.android.material.search.SearchView
|
import com.google.android.material.search.SearchView
|
||||||
import org.koitharu.kotatsu.core.nav.AppRouter
|
import org.koitharu.kotatsu.core.nav.AppRouter
|
||||||
@@ -30,6 +32,7 @@ class SearchSuggestionListenerImpl(
|
|||||||
viewModel.saveQuery(query)
|
viewModel.saveQuery(query)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
searchView.hide()
|
||||||
} else {
|
} else {
|
||||||
searchView.setText(query)
|
searchView.setText(query)
|
||||||
}
|
}
|
||||||
@@ -54,4 +57,17 @@ class SearchSuggestionListenerImpl(
|
|||||||
override fun afterTextChanged(s: Editable?) {
|
override fun afterTextChanged(s: Editable?) {
|
||||||
viewModel.onQueryChanged(s?.toString().orEmpty())
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ import org.koitharu.kotatsu.search.domain.MangaSearchRepository
|
|||||||
import org.koitharu.kotatsu.search.ui.suggestion.model.SearchSuggestionItem
|
import org.koitharu.kotatsu.search.ui.suggestion.model.SearchSuggestionItem
|
||||||
import javax.inject.Inject
|
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_MANGA_ITEMS = 12
|
||||||
private const val MAX_QUERY_ITEMS = 16
|
private const val MAX_QUERY_ITEMS = 16
|
||||||
private const val MAX_HINTS_ITEMS = 3
|
private const val MAX_HINTS_ITEMS = 3
|
||||||
|
|||||||
Reference in New Issue
Block a user