search suggestions rewrite

This commit is contained in:
Bnyro 2022-06-10 10:58:33 +02:00
parent 45f6a3319c
commit b1d9694432
5 changed files with 110 additions and 50 deletions

View File

@ -4,7 +4,7 @@ import android.content.Context
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.AutoCompleteTextView import android.widget.EditText
import android.widget.TextView import android.widget.TextView
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@ -14,7 +14,7 @@ import com.google.android.material.imageview.ShapeableImageView
class SearchHistoryAdapter( class SearchHistoryAdapter(
private val context: Context, private val context: Context,
private var historyList: List<String>, private var historyList: List<String>,
private val editText: AutoCompleteTextView private val editText: EditText
) : ) :
RecyclerView.Adapter<SearchHistoryViewHolder>() { RecyclerView.Adapter<SearchHistoryViewHolder>() {

View File

@ -0,0 +1,40 @@
package com.github.libretube.adapters
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
class SearchSuggestionsAdapter(
private var suggestionsList: List<String>,
private var autoCompleteTextView: EditText
) :
RecyclerView.Adapter<SearchSuggestionsViewHolder>() {
override fun getItemCount(): Int {
return suggestionsList.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchSuggestionsViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val cell = layoutInflater.inflate(R.layout.searchsuggestion_row, parent, false)
return SearchSuggestionsViewHolder(cell)
}
override fun onBindViewHolder(holder: SearchSuggestionsViewHolder, position: Int) {
val suggestion = suggestionsList[position]
val suggestionTextView = holder.v.findViewById<TextView>(R.id.suggestion_text)
suggestionTextView.text = suggestion
holder.v.setOnClickListener {
autoCompleteTextView.setText(suggestion)
}
}
}
class SearchSuggestionsViewHolder(val v: View) : RecyclerView.ViewHolder(v) {
init {
}
}

View File

@ -1,7 +1,6 @@
package com.github.libretube.fragments package com.github.libretube.fragments
import android.content.Context import android.content.Context
import android.content.DialogInterface
import android.os.Bundle import android.os.Bundle
import android.text.Editable import android.text.Editable
import android.text.TextWatcher import android.text.TextWatcher
@ -12,8 +11,7 @@ import android.view.ViewGroup
import android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN import android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
import android.view.inputmethod.EditorInfo import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.ArrayAdapter import android.widget.EditText
import android.widget.AutoCompleteTextView
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView.GONE import android.widget.TextView.GONE
import android.widget.TextView.OnEditorActionListener import android.widget.TextView.OnEditorActionListener
@ -27,14 +25,14 @@ import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.adapters.SearchAdapter import com.github.libretube.adapters.SearchAdapter
import com.github.libretube.adapters.SearchHistoryAdapter import com.github.libretube.adapters.SearchHistoryAdapter
import com.github.libretube.adapters.SearchSuggestionsAdapter
import com.github.libretube.hideKeyboard import com.github.libretube.hideKeyboard
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.io.IOException
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException
class SearchFragment : Fragment() { class SearchFragment : Fragment() {
private val TAG = "SearchFragment" private val TAG = "SearchFragment"
@ -42,6 +40,7 @@ class SearchFragment : Fragment() {
private var apiSearchFilter = "all" private var apiSearchFilter = "all"
private var nextPage: String? = null private var nextPage: String? = null
private lateinit var searchRecView: RecyclerView private lateinit var searchRecView: RecyclerView
private lateinit var historyRecView: RecyclerView
private var searchAdapter: SearchAdapter? = null private var searchAdapter: SearchAdapter? = null
private var isLoading: Boolean = true private var isLoading: Boolean = true
@ -62,11 +61,11 @@ class SearchFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
searchRecView = view.findViewById<RecyclerView>(R.id.search_recycler) searchRecView = view.findViewById(R.id.search_recycler)
historyRecView = view.findViewById(R.id.history_recycler)
val autoTextView = view.findViewById<AutoCompleteTextView>(R.id.autoCompleteTextView) val autoTextView = view.findViewById<EditText>(R.id.autoCompleteTextView)
val clearSearchButton = view.findViewById<ImageView>(R.id.clearSearch_imageView) val clearSearchButton = view.findViewById<ImageView>(R.id.clearSearch_imageView)
val historyRecycler = view.findViewById<RecyclerView>(R.id.history_recycler)
val filterImageView = view.findViewById<ImageView>(R.id.filterMenu_imageView) val filterImageView = view.findViewById<ImageView>(R.id.filterMenu_imageView)
var tempSelectedItem = 0 var tempSelectedItem = 0
@ -92,15 +91,12 @@ class SearchFragment : Fragment() {
MaterialAlertDialogBuilder(view.context) MaterialAlertDialogBuilder(view.context)
.setTitle(getString(R.string.choose_filter)) .setTitle(getString(R.string.choose_filter))
.setSingleChoiceItems( .setSingleChoiceItems(filterOptions, selectedFilter) { _, id ->
filterOptions, selectedFilter,
DialogInterface.OnClickListener { _, id ->
tempSelectedItem = id tempSelectedItem = id
} }
)
.setPositiveButton( .setPositiveButton(
getString(R.string.okay), getString(R.string.okay),
DialogInterface.OnClickListener { _, _ -> ) { _, _ ->
selectedFilter = tempSelectedItem selectedFilter = tempSelectedItem
apiSearchFilter = when (selectedFilter) { apiSearchFilter = when (selectedFilter) {
0 -> "all" 0 -> "all"
@ -115,7 +111,6 @@ class SearchFragment : Fragment() {
} }
fetchSearch(autoTextView.text.toString()) fetchSearch(autoTextView.text.toString())
} }
)
.setNegativeButton(getString(R.string.cancel), null) .setNegativeButton(getString(R.string.cancel), null)
.create() .create()
.show() .show()
@ -124,13 +119,13 @@ class SearchFragment : Fragment() {
// show search history // show search history
searchRecView.visibility = GONE searchRecView.visibility = GONE
historyRecycler.visibility = VISIBLE historyRecView.visibility = VISIBLE
historyRecycler.layoutManager = LinearLayoutManager(view.context) historyRecView.layoutManager = LinearLayoutManager(view.context)
val historyList = getHistory() val historyList = getHistory()
if (historyList.isNotEmpty()) { if (historyList.isNotEmpty()) {
historyRecycler.adapter = historyRecView.adapter =
SearchHistoryAdapter(requireContext(), historyList, autoTextView) SearchHistoryAdapter(requireContext(), historyList, autoTextView)
} }
@ -150,8 +145,6 @@ class SearchFragment : Fragment() {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (s!! != "") { if (s!! != "") {
searchRecView.visibility = VISIBLE
historyRecycler.visibility = GONE
searchRecView.adapter = null searchRecView.adapter = null
searchRecView.viewTreeObserver searchRecView.viewTreeObserver
@ -163,8 +156,6 @@ class SearchFragment : Fragment() {
GlobalScope.launch { GlobalScope.launch {
fetchSuggestions(s.toString(), autoTextView) fetchSuggestions(s.toString(), autoTextView)
delay(1000)
fetchSearch(s.toString())
} }
} }
} }
@ -172,10 +163,10 @@ class SearchFragment : Fragment() {
override fun afterTextChanged(s: Editable?) { override fun afterTextChanged(s: Editable?) {
if (s!!.isEmpty()) { if (s!!.isEmpty()) {
searchRecView.visibility = GONE searchRecView.visibility = GONE
historyRecycler.visibility = VISIBLE historyRecView.visibility = GONE
val historyList = getHistory() val historyList = getHistory()
if (historyList.isNotEmpty()) { if (historyList.isNotEmpty()) {
historyRecycler.adapter = historyRecView.adapter =
SearchHistoryAdapter(requireContext(), historyList, autoTextView) SearchHistoryAdapter(requireContext(), historyList, autoTextView)
} }
} }
@ -185,7 +176,9 @@ class SearchFragment : Fragment() {
OnEditorActionListener { _, actionId, _ -> OnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) { if (actionId == EditorInfo.IME_ACTION_SEARCH) {
hideKeyboard() hideKeyboard()
autoTextView.dismissDropDown() searchRecView.visibility = VISIBLE
historyRecView.visibility = GONE
fetchSearch(autoTextView.text.toString())
if (sharedPreferences.getBoolean( if (sharedPreferences.getBoolean(
"search_history_toggle", "search_history_toggle",
true true
@ -199,12 +192,9 @@ class SearchFragment : Fragment() {
false false
} }
) )
autoTextView.setOnItemClickListener { _, _, _, _ ->
hideKeyboard()
}
} }
private fun fetchSuggestions(query: String, autoTextView: AutoCompleteTextView) { private fun fetchSuggestions(query: String, autoTextView: EditText) {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.api.getSuggestions(query) RetrofitInstance.api.getSuggestions(query)
@ -216,9 +206,9 @@ class SearchFragment : Fragment() {
Log.e(TAG, "HttpException, unexpected response") Log.e(TAG, "HttpException, unexpected response")
return@launchWhenCreated return@launchWhenCreated
} }
val adapter = historyRecView.visibility = VISIBLE
ArrayAdapter(requireContext(), android.R.layout.simple_list_item_1, response) val suggestionsAdapter = SearchSuggestionsAdapter(response, autoTextView)
autoTextView.setAdapter(adapter) historyRecView.adapter = suggestionsAdapter
} }
} }

View File

@ -42,12 +42,11 @@
android:background="@android:color/transparent" android:background="@android:color/transparent"
app:hintEnabled="false"> app:hintEnabled="false">
<AutoCompleteTextView <EditText
android:id="@+id/autoCompleteTextView" android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@android:color/transparent" android:background="@android:color/transparent"
android:dropDownWidth="match_parent"
android:hint="@string/search_hint" android:hint="@string/search_hint"
android:imeOptions="actionSearch" android:imeOptions="actionSearch"
android:inputType="text" android:inputType="text"

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="16dp"
android:background="?android:attr/selectableItemBackground">
<ImageView
android:id="@+id/search_icon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@drawable/ic_search"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_margin="5dp" />
<TextView
android:id="@+id/suggestion_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/search_icon"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>