search: cleanup new filter impl
This commit is contained in:
@@ -154,8 +154,8 @@ class SearchFragment : ListFragment<Music, FragmentSearchBinding>() {
|
||||
binding.searchFilterGenres.isChecked = MusicType.GENRES in filters
|
||||
binding.searchFilterPlaylists.isChecked = MusicType.PLAYLISTS in filters
|
||||
binding.searchFilters.apply {
|
||||
setOnCheckedStateChangeListener { group, ids ->
|
||||
val types =
|
||||
setOnCheckedStateChangeListener { _, ids ->
|
||||
val filters =
|
||||
ids.mapNotNullTo(mutableSetOf()) {
|
||||
when (it) {
|
||||
R.id.search_filter_songs -> MusicType.SONGS
|
||||
@@ -166,7 +166,7 @@ class SearchFragment : ListFragment<Music, FragmentSearchBinding>() {
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
searchModel.filters = types
|
||||
searchModel.updateFilters(filters)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,10 +21,10 @@ package org.oxycblt.auxio.search
|
||||
import android.content.Context
|
||||
import androidx.core.content.edit
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import javax.inject.Inject
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.music.MusicType
|
||||
import org.oxycblt.auxio.settings.Settings
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* User configuration specific to the search UI.
|
||||
@@ -39,17 +39,47 @@ interface SearchSettings : Settings<Nothing> {
|
||||
class SearchSettingsImpl @Inject constructor(@ApplicationContext context: Context) :
|
||||
Settings.Impl<Nothing>(context), SearchSettings {
|
||||
override var filters: Set<MusicType>
|
||||
get() =
|
||||
sharedPreferences
|
||||
.getStringSet(getString(R.string.set_key_search_filters), null)
|
||||
?.mapNotNull { it.toIntOrNull()?.let(MusicType::fromIntCode) }
|
||||
?.toSet() ?: setOf()
|
||||
get() = buildSet {
|
||||
val intCode = sharedPreferences.getInt(getString(R.string.set_key_search_filters), 0)
|
||||
if (intCode and BIT_SONGS == BIT_SONGS) add(MusicType.SONGS)
|
||||
if (intCode and BIT_ALBUMS == BIT_ALBUMS) add(MusicType.ALBUMS)
|
||||
if (intCode and BIT_ARTISTS == BIT_ARTISTS) add(MusicType.ARTISTS)
|
||||
if (intCode and BIT_GENRES == BIT_GENRES) add(MusicType.GENRES)
|
||||
if (intCode and BIT_PLAYLISTS == BIT_PLAYLISTS) add(MusicType.PLAYLISTS)
|
||||
}
|
||||
set(value) {
|
||||
var intCode = 0;
|
||||
if (MusicType.SONGS in value) intCode = intCode or BIT_SONGS
|
||||
if (MusicType.ALBUMS in value) intCode = intCode or BIT_ALBUMS
|
||||
if (MusicType.ARTISTS in value) intCode = intCode or BIT_ARTISTS
|
||||
if (MusicType.GENRES in value) intCode = intCode or BIT_GENRES
|
||||
if (MusicType.PLAYLISTS in value) intCode = intCode or BIT_PLAYLISTS
|
||||
sharedPreferences.edit {
|
||||
putStringSet(
|
||||
getString(R.string.set_key_search_filters),
|
||||
value.map { it.intCode.toString() }.toSet(),
|
||||
)
|
||||
putInt(getString(R.string.set_key_search_filters), intCode)
|
||||
}
|
||||
}
|
||||
|
||||
override fun migrate() {
|
||||
val filter = MusicType.fromIntCode(sharedPreferences.getInt(OLD_KEY_SEARCH_FILTER, 0))
|
||||
val intCode = when (filter) {
|
||||
MusicType.SONGS -> BIT_SONGS
|
||||
MusicType.ALBUMS -> BIT_ALBUMS
|
||||
MusicType.ARTISTS -> BIT_ARTISTS
|
||||
MusicType.GENRES -> BIT_GENRES
|
||||
MusicType.PLAYLISTS -> BIT_PLAYLISTS
|
||||
else -> 0
|
||||
}
|
||||
sharedPreferences.edit {
|
||||
putInt(getString(R.string.set_key_search_filters), intCode)
|
||||
}
|
||||
|
||||
}
|
||||
private companion object {
|
||||
const val OLD_KEY_SEARCH_FILTER = "KEY_SEARCH_FILTER"
|
||||
const val BIT_SONGS = 0b00001
|
||||
const val BIT_ALBUMS = 0b00010
|
||||
const val BIT_ARTISTS = 0b00100
|
||||
const val BIT_GENRES = 0b01000
|
||||
const val BIT_PLAYLISTS = 0b10000
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,13 +182,20 @@ constructor(
|
||||
}
|
||||
}
|
||||
|
||||
var filters: Set<MusicType>
|
||||
/**
|
||||
* The current filters used for search.
|
||||
*/
|
||||
val filters: Set<MusicType>
|
||||
get() = searchSettings.filters
|
||||
set(value) {
|
||||
// TODO: make this consistent in convention
|
||||
searchSettings.filters = value
|
||||
search(lastQuery)
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the filters used by search. Will trigger a research.
|
||||
* @param filters The new filters to use.
|
||||
*/
|
||||
fun updateFilters(filters: Set<MusicType>) {
|
||||
searchSettings.filters = filters
|
||||
search(lastQuery)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
val SORT = Sort(Sort.Mode.ByName, Sort.Direction.ASCENDING)
|
||||
|
||||
Reference in New Issue
Block a user