music: introduce more optimized uid format

This commit is contained in:
Alexander Capehart
2025-12-31 20:47:19 -07:00
parent 56991a8c79
commit 141d9e4d2e
@@ -93,17 +93,17 @@ sealed interface Music {
override fun equals(other: Any?) = override fun equals(other: Any?) =
other is UID && format == other.format && item == other.item && uuid == other.uuid other is UID && format == other.format && item == other.item && uuid == other.uuid
override fun toString() = "${format.namespace}:${item.intCode.toString(16)}-$uuid" override fun toString() = "u${format.microNamespace}${item.microNamespace}$uuid"
internal enum class Item(val intCode: Int) { internal enum class Item(val intCode: Int, val microNamespace: Char) {
// Item used to be MusicType back when the music module was // Item used to be MusicType back when the music module was
// part of Auxio, so these old integer codes remain. // part of Auxio, so these old integer codes remain.
// TODO: Introduce new UID format that removes these. // TODO: Introduce new UID format that removes these.
SONG(0xA10B), SONG(0xA10B, 's'),
ALBUM(0xA10A), ALBUM(0xA10A, 'a'),
ARTIST(0xA109), ARTIST(0xA109, 'r'),
GENRE(0xA108), GENRE(0xA108, 'g'),
PLAYLIST(0xA107), PLAYLIST(0xA107, 'p'),
} }
/** /**
@@ -111,12 +111,12 @@ sealed interface Music {
* *
* @param namespace Namespace to use in the [Music.UID]'s string representation. * @param namespace Namespace to use in the [Music.UID]'s string representation.
*/ */
private enum class Format(val namespace: String) { private enum class Format(val namespace: String, val microNamespace: Char) {
/** @see auxio */ /** @see auxio */
AUXIO("org.oxycblt.auxio"), AUXIO("org.oxycblt.auxio", 'a'),
/** @see musicBrainz */ /** @see musicBrainz */
MUSICBRAINZ("org.musicbrainz"), MUSICBRAINZ("org.musicbrainz", 'm'),
} }
object TypeConverters { object TypeConverters {
@@ -202,6 +202,30 @@ sealed interface Music {
* representation was invalid. * representation was invalid.
*/ */
fun fromString(uid: String): UID? { fun fromString(uid: String): UID? {
if (uid.startsWith("u")) {
// switch to new micro-format parsing
val microFormat =
when (uid.getOrNull(1)) {
Format.AUXIO.microNamespace -> Format.AUXIO
Format.MUSICBRAINZ.microNamespace -> Format.MUSICBRAINZ
else -> return null
}
val microItem =
when (uid.getOrNull(2)) {
Item.SONG.microNamespace -> Item.SONG
Item.ALBUM.microNamespace -> Item.ALBUM
Item.ARTIST.microNamespace -> Item.ARTIST
Item.GENRE.microNamespace -> Item.GENRE
Item.PLAYLIST.microNamespace -> Item.PLAYLIST
else -> return null
}
if (uid.length < 4) {
// not quite sure if substring will error on malformed uids
return null
}
val uuid = uid.substring(3 until uid.length).toUuidOrNull() ?: return null
return UID(microFormat, microItem, uuid)
}
val split = uid.split(':', limit = 2) val split = uid.split(':', limit = 2)
if (split.size != 2) { if (split.size != 2) {
return null return null