service: decouple maxtab handling and ids

Simpler and more versatile.
This commit is contained in:
Alexander Capehart 2024-09-19 17:22:12 -06:00
parent 66fad791d5
commit 0ef2dafc29
4 changed files with 43 additions and 53 deletions

View file

@ -89,9 +89,7 @@ class AuxioService :
clientUid: Int, clientUid: Int,
rootHints: Bundle? rootHints: Bundle?
): BrowserRoot { ): BrowserRoot {
val maximumRootChildLimit = return musicFragment.getRoot()
rootHints?.getInt(MediaConstants.BROWSER_ROOT_HINTS_KEY_ROOT_CHILDREN_LIMIT, 4) ?: 4
return musicFragment.getRoot(maximumRootChildLimit)
} }
override fun onLoadItem(itemId: String, result: Result<MediaItem>) { override fun onLoadItem(itemId: String, result: Result<MediaItem>) {
@ -99,7 +97,10 @@ class AuxioService :
} }
override fun onLoadChildren(parentId: String, result: Result<MutableList<MediaItem>>) { override fun onLoadChildren(parentId: String, result: Result<MutableList<MediaItem>>) {
musicFragment.getChildren(parentId, result) val maximumRootChildLimit =
browserRootHints?.getInt(MediaConstants.BROWSER_ROOT_HINTS_KEY_ROOT_CHILDREN_LIMIT, 4)
?: 4
musicFragment.getChildren(parentId, maximumRootChildLimit, result)
} }
override fun onSearch(query: String, extras: Bundle?, result: Result<MutableList<MediaItem>>) { override fun onSearch(query: String, extras: Bundle?, result: Result<MutableList<MediaItem>>) {

View file

@ -88,12 +88,9 @@ private constructor(
} }
override fun invalidateTabs() { override fun invalidateTabs() {
for (i in 0..10) { val rootId = MediaSessionUID.Tab(TabNode.Root).toString()
// TODO: Temporary bodge, move the amount parameter to a bundle extra val moreId = MediaSessionUID.Tab(TabNode.More).toString()
val rootId = MediaSessionUID.Tab(TabNode.Root(i)).toString() invalidator.invalidateMusic(setOf(rootId, moreId))
val moreId = MediaSessionUID.Tab(TabNode.More(i)).toString()
invalidator.invalidateMusic(setOf(rootId, moreId))
}
} }
override fun invalidate(type: MusicType, replace: Int?) { override fun invalidate(type: MusicType, replace: Int?) {
@ -135,14 +132,13 @@ private constructor(
} }
} }
fun getChildren(parentId: String): List<MediaItem>? { fun getChildren(parentId: String, maxTabs: Int): List<MediaItem>? {
val deviceLibrary = musicRepository.deviceLibrary val deviceLibrary = musicRepository.deviceLibrary
val userLibrary = musicRepository.userLibrary val userLibrary = musicRepository.userLibrary
if (deviceLibrary == null || userLibrary == null) { if (deviceLibrary == null || userLibrary == null) {
return listOf() return listOf()
} }
return getMediaItemList(parentId, maxTabs)
return getMediaItemList(parentId)
} }
suspend fun search(query: String): MutableList<MediaItem> { suspend fun search(query: String): MutableList<MediaItem> {
@ -181,10 +177,10 @@ private constructor(
return music return music
} }
private fun getMediaItemList(id: String): List<MediaItem>? { private fun getMediaItemList(id: String, maxTabs: Int): List<MediaItem>? {
return when (val mediaSessionUID = MediaSessionUID.fromString(id)) { return when (val mediaSessionUID = MediaSessionUID.fromString(id)) {
is MediaSessionUID.Tab -> { is MediaSessionUID.Tab -> {
getCategoryMediaItems(mediaSessionUID.node) getCategoryMediaItems(mediaSessionUID.node, maxTabs)
} }
is MediaSessionUID.SingleItem -> { is MediaSessionUID.SingleItem -> {
getChildMediaItems(mediaSessionUID.uid) getChildMediaItems(mediaSessionUID.uid)
@ -198,22 +194,22 @@ private constructor(
} }
} }
private fun getCategoryMediaItems(node: TabNode) = private fun getCategoryMediaItems(node: TabNode, maxTabs: Int) =
when (node) { when (node) {
is TabNode.Root -> { is TabNode.Root -> {
val tabs = homeGenerator.tabs() val tabs = homeGenerator.tabs()
val base = tabs.take(node.amount - 1).map { TabNode.Home(it) } val base = tabs.take(maxTabs - 1).map { TabNode.Home(it) }
if (base.size < tabs.size) { if (base.size < tabs.size) {
base + TabNode.More(tabs.size - base.size) base + TabNode.More
} else { } else {
base base
} }
.map { it.toMediaItem(context) } .map { it.toMediaItem(context) }
} }
is TabNode.More -> is TabNode.More -> {
homeGenerator.tabs().takeLast(node.remainder).map { val tabs = homeGenerator.tabs()
TabNode.Home(it).toMediaItem(context) tabs.takeLast(tabs.size - maxTabs).map { TabNode.Home(it).toMediaItem(context) }
} }
is TabNode.Home -> is TabNode.Home ->
when (node.type) { when (node.type) {
MusicType.SONGS -> homeGenerator.songs().map { it.toMediaItem(context, null) } MusicType.SONGS -> homeGenerator.songs().map { it.toMediaItem(context, null) }

View file

@ -99,14 +99,17 @@ constructor(
indexer.createNotification(post) indexer.createNotification(post)
} }
fun getRoot(maxItems: Int) = fun getRoot() = BrowserRoot(MediaSessionUID.Tab(TabNode.Root).toString(), Bundle())
BrowserRoot(MediaSessionUID.Tab(TabNode.Root(maxItems)).toString(), Bundle())
fun getItem(mediaId: String, result: Result<MediaItem>) = fun getItem(mediaId: String, result: Result<MediaItem>) =
result.dispatch { musicBrowser.getItem(mediaId) } result.dispatch {
musicBrowser.getItem(
mediaId,
)
}
fun getChildren(mediaId: String, result: Result<MutableList<MediaItem>>) = fun getChildren(mediaId: String, maxTabs: Int, result: Result<MutableList<MediaItem>>) =
result.dispatch { musicBrowser.getChildren(mediaId)?.toMutableList() } result.dispatch { musicBrowser.getChildren(mediaId, maxTabs)?.toMutableList() }
fun search(query: String, result: Result<MutableList<MediaItem>>) = fun search(query: String, result: Result<MutableList<MediaItem>>) =
result.dispatchAsync { musicBrowser.search(query) } result.dispatchAsync { musicBrowser.search(query) }

View file

@ -23,37 +23,27 @@ import org.oxycblt.auxio.music.MusicType
sealed class TabNode { sealed class TabNode {
abstract val id: String abstract val id: String
abstract val data: Int
abstract val nameRes: Int abstract val nameRes: Int
abstract val bitmapRes: Int? abstract val bitmapRes: Int?
override fun toString() = "${id}/${data}" override fun toString() = id
data class Root(val amount: Int) : TabNode() { data object Root : TabNode() {
override val id = ID override val id = "root"
override val data = amount
override val nameRes = R.string.info_app_name override val nameRes = R.string.info_app_name
override val bitmapRes = null override val bitmapRes = null
companion object { override fun toString() = id
const val ID = "root"
}
} }
data class More(val remainder: Int) : TabNode() { data object More : TabNode() {
override val id = ID override val id = "more"
override val data = remainder
override val nameRes = R.string.lbl_more override val nameRes = R.string.lbl_more
override val bitmapRes = null override val bitmapRes = null
companion object {
const val ID = "more"
}
} }
data class Home(val type: MusicType) : TabNode() { data class Home(val type: MusicType) : TabNode() {
override val id = ID override val id = "$ID/${type.intCode}"
override val data = type.intCode
override val bitmapRes: Int override val bitmapRes: Int
get() = get() =
when (type) { when (type) {
@ -73,15 +63,15 @@ sealed class TabNode {
companion object { companion object {
fun fromString(str: String): TabNode? { fun fromString(str: String): TabNode? {
val split = str.split("/", limit = 2) return when {
if (split.size != 2) { str == Root.id -> Root
return null str == More.id -> More
} str.startsWith(Home.ID) -> {
val data = split[1].toIntOrNull() ?: return null val split = str.split("/")
return when (split[0]) { if (split.size != 2) return null
Root.ID -> Root(data) val intCode = split[1].toIntOrNull() ?: return null
More.ID -> More(data) Home(MusicType.fromIntCode(intCode) ?: return null)
Home.ID -> Home(MusicType.fromIntCode(data) ?: return null) }
else -> null else -> null
} }
} }