Search history works fine without any bugs

This commit is contained in:
archroid 2022-05-10 20:36:20 +04:30
parent 580e99329d
commit 8121e8534b
No known key found for this signature in database
GPG Key ID: D8EE5C11EDF911B1
2 changed files with 21 additions and 9 deletions

View File

@ -15,7 +15,6 @@ import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.TextView
import android.widget.TextView.*
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
@ -66,7 +65,12 @@ class SearchFragment : Fragment() {
history_tv.visibility = VISIBLE
historyRecycler.layoutManager = LinearLayoutManager(view.context)
historyRecycler.adapter = SearchHistoryAdapter(requireContext(),getHistory(),autoTextView)
var historylist = getHistory()
if (historylist.size != 0) {
historyRecycler.adapter =
SearchHistoryAdapter(requireContext(), historylist, autoTextView)
}
recyclerView.layoutManager = GridLayoutManager(view.context, 1)
autoTextView.requestFocus()
@ -106,7 +110,11 @@ class SearchFragment : Fragment() {
recyclerView.visibility = GONE
historyRecycler.visibility = VISIBLE
history_tv.visibility = VISIBLE
historyRecycler.adapter = SearchHistoryAdapter(requireContext(),getHistory(),autoTextView)
var historylist = getHistory()
if (historylist.size != 0) {
historyRecycler.adapter =
SearchHistoryAdapter(requireContext(), historylist, autoTextView)
}
}
}
@ -206,9 +214,7 @@ class SearchFragment : Fragment() {
private fun getHistory(): List<String> {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
val set: Set<String> = sharedPreferences.getStringSet("search_history", null)!!
return set.toList()
}
}

View File

@ -13,8 +13,10 @@ import com.github.libretube.R
import com.google.android.material.imageview.ShapeableImageView
class SearchHistoryAdapter(private val context: Context, private val historyList: List<String> , private val editText : AutoCompleteTextView) :
class SearchHistoryAdapter(private val context: Context, private var historyList: List<String> , private val editText : AutoCompleteTextView) :
RecyclerView.Adapter<SearchHistoryViewHolder>() {
override fun getItemCount(): Int {
return historyList.size
}
@ -33,15 +35,19 @@ class SearchHistoryAdapter(private val context: Context, private val historyList
holder.v.findViewById<ShapeableImageView>(R.id.delete_history).setOnClickListener {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
var splited_history = sharedPreferences.getString("search_history", "")!!.split("|")
// var historyList = sharedPreferences.getStringSet("search_history", null)!!.toList()
splited_history = splited_history - history
historyList = historyList - history
sharedPreferences.edit().putString("search_history", splited_history.joinToString("|"))
sharedPreferences.edit().putStringSet("search_history", HashSet(historyList))
.apply()
Log.d("TAG", "onBindViewHolder: " + historyList.size)
notifyDataSetChanged()
}
holder.v.setOnClickListener {
editText.setText(history)
}