settings: move impls out
Move settings implementations out of the class so that they are easier to manage.
This commit is contained in:
parent
866d20f6eb
commit
fb004a9e8b
8 changed files with 360 additions and 357 deletions
|
|
@ -41,7 +41,17 @@ interface HomeSettings : Settings<HomeSettings.Listener> {
|
|||
fun onHideCollaboratorsChanged()
|
||||
}
|
||||
|
||||
private class Real(context: Context) : Settings.Real<Listener>(context), HomeSettings {
|
||||
companion object {
|
||||
/**
|
||||
* Get a framework-backed implementation.
|
||||
* @param context [Context] required.
|
||||
*/
|
||||
fun from(context: Context): HomeSettings = RealHomeSettings(context)
|
||||
}
|
||||
}
|
||||
|
||||
private class RealHomeSettings(context: Context) :
|
||||
Settings.Real<HomeSettings.Listener>(context), HomeSettings {
|
||||
override var homeTabs: Array<Tab>
|
||||
get() =
|
||||
Tab.fromIntCode(
|
||||
|
|
@ -56,23 +66,12 @@ interface HomeSettings : Settings<HomeSettings.Listener> {
|
|||
}
|
||||
|
||||
override val shouldHideCollaborators: Boolean
|
||||
get() =
|
||||
sharedPreferences.getBoolean(getString(R.string.set_key_hide_collaborators), false)
|
||||
get() = sharedPreferences.getBoolean(getString(R.string.set_key_hide_collaborators), false)
|
||||
|
||||
override fun onSettingChanged(key: String, listener: Listener) {
|
||||
override fun onSettingChanged(key: String, listener: HomeSettings.Listener) {
|
||||
when (key) {
|
||||
getString(R.string.set_key_home_tabs) -> listener.onTabsChanged()
|
||||
getString(R.string.set_key_hide_collaborators) ->
|
||||
listener.onHideCollaboratorsChanged()
|
||||
getString(R.string.set_key_hide_collaborators) -> listener.onHideCollaboratorsChanged()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Get a framework-backed implementation.
|
||||
* @param context [Context] required.
|
||||
*/
|
||||
fun from(context: Context): HomeSettings = Real(context)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,17 @@ interface ImageSettings : Settings<ImageSettings.Listener> {
|
|||
fun onCoverModeChanged() {}
|
||||
}
|
||||
|
||||
private class Real(context: Context) : Settings.Real<Listener>(context), ImageSettings {
|
||||
companion object {
|
||||
/**
|
||||
* Get a framework-backed implementation.
|
||||
* @param context [Context] required.
|
||||
*/
|
||||
fun from(context: Context): ImageSettings = RealImageSettings(context)
|
||||
}
|
||||
}
|
||||
|
||||
private class RealImageSettings(context: Context) :
|
||||
Settings.Real<ImageSettings.Listener>(context), ImageSettings {
|
||||
override val coverMode: CoverMode
|
||||
get() =
|
||||
CoverMode.fromIntCode(
|
||||
|
|
@ -65,9 +75,9 @@ interface ImageSettings : Settings<ImageSettings.Listener> {
|
|||
}
|
||||
}
|
||||
|
||||
override fun onSettingChanged(key: String, listener: Listener) {
|
||||
override fun onSettingChanged(key: String, listener: ImageSettings.Listener) {
|
||||
if (key == getString(R.string.set_key_cover_mode)) {
|
||||
listOf(key, listener)
|
||||
listener.onCoverModeChanged()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -75,13 +85,4 @@ interface ImageSettings : Settings<ImageSettings.Listener> {
|
|||
const val OLD_KEY_SHOW_COVERS = "KEY_SHOW_COVERS"
|
||||
const val OLD_KEY_QUALITY_COVERS = "KEY_QUALITY_COVERS"
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Get a framework-backed implementation.
|
||||
* @param context [Context] required.
|
||||
*/
|
||||
fun from(context: Context): ImageSettings = Real(context)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,7 +62,17 @@ interface MusicSettings : Settings<MusicSettings.Listener> {
|
|||
fun onObservingChanged() {}
|
||||
}
|
||||
|
||||
private class Real(context: Context) : Settings.Real<Listener>(context), MusicSettings {
|
||||
companion object {
|
||||
/**
|
||||
* Get a framework-backed implementation.
|
||||
* @param context [Context] required.
|
||||
*/
|
||||
fun from(context: Context): MusicSettings = RealMusicSettings(context)
|
||||
}
|
||||
}
|
||||
|
||||
private class RealMusicSettings(context: Context) :
|
||||
Settings.Real<MusicSettings.Listener>(context), MusicSettings {
|
||||
private val storageManager = context.getSystemServiceCompat(StorageManager::class)
|
||||
|
||||
override var musicDirs: MusicDirectories
|
||||
|
|
@ -73,8 +83,7 @@ interface MusicSettings : Settings<MusicSettings.Listener> {
|
|||
.mapNotNull { Directory.fromDocumentTreeUri(storageManager, it) }
|
||||
return MusicDirectories(
|
||||
dirs,
|
||||
sharedPreferences.getBoolean(
|
||||
getString(R.string.set_key_music_dirs_include), false))
|
||||
sharedPreferences.getBoolean(getString(R.string.set_key_music_dirs_include), false))
|
||||
}
|
||||
set(value) {
|
||||
sharedPreferences.edit {
|
||||
|
|
@ -87,8 +96,7 @@ interface MusicSettings : Settings<MusicSettings.Listener> {
|
|||
}
|
||||
|
||||
override val excludeNonMusic: Boolean
|
||||
get() =
|
||||
sharedPreferences.getBoolean(getString(R.string.set_key_exclude_non_music), true)
|
||||
get() = sharedPreferences.getBoolean(getString(R.string.set_key_exclude_non_music), true)
|
||||
|
||||
override val shouldBeObserving: Boolean
|
||||
get() = sharedPreferences.getBoolean(getString(R.string.set_key_observing), false)
|
||||
|
|
@ -119,8 +127,7 @@ interface MusicSettings : Settings<MusicSettings.Listener> {
|
|||
override var albumSort: Sort
|
||||
get() =
|
||||
Sort.fromIntCode(
|
||||
sharedPreferences.getInt(
|
||||
getString(R.string.set_key_albums_sort), Int.MIN_VALUE))
|
||||
sharedPreferences.getInt(getString(R.string.set_key_albums_sort), Int.MIN_VALUE))
|
||||
?: Sort(Sort.Mode.ByName, true)
|
||||
set(value) {
|
||||
sharedPreferences.edit {
|
||||
|
|
@ -132,8 +139,7 @@ interface MusicSettings : Settings<MusicSettings.Listener> {
|
|||
override var artistSort: Sort
|
||||
get() =
|
||||
Sort.fromIntCode(
|
||||
sharedPreferences.getInt(
|
||||
getString(R.string.set_key_artists_sort), Int.MIN_VALUE))
|
||||
sharedPreferences.getInt(getString(R.string.set_key_artists_sort), Int.MIN_VALUE))
|
||||
?: Sort(Sort.Mode.ByName, true)
|
||||
set(value) {
|
||||
sharedPreferences.edit {
|
||||
|
|
@ -145,8 +151,7 @@ interface MusicSettings : Settings<MusicSettings.Listener> {
|
|||
override var genreSort: Sort
|
||||
get() =
|
||||
Sort.fromIntCode(
|
||||
sharedPreferences.getInt(
|
||||
getString(R.string.set_key_genres_sort), Int.MIN_VALUE))
|
||||
sharedPreferences.getInt(getString(R.string.set_key_genres_sort), Int.MIN_VALUE))
|
||||
?: Sort(Sort.Mode.ByName, true)
|
||||
set(value) {
|
||||
sharedPreferences.edit {
|
||||
|
|
@ -203,7 +208,7 @@ interface MusicSettings : Settings<MusicSettings.Listener> {
|
|||
}
|
||||
}
|
||||
|
||||
override fun onSettingChanged(key: String, listener: Listener) {
|
||||
override fun onSettingChanged(key: String, listener: MusicSettings.Listener) {
|
||||
when (key) {
|
||||
getString(R.string.set_key_exclude_non_music),
|
||||
getString(R.string.set_key_music_dirs),
|
||||
|
|
@ -212,13 +217,4 @@ interface MusicSettings : Settings<MusicSettings.Listener> {
|
|||
getString(R.string.set_key_observing) -> listener.onObservingChanged()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Get a framework-backed implementation.
|
||||
* @param context [Context] required.
|
||||
*/
|
||||
fun from(context: Context): MusicSettings = Real(context)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ class Directory private constructor(val volume: StorageVolume, val relativePath:
|
|||
/**
|
||||
* Converts this [Directory] instance into an opaque document tree path. This is a huge
|
||||
* violation of the document tree URI contract, but it's also the only one can sensibly work
|
||||
* with these uris in the UI, and it doesn't exactly matter since we never write or read
|
||||
* with these uris in the UI, and it doesn't exactly matter since we never write or read to
|
||||
* directory.
|
||||
* @return A URI [String] abiding by the document tree specification, or null if the [Directory]
|
||||
* is not valid.
|
||||
|
|
|
|||
|
|
@ -52,11 +52,19 @@ interface SearchEngine {
|
|||
val genres: List<Genre>?
|
||||
)
|
||||
|
||||
private class Real(private val context: Context) : SearchEngine {
|
||||
override suspend fun search(items: Items, query: String) =
|
||||
Items(
|
||||
songs =
|
||||
items.songs?.searchListImpl(query) { q, song -> song.path.name.contains(q) },
|
||||
companion object {
|
||||
/**
|
||||
* Get a framework-backed implementation.
|
||||
* @param context [Context] required.
|
||||
*/
|
||||
fun from(context: Context): SearchEngine = RealSearchEngine(context)
|
||||
}
|
||||
}
|
||||
|
||||
private class RealSearchEngine(private val context: Context) : SearchEngine {
|
||||
override suspend fun search(items: SearchEngine.Items, query: String) =
|
||||
SearchEngine.Items(
|
||||
songs = items.songs?.searchListImpl(query) { q, song -> song.path.name.contains(q) },
|
||||
albums = items.albums?.searchListImpl(query),
|
||||
artists = items.artists?.searchListImpl(query),
|
||||
genres = items.genres?.searchListImpl(query))
|
||||
|
|
@ -66,8 +74,8 @@ interface SearchEngine {
|
|||
* @param query The query to search for. The routine will compare this query to the names of
|
||||
* each object in the list and
|
||||
* @param fallback Additional comparison code to run if the item does not match the query
|
||||
* initially. This can be used to compare against additional attributes to improve search
|
||||
* result quality.
|
||||
* initially. This can be used to compare against additional attributes to improve search result
|
||||
* quality.
|
||||
*/
|
||||
private inline fun <T : Music> List<T>.searchListImpl(
|
||||
query: String,
|
||||
|
|
@ -111,13 +119,4 @@ interface SearchEngine {
|
|||
*/
|
||||
val NORMALIZATION_SANITIZE_REGEX = Regex("\\p{InCombiningDiacriticalMarks}+")
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Get a framework-backed implementation.
|
||||
* @param context [Context] required.
|
||||
*/
|
||||
fun from(context: Context): SearchEngine = Real(context)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,26 +31,25 @@ interface SearchSettings : Settings<Nothing> {
|
|||
/** The type of Music the search view is currently filtering to. */
|
||||
var searchFilterMode: MusicMode?
|
||||
|
||||
private class Real(context: Context) : Settings.Real<Nothing>(context), SearchSettings {
|
||||
override var searchFilterMode: MusicMode?
|
||||
get() =
|
||||
MusicMode.fromIntCode(
|
||||
sharedPreferences.getInt(
|
||||
getString(R.string.set_key_search_filter), Int.MIN_VALUE))
|
||||
set(value) {
|
||||
sharedPreferences.edit {
|
||||
putInt(
|
||||
getString(R.string.set_key_search_filter), value?.intCode ?: Int.MIN_VALUE)
|
||||
apply()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Get a framework-backed implementation.
|
||||
* @param context [Context] required.
|
||||
*/
|
||||
fun from(context: Context): SearchSettings = Real(context)
|
||||
fun from(context: Context): SearchSettings = RealSearchSettings(context)
|
||||
}
|
||||
}
|
||||
|
||||
private class RealSearchSettings(context: Context) :
|
||||
Settings.Real<Nothing>(context), SearchSettings {
|
||||
override var searchFilterMode: MusicMode?
|
||||
get() =
|
||||
MusicMode.fromIntCode(
|
||||
sharedPreferences.getInt(getString(R.string.set_key_search_filter), Int.MIN_VALUE))
|
||||
set(value) {
|
||||
sharedPreferences.edit {
|
||||
putInt(getString(R.string.set_key_search_filter), value?.intCode ?: Int.MIN_VALUE)
|
||||
apply()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,7 +45,17 @@ interface UISettings : Settings<UISettings.Listener> {
|
|||
fun onRoundModeChanged()
|
||||
}
|
||||
|
||||
private class Real(context: Context) : Settings.Real<Listener>(context), UISettings {
|
||||
companion object {
|
||||
/**
|
||||
* Get a framework-backed implementation.
|
||||
* @param context [Context] required.
|
||||
*/
|
||||
fun from(context: Context): UISettings = RealUISettings(context)
|
||||
}
|
||||
}
|
||||
|
||||
private class RealUISettings(context: Context) :
|
||||
Settings.Real<UISettings.Listener>(context), UISettings {
|
||||
override val theme: Int
|
||||
get() =
|
||||
sharedPreferences.getInt(
|
||||
|
|
@ -88,7 +98,7 @@ interface UISettings : Settings<UISettings.Listener> {
|
|||
}
|
||||
}
|
||||
|
||||
override fun onSettingChanged(key: String, listener: Listener) {
|
||||
override fun onSettingChanged(key: String, listener: UISettings.Listener) {
|
||||
if (key == getString(R.string.set_key_round_mode)) {
|
||||
listener.onRoundModeChanged()
|
||||
}
|
||||
|
|
@ -97,13 +107,4 @@ interface UISettings : Settings<UISettings.Listener> {
|
|||
private companion object {
|
||||
const val OLD_KEY_ACCENT3 = "auxio_accent"
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Get a framework-backed implementation.
|
||||
* @param context [Context] required.
|
||||
*/
|
||||
fun from(context: Context): UISettings = Real(context)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,10 +17,18 @@
|
|||
|
||||
package org.oxycblt.auxio.music.tags
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class DiscTest {
|
||||
@Test
|
||||
fun disc_compare() {
|
||||
val a = Disc(1, "Part I")
|
||||
val b = Disc(2, "Part II")
|
||||
assertEquals(-1, a.compareTo(b))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun disc_equals_correct() {
|
||||
val a = Disc(1, "Part I")
|
||||
|
|
|
|||
Loading…
Reference in a new issue