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

View File

@ -1,13 +1,16 @@
package com.github.libretube.adapters package com.github.libretube.adapters
import android.content.Context
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
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.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>() { RecyclerView.Adapter<SearchHistoryViewHolder>() {
override fun getItemCount(): Int { override fun getItemCount(): Int {
return historyList.size return historyList.size
@ -23,9 +26,18 @@ class SearchHistoryAdapter(private val historyList: List<String>) :
override fun onBindViewHolder(holder: SearchHistoryViewHolder, position: Int) { override fun onBindViewHolder(holder: SearchHistoryViewHolder, position: Int) {
val history = historyList[position] val history = historyList[position]
holder.v.findViewById<TextView>(R.id.history_text).text = history 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()
}
} }
} }