Saving history wokrs fine

This commit is contained in:
archroid 2022-05-09 21:50:07 +04:30
parent a15707f247
commit 6dcd8fafc5
No known key found for this signature in database
GPG Key ID: D8EE5C11EDF911B1
2 changed files with 55 additions and 10 deletions

View File

@ -22,6 +22,7 @@ import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.adapters.SearchAdapter
import com.github.libretube.adapters.SearchHistoryAdapter
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
@ -62,7 +63,7 @@ class SearchFragment : Fragment() {
history_tv.visibility = VISIBLE
recyclerView.layoutManager = LinearLayoutManager(view.context)
recyclerView.adapter
recyclerView.adapter = SearchHistoryAdapter(getHistory())
recyclerView.layoutManager = GridLayoutManager(view.context, 1)
@ -163,19 +164,32 @@ class SearchFragment : Fragment() {
}
private fun addtohistory(query: String) {
var queryFromated = "|" + query
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
var history = sharedPreferences.getString("search_history", "")
var splited_history = getHistory()
if (query == splited_history.get(splited_history.size - 1)) {
return
} else {
splited_history = splited_history + query
}
var splited_history = history!!.split("|") + queryFromated
if (splited_history.size > 10) {
splited_history.drop(9)
splited_history = splited_history.takeLast(10)
}
sharedPreferences.edit().putString("search_history",splited_history.joinToString("|") ).apply()
sharedPreferences.edit().putString("search_history", splited_history.joinToString("|"))
.apply()
}
private fun getHistory(): List<String> {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
var history = sharedPreferences.getString("search_history", "")
var splited_history = history!!.split("|")
return splited_history
}
}

View File

@ -1,4 +1,35 @@
package com.github.libretube.adapters
class SearchHistoryAdapter {
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
class SearchHistoryAdapter(private val historyList: List<String>) :
RecyclerView.Adapter<SearchHistoryViewHolder>() {
override fun getItemCount(): Int {
return historyList.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchHistoryViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val cell = layoutInflater.inflate(R.layout.searchhistory_row, parent, false)
return SearchHistoryViewHolder(cell)
}
override fun onBindViewHolder(holder: SearchHistoryViewHolder, position: Int) {
val history = historyList[position]
holder.v.findViewById<TextView>(R.id.history_text).text = history
// holder.v.findViewById<TextView>(R.id.delete_history).setOnClickListener(
//
// )
}
}
class SearchHistoryViewHolder(val v: View) : RecyclerView.ViewHolder(v) {
init {
}
}