diff --git a/app/src/main/java/com/github/libretube/SearchFragment.kt b/app/src/main/java/com/github/libretube/SearchFragment.kt index da9aa6727..87d88ecde 100644 --- a/app/src/main/java/com/github/libretube/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/SearchFragment.kt @@ -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 { val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) - val set: Set = sharedPreferences.getStringSet("search_history", null)!! - return set.toList() } } diff --git a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt index 1bdfbe3a1..6bfc96403 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt @@ -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 , private val editText : AutoCompleteTextView) : +class SearchHistoryAdapter(private val context: Context, private var historyList: List , private val editText : AutoCompleteTextView) : RecyclerView.Adapter() { + + override fun getItemCount(): Int { return historyList.size } @@ -33,15 +35,19 @@ class SearchHistoryAdapter(private val context: Context, private val historyList holder.v.findViewById(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) }