Add search to LibraryFragment
Add searching to LibraryFragment.
This commit is contained in:
@@ -13,7 +13,6 @@ import androidx.navigation.fragment.navArgs
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.FragmentArtistDetailBinding
|
||||
import org.oxycblt.auxio.detail.adapters.DetailAlbumAdapter
|
||||
import org.oxycblt.auxio.music.Album
|
||||
import org.oxycblt.auxio.music.MusicViewModel
|
||||
import org.oxycblt.auxio.recycler.ClickListener
|
||||
import org.oxycblt.auxio.theme.applyDivider
|
||||
|
||||
@@ -13,7 +13,6 @@ import androidx.navigation.fragment.navArgs
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.FragmentGenreDetailBinding
|
||||
import org.oxycblt.auxio.detail.adapters.DetailArtistAdapter
|
||||
import org.oxycblt.auxio.music.Artist
|
||||
import org.oxycblt.auxio.music.MusicViewModel
|
||||
import org.oxycblt.auxio.recycler.ClickListener
|
||||
import org.oxycblt.auxio.theme.applyDivider
|
||||
|
||||
@@ -22,7 +22,7 @@ class LibraryAdapter(
|
||||
private val listener: ClickListener<BaseModel>
|
||||
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
||||
|
||||
private var data: List<BaseModel>
|
||||
var data: List<BaseModel>
|
||||
|
||||
init {
|
||||
// Assign the data on startup depending on the type
|
||||
|
||||
@@ -5,11 +5,14 @@ import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.appcompat.widget.SearchView
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.view.forEach
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import androidx.transition.Fade
|
||||
import androidx.transition.TransitionManager
|
||||
import org.oxycblt.auxio.MainFragmentDirections
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.FragmentLibraryBinding
|
||||
@@ -26,7 +29,7 @@ import org.oxycblt.auxio.theme.applyColor
|
||||
import org.oxycblt.auxio.theme.applyDivider
|
||||
import org.oxycblt.auxio.theme.resolveAttr
|
||||
|
||||
class LibraryFragment : Fragment() {
|
||||
class LibraryFragment : Fragment(), SearchView.OnQueryTextListener {
|
||||
|
||||
private val musicModel: MusicViewModel by activityViewModels()
|
||||
private val libraryModel: LibraryViewModel by activityViewModels()
|
||||
@@ -38,12 +41,52 @@ class LibraryFragment : Fragment() {
|
||||
): View? {
|
||||
val binding = FragmentLibraryBinding.inflate(inflater)
|
||||
|
||||
val artistAdapter = LibraryAdapter(
|
||||
// Toolbar setup
|
||||
binding.libraryToolbar.overflowIcon = ContextCompat.getDrawable(
|
||||
requireContext(), R.drawable.ic_sort_none
|
||||
)
|
||||
|
||||
binding.libraryToolbar.menu.apply {
|
||||
val item = findItem(R.id.action_search)
|
||||
val searchView = item.actionView as SearchView
|
||||
|
||||
searchView.queryHint = getString(R.string.hint_search_library)
|
||||
searchView.setOnQueryTextListener(this@LibraryFragment)
|
||||
searchView.setOnQueryTextFocusChangeListener { _, hasFocus ->
|
||||
this.setGroupVisible(R.id.group_sorting, !hasFocus)
|
||||
|
||||
// Make sure the search item will still be visible, and then do an animation
|
||||
item.isVisible = !hasFocus
|
||||
TransitionManager.beginDelayedTransition(
|
||||
binding.libraryToolbar, Fade()
|
||||
)
|
||||
item.collapseActionView()
|
||||
}
|
||||
}
|
||||
|
||||
binding.libraryToolbar.setOnMenuItemClickListener {
|
||||
if (it.itemId != R.id.action_search) {
|
||||
libraryModel.updateSortMode(it)
|
||||
} else {
|
||||
// Do whatever this is in order to make the SearchView focusable.
|
||||
(it.actionView as SearchView).isIconified = false
|
||||
|
||||
// Then also do a basic animation
|
||||
TransitionManager.beginDelayedTransition(
|
||||
binding.libraryToolbar, Fade()
|
||||
)
|
||||
it.expandActionView()
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
val libraryAdapter = LibraryAdapter(
|
||||
libraryModel.showMode.value!!,
|
||||
ClickListener { navToItem(it) }
|
||||
)
|
||||
|
||||
binding.libraryRecycler.adapter = artistAdapter
|
||||
// RecyclerView setup
|
||||
binding.libraryRecycler.adapter = libraryAdapter
|
||||
binding.libraryRecycler.applyDivider()
|
||||
binding.libraryRecycler.setHasFixedSize(true)
|
||||
|
||||
@@ -51,7 +94,7 @@ class LibraryFragment : Fragment() {
|
||||
Log.d(this::class.simpleName, "Updating sort mode to $mode")
|
||||
|
||||
// Update the adapter with the new data
|
||||
artistAdapter.updateData(
|
||||
libraryAdapter.updateData(
|
||||
when (libraryModel.showMode.value) {
|
||||
SHOW_GENRES -> mode.getSortedGenreList(musicModel.genres.value!!)
|
||||
SHOW_ARTISTS -> mode.getSortedArtistList(musicModel.artists.value!!)
|
||||
@@ -61,12 +104,7 @@ class LibraryFragment : Fragment() {
|
||||
}
|
||||
)
|
||||
|
||||
// Then update the shown icon & the currently highlighted sort icon to reflect
|
||||
// the new sorting mode.
|
||||
binding.libraryToolbar.overflowIcon = ContextCompat.getDrawable(
|
||||
requireContext(), mode.iconRes
|
||||
)
|
||||
|
||||
// Then update the menu item in the toolbar to reflect the new mode
|
||||
binding.libraryToolbar.menu.forEach {
|
||||
if (it.itemId == libraryModel.sortMode.value!!.toMenuId()) {
|
||||
it.applyColor(resolveAttr(requireContext(), R.attr.colorPrimary))
|
||||
@@ -76,10 +114,17 @@ class LibraryFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
binding.libraryToolbar.setOnMenuItemClickListener {
|
||||
libraryModel.updateSortMode(it)
|
||||
libraryModel.searchQuery.observe(viewLifecycleOwner) { query ->
|
||||
// Update the adapter with the new data
|
||||
libraryAdapter.updateData(
|
||||
when (libraryModel.showMode.value) {
|
||||
SHOW_GENRES -> musicModel.genres.value!!
|
||||
SHOW_ARTISTS -> musicModel.artists.value!!
|
||||
SHOW_ALBUMS -> musicModel.albums.value!!
|
||||
|
||||
true
|
||||
else -> musicModel.artists.value!!
|
||||
}.filter { it.name.contains(query, true) }
|
||||
)
|
||||
}
|
||||
|
||||
Log.d(this::class.simpleName, "Fragment created.")
|
||||
@@ -87,6 +132,14 @@ class LibraryFragment : Fragment() {
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onQueryTextSubmit(query: String): Boolean = false
|
||||
|
||||
override fun onQueryTextChange(query: String): Boolean {
|
||||
libraryModel.updateSearchQuery(query)
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
private fun navToItem(baseModel: BaseModel) {
|
||||
Log.d(this::class.simpleName, "Navigating to the detail fragment for ${baseModel.name}")
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.recycler.SortMode
|
||||
import org.oxycblt.auxio.theme.SHOW_ARTISTS
|
||||
|
||||
class LibraryViewModel : ViewModel() {
|
||||
class LibraryViewModel() : ViewModel() {
|
||||
// TODO: Move these to prefs when they're added
|
||||
private val mShowMode = MutableLiveData(SHOW_ARTISTS)
|
||||
val showMode: LiveData<Int> get() = mShowMode
|
||||
@@ -16,11 +16,14 @@ class LibraryViewModel : ViewModel() {
|
||||
private val mSortMode = MutableLiveData(SortMode.ALPHA_DOWN)
|
||||
val sortMode: LiveData<SortMode> get() = mSortMode
|
||||
|
||||
private val mSearchQuery = MutableLiveData("")
|
||||
val searchQuery: LiveData<String> get() = mSearchQuery
|
||||
|
||||
fun updateSortMode(item: MenuItem) {
|
||||
val mode = when (item.itemId) {
|
||||
R.id.sort_none -> SortMode.NONE
|
||||
R.id.sort_alpha_down -> SortMode.ALPHA_DOWN
|
||||
R.id.sort_alpha_up -> SortMode.ALPHA_UP
|
||||
R.id.option_sort_none -> SortMode.NONE
|
||||
R.id.option_sort_alpha_down -> SortMode.ALPHA_DOWN
|
||||
R.id.option_sort_alpha_up -> SortMode.ALPHA_UP
|
||||
|
||||
else -> SortMode.NONE
|
||||
}
|
||||
@@ -29,4 +32,8 @@ class LibraryViewModel : ViewModel() {
|
||||
mSortMode.value = mode
|
||||
}
|
||||
}
|
||||
|
||||
fun updateSearchQuery(query: String) {
|
||||
mSearchQuery.value = query
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.oxycblt.auxio.recycler
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.music.Album
|
||||
import org.oxycblt.auxio.music.Artist
|
||||
import org.oxycblt.auxio.music.BaseModel
|
||||
import org.oxycblt.auxio.music.Genre
|
||||
import org.oxycblt.auxio.music.Song
|
||||
|
||||
@@ -77,13 +78,30 @@ enum class SortMode(val iconRes: Int) {
|
||||
}
|
||||
}
|
||||
|
||||
fun getSortedBaseModelList(list: List<BaseModel>): List<BaseModel> {
|
||||
return when (this) {
|
||||
ALPHA_UP -> list.sortedWith(
|
||||
compareByDescending(String.CASE_INSENSITIVE_ORDER) { it.name }
|
||||
)
|
||||
|
||||
ALPHA_DOWN -> list.sortedWith(
|
||||
compareBy(String.CASE_INSENSITIVE_ORDER) { it.name }
|
||||
)
|
||||
|
||||
NUMERIC_UP -> list.sortedWith(compareByDescending { it.id })
|
||||
NUMERIC_DOWN -> list.sortedWith(compareBy { it.id })
|
||||
|
||||
else -> list
|
||||
}
|
||||
}
|
||||
|
||||
fun toMenuId(): Int {
|
||||
return when (this) {
|
||||
NONE -> R.id.sort_none
|
||||
ALPHA_UP -> R.id.sort_alpha_up
|
||||
ALPHA_DOWN -> R.id.sort_alpha_down
|
||||
NONE -> R.id.option_sort_none
|
||||
ALPHA_UP -> R.id.option_sort_alpha_up
|
||||
ALPHA_DOWN -> R.id.option_sort_alpha_down
|
||||
|
||||
else -> R.id.sort_none
|
||||
else -> R.id.option_sort_none
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user