search: implement chip pattern prototype
very rough right now, settings are not final at all
This commit is contained in:
@@ -63,6 +63,7 @@ import org.oxycblt.auxio.music.PlaylistDecision
|
||||
import org.oxycblt.auxio.music.PlaylistMessage
|
||||
import org.oxycblt.auxio.playback.PlaybackDecision
|
||||
import org.oxycblt.auxio.playback.PlaybackViewModel
|
||||
import org.oxycblt.auxio.ui.FadingToolbarOffsetListener
|
||||
import org.oxycblt.auxio.util.collect
|
||||
import org.oxycblt.auxio.util.collectImmediately
|
||||
import org.oxycblt.auxio.util.lazyReflectedField
|
||||
@@ -82,7 +83,7 @@ import timber.log.Timber as L
|
||||
*/
|
||||
@AndroidEntryPoint
|
||||
class HomeFragment :
|
||||
SelectionFragment<FragmentHomeBinding>(), AppBarLayout.OnOffsetChangedListener {
|
||||
SelectionFragment<FragmentHomeBinding>() {
|
||||
override val listModel: ListViewModel by activityViewModels()
|
||||
override val musicModel: MusicViewModel by activityViewModels()
|
||||
override val playbackModel: PlaybackViewModel by activityViewModels()
|
||||
@@ -128,7 +129,12 @@ class HomeFragment :
|
||||
|
||||
// --- UI SETUP ---
|
||||
|
||||
binding.homeAppbar.addOnOffsetChangedListener(this)
|
||||
binding.homeAppbar.addOnOffsetChangedListener(
|
||||
FadingToolbarOffsetListener(
|
||||
binding.homeToolbar,
|
||||
binding.homeContent
|
||||
)
|
||||
)
|
||||
binding.homeNormalToolbar.apply {
|
||||
setOnMenuItemClickListener(this@HomeFragment)
|
||||
MenuCompat.setGroupDividerEnabled(menu, true)
|
||||
@@ -187,22 +193,9 @@ class HomeFragment :
|
||||
override fun onDestroyBinding(binding: FragmentHomeBinding) {
|
||||
super.onDestroyBinding(binding)
|
||||
storagePermissionLauncher = null
|
||||
binding.homeAppbar.removeOnOffsetChangedListener(this)
|
||||
binding.homeNormalToolbar.setOnMenuItemClickListener(null)
|
||||
}
|
||||
|
||||
override fun onOffsetChanged(appBarLayout: AppBarLayout, verticalOffset: Int) {
|
||||
val binding = requireBinding()
|
||||
val range = appBarLayout.totalScrollRange
|
||||
// Fade out the toolbar as the AppBarLayout collapses. To prevent status bar overlap,
|
||||
// the alpha transition is shifted such that the Toolbar becomes fully transparent
|
||||
// when the AppBarLayout is only at half-collapsed.
|
||||
binding.homeToolbar.alpha = 1f - (abs(verticalOffset.toFloat()) / (range.toFloat() / 2))
|
||||
binding.homeContent.updatePadding(
|
||||
bottom = binding.homeAppbar.totalScrollRange + verticalOffset
|
||||
)
|
||||
}
|
||||
|
||||
override fun onMenuItemClick(item: MenuItem): Boolean {
|
||||
if (super.onMenuItemClick(item)) {
|
||||
return true
|
||||
|
||||
@@ -44,11 +44,13 @@ import org.oxycblt.auxio.list.ListViewModel
|
||||
import org.oxycblt.auxio.list.PlainDivider
|
||||
import org.oxycblt.auxio.list.PlainHeader
|
||||
import org.oxycblt.auxio.list.menu.Menu
|
||||
import org.oxycblt.auxio.music.MusicType
|
||||
import org.oxycblt.auxio.music.MusicViewModel
|
||||
import org.oxycblt.auxio.music.PlaylistDecision
|
||||
import org.oxycblt.auxio.music.PlaylistMessage
|
||||
import org.oxycblt.auxio.playback.PlaybackDecision
|
||||
import org.oxycblt.auxio.playback.PlaybackViewModel
|
||||
import org.oxycblt.auxio.ui.FadingToolbarOffsetListener
|
||||
import org.oxycblt.auxio.util.collect
|
||||
import org.oxycblt.auxio.util.collectImmediately
|
||||
import org.oxycblt.auxio.util.context
|
||||
@@ -72,7 +74,6 @@ import timber.log.Timber as L
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*
|
||||
* TODO: Better keyboard management
|
||||
* TODO: Multi-filtering with chips
|
||||
*/
|
||||
@AndroidEntryPoint
|
||||
class SearchFragment : ListFragment<Music, FragmentSearchBinding>() {
|
||||
@@ -118,10 +119,15 @@ class SearchFragment : ListFragment<Music, FragmentSearchBinding>() {
|
||||
|
||||
// --- UI SETUP ---
|
||||
|
||||
binding.searchAppbar.addOnOffsetChangedListener(
|
||||
FadingToolbarOffsetListener(
|
||||
binding.searchToolbar,
|
||||
binding.searchContent
|
||||
)
|
||||
)
|
||||
|
||||
binding.searchNormalToolbar.apply {
|
||||
// Initialize the current filtering mode.
|
||||
menu.findItem(searchModel.getFilterOptionId()).isChecked = true
|
||||
|
||||
setNavigationOnClickListener {
|
||||
// Keyboard is no longer needed.
|
||||
hideKeyboard()
|
||||
@@ -145,6 +151,28 @@ class SearchFragment : ListFragment<Music, FragmentSearchBinding>() {
|
||||
}
|
||||
}
|
||||
|
||||
val filters = searchModel.filters
|
||||
binding.searchFilterSongs.isChecked = MusicType.SONGS in filters
|
||||
binding.searchFilterAlbums.isChecked = MusicType.ALBUMS in filters
|
||||
binding.searchFilterArtists.isChecked = MusicType.ARTISTS in filters
|
||||
binding.searchFilterGenres.isChecked = MusicType.GENRES in filters
|
||||
binding.searchFilterPlaylists.isChecked = MusicType.PLAYLISTS in filters
|
||||
binding.searchFilters.apply {
|
||||
setOnCheckedStateChangeListener { group, ids ->
|
||||
val types = ids.mapNotNullTo(mutableSetOf()) {
|
||||
when (it) {
|
||||
R.id.search_filter_songs -> MusicType.SONGS
|
||||
R.id.search_filter_albums -> MusicType.ALBUMS
|
||||
R.id.search_filter_artists -> MusicType.ARTISTS
|
||||
R.id.search_filter_genres -> MusicType.GENRES
|
||||
R.id.search_filter_playlists -> MusicType.PLAYLISTS
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
searchModel.filters = types
|
||||
}
|
||||
}
|
||||
|
||||
binding.searchRecycler.apply {
|
||||
adapter = searchAdapter
|
||||
(layoutManager as GridLayoutManager).setFullWidthLookup {
|
||||
@@ -179,24 +207,6 @@ class SearchFragment : ListFragment<Music, FragmentSearchBinding>() {
|
||||
binding.searchRecycler.adapter = null
|
||||
}
|
||||
|
||||
override fun onMenuItemClick(item: MenuItem): Boolean {
|
||||
if (super.onMenuItemClick(item)) {
|
||||
return true
|
||||
}
|
||||
|
||||
// Ignore junk sub-menu click events
|
||||
if (item.itemId != R.id.submenu_filtering) {
|
||||
// Is a change in filter mode and not just a junk submenu click, update
|
||||
// the filtering within SearchViewModel.
|
||||
L.d("Filter mode selected")
|
||||
item.isChecked = true
|
||||
searchModel.setFilterOptionId(item.itemId)
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
override fun onRealClick(item: Music) {
|
||||
when (item) {
|
||||
is Song -> playbackModel.play(item, searchModel.playWith)
|
||||
@@ -228,6 +238,12 @@ class SearchFragment : ListFragment<Music, FragmentSearchBinding>() {
|
||||
// that doesn't seem possible.
|
||||
L.d("Update finished, scrolling to top")
|
||||
binding.searchRecycler.scrollToPosition(0)
|
||||
if (results.isEmpty()) {
|
||||
// Expand the appbar when we no longer have results.
|
||||
// This way the user can't softlock themselves by scrolling then
|
||||
// switching to an empty-result query.
|
||||
binding.searchAppbar.expandWithScrollingRecycler()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,9 +315,11 @@ class SearchFragment : ListFragment<Music, FragmentSearchBinding>() {
|
||||
if (selected.isNotEmpty()) {
|
||||
binding.searchSelectionToolbar.title = getString(R.string.fmt_selected, selected.size)
|
||||
if (binding.searchToolbar.setVisible(R.id.search_selection_toolbar)) {
|
||||
// New selection started, show the keyboard to make selection easier.
|
||||
L.d("Significant selection occurred, hiding keyboard")
|
||||
// New selection started, show the keyboard to make selection easier and
|
||||
// restore the appbar as well
|
||||
L.d("Significant selection occurred, hiding keyboard & expanding AppBar")
|
||||
hideKeyboard()
|
||||
binding.searchAppbar.expandWithScrollingRecycler()
|
||||
}
|
||||
} else {
|
||||
binding.searchToolbar.setVisible(R.id.search_normal_toolbar)
|
||||
|
||||
@@ -32,27 +32,26 @@ import org.oxycblt.auxio.settings.Settings
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
interface SearchSettings : Settings<Nothing> {
|
||||
/** The type of Music the search view is should filter to. */
|
||||
var filterTo: MusicType?
|
||||
/** The type of Music the search view should filter to. */
|
||||
var filters: Set<MusicType>
|
||||
}
|
||||
|
||||
class SearchSettingsImpl @Inject constructor(@ApplicationContext context: Context) :
|
||||
Settings.Impl<Nothing>(context), SearchSettings {
|
||||
override var filterTo: MusicType?
|
||||
override var filters: Set<MusicType>
|
||||
get() =
|
||||
MusicType.fromIntCode(
|
||||
sharedPreferences.getInt(
|
||||
getString(R.string.set_key_search_filter_to),
|
||||
Int.MIN_VALUE,
|
||||
)
|
||||
)
|
||||
sharedPreferences.getStringSet(
|
||||
getString(R.string.set_key_search_filters),
|
||||
null
|
||||
)?.mapNotNull {
|
||||
it.toIntOrNull()?.let(MusicType::fromIntCode)
|
||||
}?.toSet() ?: setOf()
|
||||
set(value) {
|
||||
sharedPreferences.edit {
|
||||
putInt(
|
||||
getString(R.string.set_key_search_filter_to),
|
||||
value?.intCode ?: Int.MIN_VALUE,
|
||||
putStringSet(
|
||||
getString(R.string.set_key_search_filters),
|
||||
value.map { it.intCode.toString() }.toSet()
|
||||
)
|
||||
apply()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import androidx.annotation.IdRes
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
@@ -34,11 +33,13 @@ import org.oxycblt.auxio.list.Item
|
||||
import org.oxycblt.auxio.list.PlainDivider
|
||||
import org.oxycblt.auxio.list.sort.Sort
|
||||
import org.oxycblt.auxio.music.MusicRepository
|
||||
import org.oxycblt.auxio.music.MusicSettings
|
||||
import org.oxycblt.auxio.music.MusicType
|
||||
import org.oxycblt.auxio.playback.PlaySong
|
||||
import org.oxycblt.auxio.playback.PlaybackSettings
|
||||
import org.oxycblt.musikr.Library
|
||||
import org.oxycblt.musikr.Song
|
||||
import javax.inject.Inject
|
||||
import timber.log.Timber as L
|
||||
|
||||
/**
|
||||
@@ -110,29 +111,25 @@ constructor(
|
||||
}
|
||||
|
||||
private suspend fun searchImpl(library: Library, query: String): List<Item> {
|
||||
val filter = searchSettings.filterTo
|
||||
val filters = searchSettings.filters
|
||||
|
||||
val items =
|
||||
if (filter == null) {
|
||||
// A nulled filter type means to not filter anything.
|
||||
L.d("No filter specified, using entire library")
|
||||
SearchEngine.Items(
|
||||
library.songs,
|
||||
library.albums,
|
||||
library.artists,
|
||||
library.genres,
|
||||
library.playlists,
|
||||
)
|
||||
} else {
|
||||
L.d("Filter specified, reducing library")
|
||||
SearchEngine.Items(
|
||||
songs = if (filter == MusicType.SONGS) library.songs else null,
|
||||
albums = if (filter == MusicType.ALBUMS) library.albums else null,
|
||||
artists = if (filter == MusicType.ARTISTS) library.artists else null,
|
||||
genres = if (filter == MusicType.GENRES) library.genres else null,
|
||||
playlists = if (filter == MusicType.PLAYLISTS) library.playlists else null,
|
||||
)
|
||||
}
|
||||
val items = if (!filters.isEmpty()) {
|
||||
SearchEngine.Items(
|
||||
songs = if (MusicType.SONGS in filters) library.songs else null,
|
||||
albums = if (MusicType.ALBUMS in filters) library.albums else null,
|
||||
artists = if (MusicType.ARTISTS in filters) library.artists else null,
|
||||
genres = if (MusicType.GENRES in filters) library.genres else null,
|
||||
playlists = if (MusicType.PLAYLISTS in filters) library.playlists else null,
|
||||
)
|
||||
} else {
|
||||
SearchEngine.Items(
|
||||
songs = library.songs,
|
||||
albums = library.albums,
|
||||
artists = library.artists,
|
||||
genres = library.genres,
|
||||
playlists = library.playlists
|
||||
)
|
||||
}
|
||||
|
||||
val results = searchEngine.search(items, query)
|
||||
|
||||
@@ -186,45 +183,14 @@ constructor(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ID of the filter option to currently highlight.
|
||||
*
|
||||
* @return A menu item ID of the filtering option selected.
|
||||
*/
|
||||
@IdRes
|
||||
fun getFilterOptionId() =
|
||||
when (searchSettings.filterTo) {
|
||||
MusicType.SONGS -> R.id.option_filter_songs
|
||||
MusicType.ALBUMS -> R.id.option_filter_albums
|
||||
MusicType.ARTISTS -> R.id.option_filter_artists
|
||||
MusicType.GENRES -> R.id.option_filter_genres
|
||||
MusicType.PLAYLISTS -> R.id.option_filter_playlists
|
||||
// Null maps to filtering nothing.
|
||||
null -> R.id.option_filter_all
|
||||
var filters: Set<MusicType>
|
||||
get() = searchSettings.filters
|
||||
set(value) {
|
||||
// TODO: make this consistent in convention
|
||||
searchSettings.filters = value
|
||||
search(lastQuery)
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the filter type with the newly-selected filter option.
|
||||
*
|
||||
* @return A menu item ID of the new filtering option selected.
|
||||
*/
|
||||
fun setFilterOptionId(@IdRes id: Int) {
|
||||
val newFilter =
|
||||
when (id) {
|
||||
R.id.option_filter_songs -> MusicType.SONGS
|
||||
R.id.option_filter_albums -> MusicType.ALBUMS
|
||||
R.id.option_filter_artists -> MusicType.ARTISTS
|
||||
R.id.option_filter_genres -> MusicType.GENRES
|
||||
R.id.option_filter_playlists -> MusicType.PLAYLISTS
|
||||
// Null maps to filtering nothing.
|
||||
R.id.option_filter_all -> null
|
||||
else -> error("Invalid option ID provided")
|
||||
}
|
||||
L.d("Updating filter type to $newFilter")
|
||||
searchSettings.filterTo = newFilter
|
||||
search(lastQuery)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
val SORT = Sort(Sort.Mode.ByName, Sort.Direction.ASCENDING)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.oxycblt.auxio.ui
|
||||
|
||||
import android.view.View
|
||||
import androidx.core.view.updatePadding
|
||||
import com.google.android.material.appbar.AppBarLayout
|
||||
import kotlin.math.abs
|
||||
|
||||
class FadingToolbarOffsetListener(
|
||||
private val toolbar: View,
|
||||
private val content: View
|
||||
) : AppBarLayout.OnOffsetChangedListener {
|
||||
override fun onOffsetChanged(appBarLayout: AppBarLayout, verticalOffset: Int) {
|
||||
val range = appBarLayout.totalScrollRange
|
||||
// Fade out the toolbar as the AppBarLayout collapses. To prevent status bar overlap,
|
||||
// the alpha transition is shifted such that the Toolbar becomes fully transparent
|
||||
// when the AppBarLayout is only at half-collapsed.
|
||||
toolbar.alpha = 1f - (abs(verticalOffset.toFloat()) / (range.toFloat() / 2))
|
||||
content.updatePadding(bottom = range + verticalOffset)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user