diff --git a/app/src/main/java/com/github/libretube/SearchFragment.kt b/app/src/main/java/com/github/libretube/SearchFragment.kt index 6f79f2509..7a7cec045 100644 --- a/app/src/main/java/com/github/libretube/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/SearchFragment.kt @@ -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(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 } diff --git a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt index c12d218e1..433f78588 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt @@ -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) : +class SearchHistoryAdapter(private val context: Context, private val historyList: List) : RecyclerView.Adapter() { override fun getItemCount(): Int { return historyList.size @@ -23,9 +26,18 @@ class SearchHistoryAdapter(private val historyList: List) : override fun onBindViewHolder(holder: SearchHistoryViewHolder, position: Int) { val history = historyList[position] holder.v.findViewById(R.id.history_text).text = history -// holder.v.findViewById(R.id.delete_history).setOnClickListener( -// -// ) + + + holder.v.findViewById(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() + + } } }