mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 06:10:31 +05:30
move search history to prefhelper
This commit is contained in:
parent
25fab2df40
commit
6f1734c57e
@ -6,10 +6,10 @@ import android.view.View
|
|||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.EditText
|
import android.widget.EditText
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
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.github.libretube.fragments.SearchFragment
|
||||||
|
import com.github.libretube.util.PreferenceHelper
|
||||||
import com.google.android.material.imageview.ShapeableImageView
|
import com.google.android.material.imageview.ShapeableImageView
|
||||||
|
|
||||||
class SearchHistoryAdapter(
|
class SearchHistoryAdapter(
|
||||||
@ -35,9 +35,8 @@ class SearchHistoryAdapter(
|
|||||||
holder.v.findViewById<TextView>(R.id.history_text).text = history
|
holder.v.findViewById<TextView>(R.id.history_text).text = history
|
||||||
|
|
||||||
holder.v.findViewById<ShapeableImageView>(R.id.delete_history).setOnClickListener {
|
holder.v.findViewById<ShapeableImageView>(R.id.delete_history).setOnClickListener {
|
||||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
|
|
||||||
historyList = historyList - history
|
historyList = historyList - history
|
||||||
sharedPreferences.edit().putStringSet("search_history", HashSet(historyList)).apply()
|
PreferenceHelper.saveHistory(context, historyList)
|
||||||
notifyDataSetChanged()
|
notifyDataSetChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,6 @@ import android.widget.TextView.OnEditorActionListener
|
|||||||
import android.widget.TextView.VISIBLE
|
import android.widget.TextView.VISIBLE
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.preference.PreferenceManager
|
|
||||||
import androidx.recyclerview.widget.GridLayoutManager
|
import androidx.recyclerview.widget.GridLayoutManager
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
@ -267,7 +266,7 @@ class SearchFragment : Fragment() {
|
|||||||
|
|
||||||
private fun showHistory() {
|
private fun showHistory() {
|
||||||
searchRecView.visibility = GONE
|
searchRecView.visibility = GONE
|
||||||
val historyList = getHistory()
|
val historyList = PreferenceHelper.getHistory(requireContext())
|
||||||
if (historyList.isNotEmpty()) {
|
if (historyList.isNotEmpty()) {
|
||||||
historyRecView.adapter =
|
historyRecView.adapter =
|
||||||
SearchHistoryAdapter(requireContext(), historyList, autoTextView, this)
|
SearchHistoryAdapter(requireContext(), historyList, autoTextView, this)
|
||||||
@ -276,10 +275,9 @@ class SearchFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun addToHistory(query: String) {
|
private fun addToHistory(query: String) {
|
||||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
|
||||||
val searchHistoryEnabled = PreferenceHelper.getBoolean(requireContext(), "search_history_toggle", true)
|
val searchHistoryEnabled = PreferenceHelper.getBoolean(requireContext(), "search_history_toggle", true)
|
||||||
if (searchHistoryEnabled) {
|
if (searchHistoryEnabled) {
|
||||||
var historyList = getHistory()
|
var historyList = PreferenceHelper.getHistory(requireContext())
|
||||||
|
|
||||||
if ((historyList.isNotEmpty() && historyList.contains(query)) || query == "") {
|
if ((historyList.isNotEmpty() && historyList.contains(query)) || query == "") {
|
||||||
return
|
return
|
||||||
@ -291,20 +289,7 @@ class SearchFragment : Fragment() {
|
|||||||
historyList = historyList.takeLast(10)
|
historyList = historyList.takeLast(10)
|
||||||
}
|
}
|
||||||
|
|
||||||
val set: Set<String> = HashSet(historyList)
|
PreferenceHelper.saveHistory(requireContext(), historyList)
|
||||||
|
|
||||||
sharedPreferences.edit().putStringSet("search_history", set)
|
|
||||||
.apply()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getHistory(): List<String> {
|
|
||||||
return try {
|
|
||||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
|
||||||
val set: Set<String> = sharedPreferences.getStringSet("search_history", HashSet())!!
|
|
||||||
set.toList()
|
|
||||||
} catch (e: Exception) {
|
|
||||||
emptyList()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package com.github.libretube.util
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
|
import androidx.core.content.ContentProviderCompat.requireContext
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import com.github.libretube.obj.CustomInstance
|
import com.github.libretube.obj.CustomInstance
|
||||||
import com.google.common.reflect.TypeToken
|
import com.google.common.reflect.TypeToken
|
||||||
@ -108,6 +109,22 @@ object PreferenceHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getHistory(context: Context): List<String> {
|
||||||
|
return try {
|
||||||
|
val settings = getDefaultSharedPreferences(context)
|
||||||
|
val set: Set<String> = settings.getStringSet("search_history", HashSet())!!
|
||||||
|
set.toList()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
emptyList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun saveHistory(context: Context, historyList: List<String>) {
|
||||||
|
val editor = getDefaultSharedPreferencesEditor(context)
|
||||||
|
val set: Set<String> = HashSet(historyList)
|
||||||
|
editor.putStringSet("search_history", set).apply()
|
||||||
|
}
|
||||||
|
|
||||||
private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
|
private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
|
||||||
return PreferenceManager.getDefaultSharedPreferences(context)
|
return PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user