Skip to content

Commit

Permalink
Make search queries removable
Browse files Browse the repository at this point in the history
  • Loading branch information
cemrich committed Dec 23, 2023
1 parent 43e9ffe commit b1342dc
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package de.christinecoenen.code.zapp.app.search

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.PopupMenu
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.ConcatAdapter
import de.christinecoenen.code.zapp.R
import de.christinecoenen.code.zapp.app.search.suggestions.text.LocalSearchSuggestionsAdapter
import de.christinecoenen.code.zapp.app.search.suggestions.chips.ChannelChipContent
import de.christinecoenen.code.zapp.app.search.suggestions.chips.ChipType
import de.christinecoenen.code.zapp.app.search.suggestions.chips.ChipsAdapter
import de.christinecoenen.code.zapp.app.search.suggestions.chips.SuggestionChipListener
import de.christinecoenen.code.zapp.app.search.suggestions.text.LocalSearchSuggestionsAdapter
import de.christinecoenen.code.zapp.app.search.suggestions.text.SuggestionTextListener
import de.christinecoenen.code.zapp.app.search.suggestions.text.TextSuggestionType
import de.christinecoenen.code.zapp.databinding.SearchFragmentBinding
import de.christinecoenen.code.zapp.utils.system.LifecycleOwnerHelper.launchOnCreated
import kotlinx.coroutines.flow.collectLatest
Expand Down Expand Up @@ -74,9 +78,9 @@ class SearchFragment : Fragment(), SuggestionTextListener {

// suggestions
val lastQueriesAdapter =
LocalSearchSuggestionsAdapter(this, R.drawable.ic_history_24)
LocalSearchSuggestionsAdapter(this, TextSuggestionType.RecentQuery)
val localSuggestionsAdapter =
LocalSearchSuggestionsAdapter(this, R.drawable.ic_baseline_search_24)
LocalSearchSuggestionsAdapter(this, TextSuggestionType.Visited)
binding.suggestions.adapter = ConcatAdapter(lastQueriesAdapter, localSuggestionsAdapter)

viewLifecycleOwner.launchOnCreated {
Expand Down Expand Up @@ -105,4 +109,34 @@ class SearchFragment : Fragment(), SuggestionTextListener {
override fun onSuggestionInserted(suggestion: String) {
viewModel.setSearchQuery(suggestion)
}

override fun onSuggestionLongPress(
suggestion: String,
type: TextSuggestionType,
view: View
): Boolean {
if (type != TextSuggestionType.RecentQuery) {
return false
}

val menu = PopupMenu(context, view, Gravity.TOP or Gravity.END)
menu.inflate(R.menu.search_recent_query_context)
menu.show()
menu.setOnMenuItemClickListener { menuItem ->
onSuggesionContextMenuClicked(menuItem, suggestion)
}

return true
}

private fun onSuggesionContextMenuClicked(menuItem: MenuItem, suggestion: String): Boolean {
return when (menuItem.itemId) {
R.id.menu_delete -> {
viewModel.deleteSavedSearchQuery(suggestion)
true
}

else -> false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ class SearchViewModel(
}
}

fun deleteSavedSearchQuery(searchQuery: String) {
viewModelScope.launch {
searchRepository.deleteQuery(searchQuery)
}
}

fun enterQueryMode() {
_searchState.tryEmit(SeachState.Query)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package de.christinecoenen.code.zapp.app.search.suggestions.text

import androidx.annotation.DrawableRes
import androidx.recyclerview.widget.RecyclerView
import de.christinecoenen.code.zapp.databinding.SearchSuggestionItemLocalBinding

class LocalSearchSuggestionViewHolder(
private val binding: SearchSuggestionItemLocalBinding,
@DrawableRes
private val typeIcon: Int,
private val type: TextSuggestionType,
) : RecyclerView.ViewHolder(binding.root) {

fun setSuggestion(suggestion: String) {
binding.typeIcon.setImageResource(typeIcon)
binding.typeIcon.setImageResource(type.iconResId)
binding.suggestion.text = suggestion
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package de.christinecoenen.code.zapp.app.search.suggestions.text

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.DrawableRes
import androidx.paging.PagingDataAdapter
import de.christinecoenen.code.zapp.app.search.StringComparator
import de.christinecoenen.code.zapp.databinding.SearchSuggestionItemLocalBinding

class LocalSearchSuggestionsAdapter(
private val listener: SuggestionTextListener,
@DrawableRes
private val typeIcon: Int,
private val type: TextSuggestionType,
) :
PagingDataAdapter<String, LocalSearchSuggestionViewHolder>(StringComparator) {
override fun onBindViewHolder(holder: LocalSearchSuggestionViewHolder, position: Int) {
Expand All @@ -23,11 +21,18 @@ class LocalSearchSuggestionsAdapter(
): LocalSearchSuggestionViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = SearchSuggestionItemLocalBinding.inflate(layoutInflater, parent, false)
val holder = LocalSearchSuggestionViewHolder(binding, typeIcon)
val holder = LocalSearchSuggestionViewHolder(binding, type)

binding.root.setOnClickListener {
listener.onSuggestionSelected(getItem(holder.bindingAdapterPosition)!!)
}
binding.root.setOnLongClickListener {
listener.onSuggestionLongPress(
getItem(holder.bindingAdapterPosition)!!,
type,
binding.root
)
}
binding.insertButton.setOnClickListener {
listener.onSuggestionInserted(getItem(holder.bindingAdapterPosition)!!)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package de.christinecoenen.code.zapp.app.search.suggestions.text

import android.view.View

interface SuggestionTextListener {
fun onSuggestionSelected(suggestion: String)
fun onSuggestionInserted(suggestion: String)
fun onSuggestionSelected(suggestion: String)
fun onSuggestionInserted(suggestion: String)
fun onSuggestionLongPress(suggestion: String, type: TextSuggestionType, view: View): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package de.christinecoenen.code.zapp.app.search.suggestions.text

import androidx.annotation.DrawableRes
import de.christinecoenen.code.zapp.R

enum class TextSuggestionType(@DrawableRes val iconResId: Int) {
RecentQuery(R.drawable.ic_history_24),
Visited(R.drawable.ic_baseline_search_24),
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ interface SearchDao {
@Query("SELECT topic FROM PersistedMediathekShow WHERE topic LIKE :searchQuery UNION SELECT title FROM PersistedMediathekShow WHERE title LIKE :searchQuery")
fun getLocalSearchSuggestions(searchQuery: String): PagingSource<Int, String>

@Query("SELECT query FROM SearchQuery WHERE query LIKE :searchQuery ORDER BY date DESC LIMIT :limit")
@Query("SELECT `query` FROM SearchQuery WHERE `query` LIKE :searchQuery ORDER BY date DESC LIMIT :limit")
fun getQueries(searchQuery: String, limit: Int): PagingSource<Int, String>

@Query("DELETE FROM SearchQuery WHERE `query` = :query")
suspend fun deleteQuery(query: String)

@Upsert
suspend fun saveQuery(vararg query: SearchQuery)

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ class SearchRepository(private val database: Database) {
.searchDao()
.saveQuery(SearchQuery(query, DateTime.now()))
}

suspend fun deleteQuery(query: String) = withContext(Dispatchers.IO) {
database
.searchDao()
.deleteQuery(query)
}
}
11 changes: 11 additions & 0 deletions app/src/main/res/menu/search_recent_query_context.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/menu_delete"
android:icon="@drawable/ic_baseline_delete_outline_24"
android:title="@string/menu_delete_search_query_from_history"
app:showAsAction="ifRoom" />

</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<string name="menu_filter">Filter</string>
<string name="menu_program_info">Programm-Informationen</string>
<string name="menu_voice_search">Sprachsuche</string>
<string name="menu_delete_search_query_from_history">Aus Suchverlauf löschen</string>

<!-- player -->
<string name="player_zoom_state_cropped">Gezoomt</string>
Expand Down

0 comments on commit b1342dc

Please sign in to comment.