Add genre support to search
Add the ability for genres to show up in search, as long as the library is currently displaying them.
This commit is contained in:
@@ -6,6 +6,7 @@ import android.view.LayoutInflater
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ImageView
|
||||
import androidx.appcompat.widget.SearchView
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.view.forEach
|
||||
@@ -58,8 +59,9 @@ class LibraryFragment : Fragment(), SearchView.OnQueryTextListener {
|
||||
val item = findItem(R.id.action_search)
|
||||
val searchView = item.actionView as SearchView
|
||||
|
||||
// Set up the SearchView itself
|
||||
searchView.queryHint = getString(R.string.hint_search_library)
|
||||
searchView.findViewById<ImageView>(androidx.appcompat.R.id.search_go_btn)
|
||||
.setImageResource(R.drawable.ic_back)
|
||||
|
||||
searchView.setOnQueryTextListener(this@LibraryFragment)
|
||||
searchView.setOnQueryTextFocusChangeListener { _, hasFocus ->
|
||||
libraryModel.updateSearchFocusStatus(hasFocus)
|
||||
@@ -139,7 +141,9 @@ class LibraryFragment : Fragment(), SearchView.OnQueryTextListener {
|
||||
|
||||
libraryModel.searchResults.observe(viewLifecycleOwner) {
|
||||
if (libraryModel.searchHasFocus) {
|
||||
searchAdapter.submitList(it)
|
||||
searchAdapter.submitList(it) {
|
||||
binding.libraryRecycler.scrollToPosition(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@ class LibraryViewModel : ViewModel() {
|
||||
}
|
||||
|
||||
fun updateSearchQuery(query: String, musicModel: MusicViewModel) {
|
||||
// Don't bother if the query is blank.
|
||||
if (query == "") {
|
||||
resetQuery()
|
||||
|
||||
@@ -52,18 +53,32 @@ class LibraryViewModel : ViewModel() {
|
||||
}
|
||||
|
||||
// Search MusicViewModel for all the items [Artists, Albums, Songs] that contain
|
||||
// the query, and update the LiveData with those items. This is done on a seperate
|
||||
// thread as it can be a very intensive operation for large music libraries.
|
||||
// the query, and update the LiveData with those items. This is done on a separate
|
||||
// thread as it can be a very long operation for large music libraries.
|
||||
viewModelScope.launch {
|
||||
val combined = mutableListOf<BaseModel>()
|
||||
val children = showMode.value!!.getChildren()
|
||||
|
||||
val artists = musicModel.artists.value!!.filter { it.name.contains(query, true) }
|
||||
// If the Library ShowMode supports it, include artists / genres in the search.
|
||||
if (children.contains(ShowMode.SHOW_GENRES)) {
|
||||
val genres = musicModel.genres.value!!.filter { it.name.contains(query, true) }
|
||||
|
||||
if (artists.isNotEmpty()) {
|
||||
combined.add(Header(id = ShowMode.SHOW_ARTISTS.constant))
|
||||
combined.addAll(artists)
|
||||
if (genres.isNotEmpty()) {
|
||||
combined.add(Header(id = ShowMode.SHOW_GENRES.constant))
|
||||
combined.addAll(genres)
|
||||
}
|
||||
}
|
||||
|
||||
if (children.contains(ShowMode.SHOW_ARTISTS)) {
|
||||
val artists = musicModel.artists.value!!.filter { it.name.contains(query, true) }
|
||||
|
||||
if (artists.isNotEmpty()) {
|
||||
combined.add(Header(id = ShowMode.SHOW_ARTISTS.constant))
|
||||
combined.addAll(artists)
|
||||
}
|
||||
}
|
||||
|
||||
// Albums & Songs are always included.
|
||||
val albums = musicModel.albums.value!!.filter { it.name.contains(query, true) }
|
||||
|
||||
if (albums.isNotEmpty()) {
|
||||
|
||||
@@ -97,7 +97,7 @@ fun TextView.bindGenreCounts(genre: Genre) {
|
||||
R.plurals.format_artist_count, genre.numArtists, genre.numArtists
|
||||
)
|
||||
val albums = context.resources.getQuantityString(
|
||||
R.plurals.format_song_count, genre.numAlbums, genre.numAlbums
|
||||
R.plurals.format_album_count, genre.numAlbums, genre.numAlbums
|
||||
)
|
||||
|
||||
text = context.getString(R.string.format_double_counts, artists, albums)
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
package org.oxycblt.auxio.recycler
|
||||
|
||||
enum class ShowMode(val constant: Long) {
|
||||
SHOW_GENRES(10), SHOW_ARTISTS(11), SHOW_ALBUMS(12), SHOW_SONGS(13);
|
||||
SHOW_GENRES(0), SHOW_ARTISTS(1), SHOW_ALBUMS(2), SHOW_SONGS(3);
|
||||
|
||||
// Make a slice of all the values that this ShowMode covers.
|
||||
// ex. SHOW_ARTISTS would return SHOW_ARTISTS, SHOW_ALBUMS, and SHOW_SONGS
|
||||
fun getChildren(): List<ShowMode> {
|
||||
val vals = values()
|
||||
|
||||
return vals.slice(vals.indexOf(this) until vals.size)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,8 +14,8 @@ import org.oxycblt.auxio.music.Header
|
||||
import org.oxycblt.auxio.music.Song
|
||||
import org.oxycblt.auxio.recycler.ClickListener
|
||||
|
||||
// Basic ViewHolders for each music model.
|
||||
|
||||
// Shared ViewHolders for each ViewModel, providing basic information
|
||||
// All new instances should be created with from(context, listener) instead of direct instantiation.
|
||||
class GenreViewHolder private constructor(
|
||||
listener: ClickListener<Genre>,
|
||||
private val binding: ItemGenreBinding
|
||||
|
||||
@@ -65,6 +65,7 @@ fun Int.toColor(context: Context): Int {
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve an attribute into a color
|
||||
@ColorInt
|
||||
fun resolveAttr(context: Context, @AttrRes attr: Int): Int {
|
||||
// Convert the attribute into its color
|
||||
@@ -82,6 +83,7 @@ fun resolveAttr(context: Context, @AttrRes attr: Int): Int {
|
||||
return color.toColor(context)
|
||||
}
|
||||
|
||||
// Apply a color to a Menu Item
|
||||
fun MenuItem.applyColor(color: Int) {
|
||||
SpannableString(title).apply {
|
||||
setSpan(ForegroundColorSpan(color), 0, length, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
|
||||
Reference in New Issue
Block a user