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 7dfdeaa72..baeea812d 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt @@ -6,10 +6,10 @@ import android.view.View import android.view.ViewGroup import android.widget.EditText import android.widget.TextView -import androidx.preference.PreferenceManager import androidx.recyclerview.widget.RecyclerView import com.github.libretube.R import com.github.libretube.fragments.SearchFragment +import com.github.libretube.util.PreferenceHelper import com.google.android.material.imageview.ShapeableImageView class SearchHistoryAdapter( @@ -35,9 +35,8 @@ class SearchHistoryAdapter( holder.v.findViewById(R.id.history_text).text = history holder.v.findViewById(R.id.delete_history).setOnClickListener { - val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) historyList = historyList - history - sharedPreferences.edit().putStringSet("search_history", HashSet(historyList)).apply() + PreferenceHelper.saveHistory(context, historyList) notifyDataSetChanged() } diff --git a/app/src/main/java/com/github/libretube/fragments/SearchFragment.kt b/app/src/main/java/com/github/libretube/fragments/SearchFragment.kt index 668af1bb8..b8fadffca 100644 --- a/app/src/main/java/com/github/libretube/fragments/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/SearchFragment.kt @@ -18,7 +18,6 @@ import android.widget.TextView.OnEditorActionListener import android.widget.TextView.VISIBLE import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope -import androidx.preference.PreferenceManager import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -267,7 +266,7 @@ class SearchFragment : Fragment() { private fun showHistory() { searchRecView.visibility = GONE - val historyList = getHistory() + val historyList = PreferenceHelper.getHistory(requireContext()) if (historyList.isNotEmpty()) { historyRecView.adapter = SearchHistoryAdapter(requireContext(), historyList, autoTextView, this) @@ -276,10 +275,9 @@ class SearchFragment : Fragment() { } private fun addToHistory(query: String) { - val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) val searchHistoryEnabled = PreferenceHelper.getBoolean(requireContext(), "search_history_toggle", true) if (searchHistoryEnabled) { - var historyList = getHistory() + var historyList = PreferenceHelper.getHistory(requireContext()) if ((historyList.isNotEmpty() && historyList.contains(query)) || query == "") { return @@ -291,20 +289,7 @@ class SearchFragment : Fragment() { historyList = historyList.takeLast(10) } - val set: Set = HashSet(historyList) - - sharedPreferences.edit().putStringSet("search_history", set) - .apply() - } - } - - private fun getHistory(): List { - return try { - val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) - val set: Set = sharedPreferences.getStringSet("search_history", HashSet())!! - set.toList() - } catch (e: Exception) { - emptyList() + PreferenceHelper.saveHistory(requireContext(), historyList) } } } diff --git a/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt index 2da49ac4d..11f5ddd65 100644 --- a/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt @@ -2,6 +2,7 @@ package com.github.libretube.util import android.content.Context import android.content.SharedPreferences +import androidx.core.content.ContentProviderCompat.requireContext import androidx.preference.PreferenceManager import com.github.libretube.obj.CustomInstance import com.google.common.reflect.TypeToken @@ -108,6 +109,22 @@ object PreferenceHelper { } } + fun getHistory(context: Context): List { + return try { + val settings = getDefaultSharedPreferences(context) + val set: Set = settings.getStringSet("search_history", HashSet())!! + set.toList() + } catch (e: Exception) { + emptyList() + } + } + + fun saveHistory(context: Context, historyList: List) { + val editor = getDefaultSharedPreferencesEditor(context) + val set: Set = HashSet(historyList) + editor.putStringSet("search_history", set).apply() + } + private fun getDefaultSharedPreferences(context: Context): SharedPreferences { return PreferenceManager.getDefaultSharedPreferences(context) }