mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20:30 +05:30
Saving history wokrs fine
This commit is contained in:
parent
a15707f247
commit
6dcd8fafc5
@ -22,6 +22,7 @@ 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
|
||||||
import com.github.libretube.adapters.SearchAdapter
|
import com.github.libretube.adapters.SearchAdapter
|
||||||
|
import com.github.libretube.adapters.SearchHistoryAdapter
|
||||||
import kotlinx.coroutines.GlobalScope
|
import kotlinx.coroutines.GlobalScope
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@ -62,7 +63,7 @@ class SearchFragment : Fragment() {
|
|||||||
history_tv.visibility = VISIBLE
|
history_tv.visibility = VISIBLE
|
||||||
|
|
||||||
recyclerView.layoutManager = LinearLayoutManager(view.context)
|
recyclerView.layoutManager = LinearLayoutManager(view.context)
|
||||||
recyclerView.adapter
|
recyclerView.adapter = SearchHistoryAdapter(getHistory())
|
||||||
|
|
||||||
|
|
||||||
recyclerView.layoutManager = GridLayoutManager(view.context, 1)
|
recyclerView.layoutManager = GridLayoutManager(view.context, 1)
|
||||||
@ -163,19 +164,32 @@ class SearchFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun addtohistory(query: String) {
|
private fun addtohistory(query: String) {
|
||||||
var queryFromated = "|" + query
|
|
||||||
|
|
||||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||||
|
|
||||||
var history = sharedPreferences.getString("search_history", "")
|
var splited_history = getHistory()
|
||||||
|
|
||||||
var splited_history = history!!.split("|") + queryFromated
|
if (query == splited_history.get(splited_history.size - 1)) {
|
||||||
|
return
|
||||||
if (splited_history.size > 10) {
|
} else {
|
||||||
splited_history.drop(9)
|
splited_history = splited_history + query
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sharedPreferences.edit().putString("search_history",splited_history.joinToString("|") ).apply()
|
if (splited_history.size > 10) {
|
||||||
|
splited_history = splited_history.takeLast(10)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,35 @@
|
|||||||
package com.github.libretube.adapters
|
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 {
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user