mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 00:10:32 +05:30
functionality and improvements
This commit is contained in:
parent
b1d9694432
commit
587bb9d553
@ -9,12 +9,14 @@ import android.widget.TextView
|
|||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
|
import com.github.libretube.fragments.SearchFragment
|
||||||
import com.google.android.material.imageview.ShapeableImageView
|
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: EditText
|
private val editText: EditText,
|
||||||
|
private val searchFragment: SearchFragment
|
||||||
) :
|
) :
|
||||||
RecyclerView.Adapter<SearchHistoryViewHolder>() {
|
RecyclerView.Adapter<SearchHistoryViewHolder>() {
|
||||||
|
|
||||||
@ -34,17 +36,14 @@ class SearchHistoryAdapter(
|
|||||||
|
|
||||||
holder.v.findViewById<ShapeableImageView>(R.id.delete_history).setOnClickListener {
|
holder.v.findViewById<ShapeableImageView>(R.id.delete_history).setOnClickListener {
|
||||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
|
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
|
|
||||||
historyList = historyList - history
|
historyList = historyList - history
|
||||||
|
sharedPreferences.edit().putStringSet("search_history", HashSet(historyList)).apply()
|
||||||
sharedPreferences.edit().putStringSet("search_history", HashSet(historyList))
|
|
||||||
.apply()
|
|
||||||
|
|
||||||
notifyDataSetChanged()
|
notifyDataSetChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
holder.v.setOnClickListener {
|
holder.v.setOnClickListener {
|
||||||
editText.setText(history)
|
editText.setText(history)
|
||||||
|
searchFragment.fetchSearch(history)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,12 @@ import android.widget.EditText
|
|||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
|
import com.github.libretube.fragments.SearchFragment
|
||||||
|
|
||||||
class SearchSuggestionsAdapter(
|
class SearchSuggestionsAdapter(
|
||||||
private var suggestionsList: List<String>,
|
private var suggestionsList: List<String>,
|
||||||
private var autoCompleteTextView: EditText
|
private var editText: EditText,
|
||||||
|
private val searchFragment: SearchFragment
|
||||||
) :
|
) :
|
||||||
RecyclerView.Adapter<SearchSuggestionsViewHolder>() {
|
RecyclerView.Adapter<SearchSuggestionsViewHolder>() {
|
||||||
|
|
||||||
@ -29,7 +31,8 @@ class SearchSuggestionsAdapter(
|
|||||||
val suggestionTextView = holder.v.findViewById<TextView>(R.id.suggestion_text)
|
val suggestionTextView = holder.v.findViewById<TextView>(R.id.suggestion_text)
|
||||||
suggestionTextView.text = suggestion
|
suggestionTextView.text = suggestion
|
||||||
holder.v.setOnClickListener {
|
holder.v.setOnClickListener {
|
||||||
autoCompleteTextView.setText(suggestion)
|
editText.setText(suggestion)
|
||||||
|
searchFragment.fetchSearch(editText.text.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,8 +41,10 @@ class SearchFragment : Fragment() {
|
|||||||
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 lateinit var historyRecView: RecyclerView
|
||||||
|
private lateinit var autoTextView: EditText
|
||||||
private var searchAdapter: SearchAdapter? = null
|
private var searchAdapter: SearchAdapter? = null
|
||||||
private var isLoading: Boolean = true
|
private var isLoading: Boolean = true
|
||||||
|
private var isFetchingSearch: Boolean = false
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@ -63,16 +65,13 @@ class SearchFragment : Fragment() {
|
|||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
searchRecView = view.findViewById(R.id.search_recycler)
|
searchRecView = view.findViewById(R.id.search_recycler)
|
||||||
historyRecView = view.findViewById(R.id.history_recycler)
|
historyRecView = view.findViewById(R.id.history_recycler)
|
||||||
|
autoTextView = view.findViewById(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 filterImageView = view.findViewById<ImageView>(R.id.filterMenu_imageView)
|
val filterImageView = view.findViewById<ImageView>(R.id.filterMenu_imageView)
|
||||||
|
|
||||||
var tempSelectedItem = 0
|
var tempSelectedItem = 0
|
||||||
|
|
||||||
val sharedPreferences =
|
|
||||||
PreferenceManager.getDefaultSharedPreferences(requireContext())
|
|
||||||
|
|
||||||
clearSearchButton.setOnClickListener {
|
clearSearchButton.setOnClickListener {
|
||||||
autoTextView.text.clear()
|
autoTextView.text.clear()
|
||||||
}
|
}
|
||||||
@ -117,23 +116,15 @@ class SearchFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// show search history
|
// show search history
|
||||||
|
|
||||||
searchRecView.visibility = GONE
|
|
||||||
historyRecView.visibility = VISIBLE
|
|
||||||
|
|
||||||
historyRecView.layoutManager = LinearLayoutManager(view.context)
|
historyRecView.layoutManager = LinearLayoutManager(view.context)
|
||||||
|
showHistory()
|
||||||
val historyList = getHistory()
|
|
||||||
if (historyList.isNotEmpty()) {
|
|
||||||
historyRecView.adapter =
|
|
||||||
SearchHistoryAdapter(requireContext(), historyList, autoTextView)
|
|
||||||
}
|
|
||||||
|
|
||||||
searchRecView.layoutManager = GridLayoutManager(view.context, 1)
|
searchRecView.layoutManager = GridLayoutManager(view.context, 1)
|
||||||
autoTextView.requestFocus()
|
autoTextView.requestFocus()
|
||||||
val imm =
|
val imm =
|
||||||
requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||||
imm.showSoftInput(autoTextView, InputMethodManager.SHOW_IMPLICIT)
|
imm.showSoftInput(autoTextView, InputMethodManager.SHOW_IMPLICIT)
|
||||||
|
|
||||||
autoTextView.addTextChangedListener(object : TextWatcher {
|
autoTextView.addTextChangedListener(object : TextWatcher {
|
||||||
override fun beforeTextChanged(
|
override fun beforeTextChanged(
|
||||||
s: CharSequence?,
|
s: CharSequence?,
|
||||||
@ -162,13 +153,7 @@ class SearchFragment : Fragment() {
|
|||||||
|
|
||||||
override fun afterTextChanged(s: Editable?) {
|
override fun afterTextChanged(s: Editable?) {
|
||||||
if (s!!.isEmpty()) {
|
if (s!!.isEmpty()) {
|
||||||
searchRecView.visibility = GONE
|
showHistory()
|
||||||
historyRecView.visibility = GONE
|
|
||||||
val historyList = getHistory()
|
|
||||||
if (historyList.isNotEmpty()) {
|
|
||||||
historyRecView.adapter =
|
|
||||||
SearchHistoryAdapter(requireContext(), historyList, autoTextView)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -179,14 +164,6 @@ class SearchFragment : Fragment() {
|
|||||||
searchRecView.visibility = VISIBLE
|
searchRecView.visibility = VISIBLE
|
||||||
historyRecView.visibility = GONE
|
historyRecView.visibility = GONE
|
||||||
fetchSearch(autoTextView.text.toString())
|
fetchSearch(autoTextView.text.toString())
|
||||||
if (sharedPreferences.getBoolean(
|
|
||||||
"search_history_toggle",
|
|
||||||
true
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
val newString = autoTextView.text.toString()
|
|
||||||
addToHistory(newString)
|
|
||||||
}
|
|
||||||
return@OnEditorActionListener true
|
return@OnEditorActionListener true
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
@ -195,25 +172,33 @@ class SearchFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun fetchSuggestions(query: String, autoTextView: EditText) {
|
private fun fetchSuggestions(query: String, autoTextView: EditText) {
|
||||||
lifecycleScope.launchWhenCreated {
|
fun run() {
|
||||||
val response = try {
|
lifecycleScope.launchWhenCreated {
|
||||||
RetrofitInstance.api.getSuggestions(query)
|
searchRecView.visibility = GONE
|
||||||
} catch (e: IOException) {
|
historyRecView.visibility = VISIBLE
|
||||||
println(e)
|
val response = try {
|
||||||
Log.e(TAG, "IOException, you might not have internet connection")
|
RetrofitInstance.api.getSuggestions(query)
|
||||||
return@launchWhenCreated
|
} catch (e: IOException) {
|
||||||
} catch (e: HttpException) {
|
println(e)
|
||||||
Log.e(TAG, "HttpException, unexpected response")
|
Log.e(TAG, "IOException, you might not have internet connection")
|
||||||
return@launchWhenCreated
|
return@launchWhenCreated
|
||||||
|
} catch (e: HttpException) {
|
||||||
|
Log.e(TAG, "HttpException, unexpected response")
|
||||||
|
return@launchWhenCreated
|
||||||
|
}
|
||||||
|
val suggestionsAdapter =
|
||||||
|
SearchSuggestionsAdapter(response, autoTextView, this@SearchFragment)
|
||||||
|
historyRecView.adapter = suggestionsAdapter
|
||||||
}
|
}
|
||||||
historyRecView.visibility = VISIBLE
|
|
||||||
val suggestionsAdapter = SearchSuggestionsAdapter(response, autoTextView)
|
|
||||||
historyRecView.adapter = suggestionsAdapter
|
|
||||||
}
|
}
|
||||||
|
if (!isFetchingSearch) run()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fetchSearch(query: String) {
|
fun fetchSearch(query: String) {
|
||||||
lifecycleScope.launchWhenCreated {
|
lifecycleScope.launchWhenCreated {
|
||||||
|
isFetchingSearch = true
|
||||||
|
hideKeyboard()
|
||||||
|
Log.e("here", "here")
|
||||||
val response = try {
|
val response = try {
|
||||||
RetrofitInstance.api.getSearchResults(query, apiSearchFilter)
|
RetrofitInstance.api.getSearchResults(query, apiSearchFilter)
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
@ -227,11 +212,15 @@ class SearchFragment : Fragment() {
|
|||||||
nextPage = response.nextpage
|
nextPage = response.nextpage
|
||||||
if (response.items!!.isNotEmpty()) {
|
if (response.items!!.isNotEmpty()) {
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
|
historyRecView.visibility = GONE
|
||||||
|
searchRecView.visibility = VISIBLE
|
||||||
searchAdapter = SearchAdapter(response.items, childFragmentManager)
|
searchAdapter = SearchAdapter(response.items, childFragmentManager)
|
||||||
searchRecView.adapter = searchAdapter
|
searchRecView.adapter = searchAdapter
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
addToHistory(query)
|
||||||
isLoading = false
|
isLoading = false
|
||||||
|
isFetchingSearch = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,27 +265,37 @@ class SearchFragment : Fragment() {
|
|||||||
hideKeyboard()
|
hideKeyboard()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun showHistory() {
|
||||||
|
searchRecView.visibility = GONE
|
||||||
|
val historyList = getHistory()
|
||||||
|
if (historyList.isNotEmpty()) {
|
||||||
|
historyRecView.adapter =
|
||||||
|
SearchHistoryAdapter(requireContext(), historyList, autoTextView, this)
|
||||||
|
historyRecView.visibility = VISIBLE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun addToHistory(query: String) {
|
private fun addToHistory(query: String) {
|
||||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||||
|
val searchHistoryEnabled = sharedPreferences.getBoolean("search_history_toggle", true)
|
||||||
|
if (searchHistoryEnabled) {
|
||||||
|
var historyList = getHistory()
|
||||||
|
|
||||||
var historyList = getHistory()
|
if ((historyList.isNotEmpty() && historyList.contains(query)) || query == "") {
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
historyList = historyList + query
|
||||||
|
}
|
||||||
|
|
||||||
if (historyList.isNotEmpty() && query == historyList[historyList.size - 1]) {
|
if (historyList.size > 10) {
|
||||||
return
|
historyList = historyList.takeLast(10)
|
||||||
} else if (query == "") {
|
}
|
||||||
return
|
|
||||||
} else {
|
val set: Set<String> = HashSet(historyList)
|
||||||
historyList = historyList + query
|
|
||||||
|
sharedPreferences.edit().putStringSet("search_history", set)
|
||||||
|
.apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (historyList.size > 10) {
|
|
||||||
historyList = historyList.takeLast(10)
|
|
||||||
}
|
|
||||||
|
|
||||||
val set: Set<String> = HashSet(historyList)
|
|
||||||
|
|
||||||
sharedPreferences.edit().putStringSet("search_history", set)
|
|
||||||
.apply()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getHistory(): List<String> {
|
private fun getHistory(): List<String> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user