Search history now saves as stringset

This commit is contained in:
archroid 2022-05-10 20:12:17 +04:30
parent 5b1f16fe97
commit 580e99329d
No known key found for this signature in database
GPG Key ID: D8EE5C11EDF911B1
2 changed files with 15 additions and 11 deletions

View File

@ -180,32 +180,36 @@ class SearchFragment : Fragment() {
private fun addtohistory(query: String) { private fun addtohistory(query: String) {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
var splited_history = getHistory() var historyList = getHistory()
if (query == splited_history.get(splited_history.size - 1)) {
if (historyList.size != 0 && query == historyList.get(historyList.size - 1)) {
return return
} else if (query == "") { } else if (query == "") {
return return
} else { } else {
splited_history = splited_history + query historyList = historyList + query
} }
if (splited_history.size > 10) { if (historyList.size > 10) {
splited_history = splited_history.takeLast(10) historyList = historyList.takeLast(10)
} }
var set: Set<String> = HashSet(historyList)
sharedPreferences.edit().putString("search_history", splited_history.joinToString("|")) sharedPreferences.edit().putStringSet("search_history", set)
.apply() .apply()
} }
private fun getHistory(): List<String> { private fun getHistory(): List<String> {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
var history = sharedPreferences.getString("search_history", "")
var splited_history = history!!.split("|") val set: Set<String> = sharedPreferences.getStringSet("search_history", null)!!
return splited_history
return set.toList()
} }
} }

View File

@ -16,7 +16,7 @@ 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 val historyList: List<String> , private val editText : AutoCompleteTextView) :
RecyclerView.Adapter<SearchHistoryViewHolder>() { RecyclerView.Adapter<SearchHistoryViewHolder>() {
override fun getItemCount(): Int { override fun getItemCount(): Int {
return historyList.size -1 return historyList.size
} }
@ -27,7 +27,7 @@ class SearchHistoryAdapter(private val context: Context, private val historyList
} }
override fun onBindViewHolder(holder: SearchHistoryViewHolder, position: Int) { override fun onBindViewHolder(holder: SearchHistoryViewHolder, position: Int) {
val history = historyList[position+1] val history = historyList[position]
holder.v.findViewById<TextView>(R.id.history_text).text = history holder.v.findViewById<TextView>(R.id.history_text).text = history