fixed search results showing duplicates on fresh boot
This commit is contained in:
parent
de044b7968
commit
d6c5e6b692
2 changed files with 96 additions and 92 deletions
|
|
@ -1,8 +1,6 @@
|
||||||
package com.deniscerri.ytdlnis.database.dao
|
package com.deniscerri.ytdlnis.database.dao
|
||||||
|
|
||||||
import androidx.room.Dao
|
import androidx.room.*
|
||||||
import androidx.room.Insert
|
|
||||||
import androidx.room.Query
|
|
||||||
import com.deniscerri.ytdlnis.database.models.SearchHistoryItem
|
import com.deniscerri.ytdlnis.database.models.SearchHistoryItem
|
||||||
|
|
||||||
@Dao
|
@Dao
|
||||||
|
|
@ -13,7 +11,7 @@ interface SearchHistoryDao {
|
||||||
@Query("SELECT * from searchHistory WHERE query COLLATE NOCASE ='%'||:keyword||'%'")
|
@Query("SELECT * from searchHistory WHERE query COLLATE NOCASE ='%'||:keyword||'%'")
|
||||||
fun getAllByKeyword(keyword: String) : List<SearchHistoryItem>
|
fun getAllByKeyword(keyword: String) : List<SearchHistoryItem>
|
||||||
|
|
||||||
@Insert
|
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||||
suspend fun insert(new: SearchHistoryItem)
|
suspend fun insert(new: SearchHistoryItem)
|
||||||
|
|
||||||
@Query("DELETE FROM searchHistory")
|
@Query("DELETE FROM searchHistory")
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ import android.graphics.drawable.RippleDrawable
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
|
import android.text.Editable
|
||||||
import android.util.DisplayMetrics
|
import android.util.DisplayMetrics
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.view.*
|
import android.view.*
|
||||||
|
|
@ -26,6 +27,7 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout
|
||||||
import androidx.core.view.children
|
import androidx.core.view.children
|
||||||
import androidx.core.view.forEach
|
import androidx.core.view.forEach
|
||||||
import androidx.core.widget.addTextChangedListener
|
import androidx.core.widget.addTextChangedListener
|
||||||
|
import androidx.core.widget.doAfterTextChanged
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
|
@ -256,6 +258,7 @@ class HomeFragment : Fragment(), HomeAdapter.OnItemClickListener, View.OnClickLi
|
||||||
}else{
|
}else{
|
||||||
linkYouCopied.visibility = GONE
|
linkYouCopied.visibility = GONE
|
||||||
}
|
}
|
||||||
|
updateSearchViewItems(searchView.editText.text, searchView, linkYouCopied)
|
||||||
}catch (e: Exception){
|
}catch (e: Exception){
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
linkYouCopied.visibility = GONE
|
linkYouCopied.visibility = GONE
|
||||||
|
|
@ -263,7 +266,78 @@ class HomeFragment : Fragment(), HomeAdapter.OnItemClickListener, View.OnClickLi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
searchView.editText.addTextChangedListener {
|
searchView.editText.doAfterTextChanged {
|
||||||
|
if (searchView.currentTransitionState != SearchView.TransitionState.SHOWN) return@doAfterTextChanged
|
||||||
|
updateSearchViewItems(it, searchView, linkYouCopied)
|
||||||
|
}
|
||||||
|
|
||||||
|
searchView.editText.setOnTouchListener(OnTouchListener { v, event ->
|
||||||
|
try{
|
||||||
|
val drawableLeft = 0
|
||||||
|
val drawableTop = 1
|
||||||
|
val drawableRight = 2
|
||||||
|
val drawableBottom = 3
|
||||||
|
if (event.action == MotionEvent.ACTION_UP) {
|
||||||
|
if (
|
||||||
|
(isRightToLeft && (event.x < (searchView.editText.left - searchView.editText.compoundDrawables[drawableLeft].bounds.width()))) ||
|
||||||
|
(!isRightToLeft && (event.x > (searchView.editText.right - searchView.editText.compoundDrawables[drawableRight].bounds.width())))
|
||||||
|
){
|
||||||
|
|
||||||
|
val present = queriesChipGroup!!.children.firstOrNull { (it as Chip).text.toString() == searchView.editText.text.toString() }
|
||||||
|
if (present == null) {
|
||||||
|
val chip = layoutinflater!!.inflate(R.layout.input_chip, queriesChipGroup, false) as Chip
|
||||||
|
chip.text = searchView.editText.text
|
||||||
|
chip.chipBackgroundColor = ColorStateList.valueOf(MaterialColors.getColor(requireContext(), R.attr.colorSecondaryContainer, Color.BLACK))
|
||||||
|
chip.setOnClickListener {
|
||||||
|
queriesChipGroup!!.removeView(chip)
|
||||||
|
}
|
||||||
|
queriesChipGroup!!.addView(chip)
|
||||||
|
}
|
||||||
|
if (queriesChipGroup!!.childCount == 0) queriesConstraint.visibility = GONE
|
||||||
|
else queriesConstraint.visibility = VISIBLE
|
||||||
|
searchView.editText.setText("")
|
||||||
|
return@OnTouchListener true
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}catch (ignored: Exception){}
|
||||||
|
false
|
||||||
|
})
|
||||||
|
|
||||||
|
searchView.editText.setOnEditorActionListener { textView, i, keyEvent ->
|
||||||
|
initSearch(searchView)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
searchBar!!.setOnMenuItemClickListener { m: MenuItem ->
|
||||||
|
when (m.itemId) {
|
||||||
|
R.id.delete_results -> {
|
||||||
|
resultViewModel.getTrending()
|
||||||
|
selectedObjects = ArrayList()
|
||||||
|
searchBar!!.setText("")
|
||||||
|
downloadAllFabCoordinator!!.visibility = GONE
|
||||||
|
downloadFabs!!.visibility = GONE
|
||||||
|
}
|
||||||
|
R.id.delete_search -> {
|
||||||
|
resultViewModel.deleteAllSearchQueryHistory()
|
||||||
|
searchSuggestionsLinearLayout!!.removeAllViews()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
queriesChipGroup!!.addOnLayoutChangeListener { view, i, i2, i3, i4, i5, i6, i7, i8 ->
|
||||||
|
if (queriesChipGroup!!.childCount == 0) queriesConstraint.visibility = GONE
|
||||||
|
else queriesConstraint.visibility = VISIBLE
|
||||||
|
searchView.editText.setText("")
|
||||||
|
}
|
||||||
|
|
||||||
|
queriesInitStartBtn.setOnClickListener {
|
||||||
|
initSearch(searchView)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateSearchViewItems(it: Editable?, searchView: SearchView, linkYouCopied: View?){
|
||||||
|
lifecycleScope.launch {
|
||||||
searchSuggestionsLinearLayout!!.removeAllViews()
|
searchSuggestionsLinearLayout!!.removeAllViews()
|
||||||
searchHistoryLinearLayout!!.removeAllViews()
|
searchHistoryLinearLayout!!.removeAllViews()
|
||||||
|
|
||||||
|
|
@ -276,8 +350,6 @@ class HomeFragment : Fragment(), HomeAdapter.OnItemClickListener, View.OnClickLi
|
||||||
}else{
|
}else{
|
||||||
searchView.editText.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.ic_plus, 0)
|
searchView.editText.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.ic_plus, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
lifecycleScope.launch {
|
|
||||||
val suggestions = withContext(Dispatchers.IO){
|
val suggestions = withContext(Dispatchers.IO){
|
||||||
if (it!!.isEmpty()) {
|
if (it!!.isEmpty()) {
|
||||||
resultViewModel.getSearchHistory().map { it.query }
|
resultViewModel.getSearchHistory().map { it.query }
|
||||||
|
|
@ -350,72 +422,6 @@ class HomeFragment : Fragment(), HomeAdapter.OnItemClickListener, View.OnClickLi
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
searchView.editText.setOnTouchListener(OnTouchListener { v, event ->
|
|
||||||
try{
|
|
||||||
val drawableLeft = 0
|
|
||||||
val drawableTop = 1
|
|
||||||
val drawableRight = 2
|
|
||||||
val drawableBottom = 3
|
|
||||||
if (event.action == MotionEvent.ACTION_UP) {
|
|
||||||
if (
|
|
||||||
(isRightToLeft && (event.x < (searchView.editText.left - searchView.editText.compoundDrawables[drawableLeft].bounds.width()))) ||
|
|
||||||
(!isRightToLeft && (event.x > (searchView.editText.right - searchView.editText.compoundDrawables[drawableRight].bounds.width())))
|
|
||||||
){
|
|
||||||
|
|
||||||
val present = queriesChipGroup!!.children.firstOrNull { (it as Chip).text.toString() == searchView.editText.text.toString() }
|
|
||||||
if (present == null) {
|
|
||||||
val chip = layoutinflater!!.inflate(R.layout.input_chip, queriesChipGroup, false) as Chip
|
|
||||||
chip.text = searchView.editText.text
|
|
||||||
chip.chipBackgroundColor = ColorStateList.valueOf(MaterialColors.getColor(requireContext(), R.attr.colorSecondaryContainer, Color.BLACK))
|
|
||||||
chip.setOnClickListener {
|
|
||||||
queriesChipGroup!!.removeView(chip)
|
|
||||||
}
|
|
||||||
queriesChipGroup!!.addView(chip)
|
|
||||||
}
|
|
||||||
if (queriesChipGroup!!.childCount == 0) queriesConstraint.visibility = GONE
|
|
||||||
else queriesConstraint.visibility = VISIBLE
|
|
||||||
searchView.editText.setText("")
|
|
||||||
return@OnTouchListener true
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}catch (ignored: Exception){}
|
|
||||||
false
|
|
||||||
})
|
|
||||||
|
|
||||||
searchView.editText.setOnEditorActionListener { textView, i, keyEvent ->
|
|
||||||
initSearch(searchView)
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
searchBar!!.setOnMenuItemClickListener { m: MenuItem ->
|
|
||||||
when (m.itemId) {
|
|
||||||
R.id.delete_results -> {
|
|
||||||
resultViewModel.getTrending()
|
|
||||||
selectedObjects = ArrayList()
|
|
||||||
searchBar!!.setText("")
|
|
||||||
downloadAllFabCoordinator!!.visibility = GONE
|
|
||||||
downloadFabs!!.visibility = GONE
|
|
||||||
}
|
|
||||||
R.id.delete_search -> {
|
|
||||||
resultViewModel.deleteAllSearchQueryHistory()
|
|
||||||
searchSuggestionsLinearLayout!!.removeAllViews()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
true
|
|
||||||
}
|
|
||||||
queriesChipGroup!!.addOnLayoutChangeListener { view, i, i2, i3, i4, i5, i6, i7, i8 ->
|
|
||||||
if (queriesChipGroup!!.childCount == 0) queriesConstraint.visibility = GONE
|
|
||||||
else queriesConstraint.visibility = VISIBLE
|
|
||||||
searchView.editText.setText("")
|
|
||||||
}
|
|
||||||
|
|
||||||
queriesInitStartBtn.setOnClickListener {
|
|
||||||
initSearch(searchView)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initSearch(searchView: SearchView){
|
private fun initSearch(searchView: SearchView){
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue