music: move sort name number handling to setting

Make the numeric sort name handling added prior dependent on a new
"Intelligent Sorting" setting that also controls the article checks.

This kind of behavior might not be desirable in all cases, and it makes
the setting more consistent anyway.
This commit is contained in:
Alexander Capehart
2023-03-24 08:26:33 -06:00
parent abeac90735
commit 6fc8a64677
24 changed files with 225 additions and 57 deletions
@@ -130,6 +130,7 @@ constructor(
override fun onLibraryChanged(library: Library?) {
if (library != null) {
logD("Library changed, refreshing library")
// FIXME: Sort name setting changes result in incorrect list updates
// Get the each list of items in the library to use as our list data.
// Applying the preferred sorting to them.
_songsInstructions.put(UpdateInstructions.Diff)
@@ -85,7 +85,7 @@ sealed interface Music : Item {
* A unique identifier for a piece of music.
*
* [UID] enables a much cheaper and more reliable form of differentiating music, derived from
* either a hash of meaningful metadata or the MusicBrainz ID spec. Using this enables several
* either internal app information or the MusicBrainz ID spec. Using this enables several
* improvements to music management in this app, including:
* - Proper differentiation of identical music. It's common for large, well-tagged libraries to
* have functionally duplicate items that are differentiated with MusicBrainz IDs, and so
@@ -369,7 +369,7 @@ class SortName(name: String, musicSettings: MusicSettings) : Comparable<SortName
init {
var sortName = name
if (musicSettings.automaticSortNames) {
if (musicSettings.intelligentSorting) {
sortName =
sortName.run {
when {
@@ -379,23 +379,24 @@ class SortName(name: String, musicSettings: MusicSettings) : Comparable<SortName
else -> this
}
}
}
// Parse out numeric portions of the title and use those for sorting, if applicable.
val numericEnd = sortName.indexOfFirst { !it.isDigit() }
when (numericEnd) {
// No numeric component.
0 -> number = null
// Whole title is numeric.
-1 -> {
number = sortName.toIntOrNull()
sortName = ""
}
// Part of the title is numeric.
else -> {
number = sortName.slice(0 until numericEnd).toIntOrNull()
sortName = sortName.slice(numericEnd until sortName.length)
// Parse out numeric portions of the title and use those for sorting, if applicable.
when (val numericEnd = sortName.indexOfFirst { !it.isDigit() }) {
// No numeric component.
0 -> number = null
// Whole title is numeric.
-1 -> {
number = sortName.toIntOrNull()
sortName = ""
}
// Part of the title is numeric.
else -> {
number = sortName.slice(0 until numericEnd).toIntOrNull()
sortName = sortName.slice(numericEnd until sortName.length)
}
}
} else {
number = null
}
collationKey = COLLATOR.getCollationKey(sortName)
@@ -44,8 +44,9 @@ interface MusicSettings : Settings<MusicSettings.Listener> {
val shouldBeObserving: Boolean
/** A [String] of characters representing the desired characters to denote multi-value tags. */
var multiValueSeparators: String
/** Whether to trim english articles with song sort names. */
val automaticSortNames: Boolean
/** Whether to enable more advanced sorting by articles and numbers. */
val intelligentSorting: Boolean
// TODO: Move sort settings to list module
/** The [Sort] mode used in [Song] lists. */
var songSort: Sort
/** The [Sort] mode used in [Album] lists. */
@@ -110,7 +111,7 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext context: Context
}
}
override val automaticSortNames: Boolean
override val intelligentSorting: Boolean
get() = sharedPreferences.getBoolean(getString(R.string.set_key_auto_sort_names), true)
override var songSort: Sort