Wokring on showing history

This commit is contained in:
archroid 2022-05-09 22:20:36 +04:30
parent 6dcd8fafc5
commit 8149e69210
No known key found for this signature in database
GPG Key ID: D8EE5C11EDF911B1
2 changed files with 53 additions and 27 deletions

View File

@ -62,41 +62,53 @@ class SearchFragment : Fragment() {
historyRecycler.visibility = VISIBLE
history_tv.visibility = VISIBLE
recyclerView.layoutManager = LinearLayoutManager(view.context)
recyclerView.adapter = SearchHistoryAdapter(getHistory())
historyRecycler.layoutManager = LinearLayoutManager(view.context)
historyRecycler.adapter = SearchHistoryAdapter(requireContext(),getHistory())
recyclerView.layoutManager = GridLayoutManager(view.context, 1)
val autoTextView = view.findViewById<AutoCompleteTextView>(R.id.autoCompleteTextView)
autoTextView.requestFocus()
val imm = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val imm =
requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm!!.showSoftInput(autoTextView, InputMethodManager.SHOW_IMPLICIT)
autoTextView.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(
s: CharSequence?,
start: Int,
count: Int,
after: Int
) {
autoTextView.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(
s: CharSequence?,
start: Int,
count: Int,
after: Int
) {
}
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if(s!! != ""){
GlobalScope.launch {
fetchSuggestions(s.toString(), autoTextView)
delay(3000)
addtohistory(s.toString())
fetchSearch(s.toString(),recyclerView)
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (s!! != "") {
recyclerView.visibility = VISIBLE
historyRecycler.visibility = GONE
history_tv.visibility = GONE
recyclerView.adapter = null
GlobalScope.launch {
fetchSuggestions(s.toString(), autoTextView)
delay(3000)
addtohistory(s.toString())
fetchSearch(s.toString(), recyclerView)
}
}
override fun afterTextChanged(s: Editable?) {
}
}
})
override fun afterTextChanged(s: Editable?) {
if (s!!.isEmpty()) {
recyclerView.visibility = GONE
historyRecycler.visibility = VISIBLE
history_tv.visibility = VISIBLE
historyRecycler.adapter = SearchHistoryAdapter(requireContext(),getHistory())
}
}
})
autoTextView.setOnEditorActionListener(OnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
hideKeyboard();
@ -170,6 +182,8 @@ class SearchFragment : Fragment() {
if (query == splited_history.get(splited_history.size - 1)) {
return
} else if (query == "") {
return
} else {
splited_history = splited_history + query
}

View File

@ -1,13 +1,16 @@
package com.github.libretube.adapters
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.google.android.material.imageview.ShapeableImageView
class SearchHistoryAdapter(private val historyList: List<String>) :
class SearchHistoryAdapter(private val context: Context, private val historyList: List<String>) :
RecyclerView.Adapter<SearchHistoryViewHolder>() {
override fun getItemCount(): Int {
return historyList.size
@ -23,9 +26,18 @@ class SearchHistoryAdapter(private val historyList: List<String>) :
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(
//
// )
holder.v.findViewById<ShapeableImageView>(R.id.delete_history).setOnClickListener {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
var splited_history = sharedPreferences.getString("search_history", "")!!.split("|")
splited_history = splited_history - history
sharedPreferences.edit().putString("search_history", splited_history.joinToString("|"))
.apply()
}
}
}