service: decouple maxtab handling and ids
Simpler and more versatile.
This commit is contained in:
parent
66fad791d5
commit
0ef2dafc29
4 changed files with 43 additions and 53 deletions
|
@ -89,9 +89,7 @@ class AuxioService :
|
|||
clientUid: Int,
|
||||
rootHints: Bundle?
|
||||
): BrowserRoot {
|
||||
val maximumRootChildLimit =
|
||||
rootHints?.getInt(MediaConstants.BROWSER_ROOT_HINTS_KEY_ROOT_CHILDREN_LIMIT, 4) ?: 4
|
||||
return musicFragment.getRoot(maximumRootChildLimit)
|
||||
return musicFragment.getRoot()
|
||||
}
|
||||
|
||||
override fun onLoadItem(itemId: String, result: Result<MediaItem>) {
|
||||
|
@ -99,7 +97,10 @@ class AuxioService :
|
|||
}
|
||||
|
||||
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>>) {
|
||||
|
|
|
@ -88,13 +88,10 @@ private constructor(
|
|||
}
|
||||
|
||||
override fun invalidateTabs() {
|
||||
for (i in 0..10) {
|
||||
// TODO: Temporary bodge, move the amount parameter to a bundle extra
|
||||
val rootId = MediaSessionUID.Tab(TabNode.Root(i)).toString()
|
||||
val moreId = MediaSessionUID.Tab(TabNode.More(i)).toString()
|
||||
val rootId = MediaSessionUID.Tab(TabNode.Root).toString()
|
||||
val moreId = MediaSessionUID.Tab(TabNode.More).toString()
|
||||
invalidator.invalidateMusic(setOf(rootId, moreId))
|
||||
}
|
||||
}
|
||||
|
||||
override fun invalidate(type: MusicType, replace: Int?) {
|
||||
val deviceLibrary = musicRepository.deviceLibrary ?: return
|
||||
|
@ -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 userLibrary = musicRepository.userLibrary
|
||||
if (deviceLibrary == null || userLibrary == null) {
|
||||
return listOf()
|
||||
}
|
||||
|
||||
return getMediaItemList(parentId)
|
||||
return getMediaItemList(parentId, maxTabs)
|
||||
}
|
||||
|
||||
suspend fun search(query: String): MutableList<MediaItem> {
|
||||
|
@ -181,10 +177,10 @@ private constructor(
|
|||
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)) {
|
||||
is MediaSessionUID.Tab -> {
|
||||
getCategoryMediaItems(mediaSessionUID.node)
|
||||
getCategoryMediaItems(mediaSessionUID.node, maxTabs)
|
||||
}
|
||||
is MediaSessionUID.SingleItem -> {
|
||||
getChildMediaItems(mediaSessionUID.uid)
|
||||
|
@ -198,21 +194,21 @@ private constructor(
|
|||
}
|
||||
}
|
||||
|
||||
private fun getCategoryMediaItems(node: TabNode) =
|
||||
private fun getCategoryMediaItems(node: TabNode, maxTabs: Int) =
|
||||
when (node) {
|
||||
is TabNode.Root -> {
|
||||
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) {
|
||||
base + TabNode.More(tabs.size - base.size)
|
||||
base + TabNode.More
|
||||
} else {
|
||||
base
|
||||
}
|
||||
.map { it.toMediaItem(context) }
|
||||
}
|
||||
is TabNode.More ->
|
||||
homeGenerator.tabs().takeLast(node.remainder).map {
|
||||
TabNode.Home(it).toMediaItem(context)
|
||||
is TabNode.More -> {
|
||||
val tabs = homeGenerator.tabs()
|
||||
tabs.takeLast(tabs.size - maxTabs).map { TabNode.Home(it).toMediaItem(context) }
|
||||
}
|
||||
is TabNode.Home ->
|
||||
when (node.type) {
|
||||
|
|
|
@ -99,14 +99,17 @@ constructor(
|
|||
indexer.createNotification(post)
|
||||
}
|
||||
|
||||
fun getRoot(maxItems: Int) =
|
||||
BrowserRoot(MediaSessionUID.Tab(TabNode.Root(maxItems)).toString(), Bundle())
|
||||
fun getRoot() = BrowserRoot(MediaSessionUID.Tab(TabNode.Root).toString(), Bundle())
|
||||
|
||||
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>>) =
|
||||
result.dispatch { musicBrowser.getChildren(mediaId)?.toMutableList() }
|
||||
fun getChildren(mediaId: String, maxTabs: Int, result: Result<MutableList<MediaItem>>) =
|
||||
result.dispatch { musicBrowser.getChildren(mediaId, maxTabs)?.toMutableList() }
|
||||
|
||||
fun search(query: String, result: Result<MutableList<MediaItem>>) =
|
||||
result.dispatchAsync { musicBrowser.search(query) }
|
||||
|
|
|
@ -23,37 +23,27 @@ import org.oxycblt.auxio.music.MusicType
|
|||
|
||||
sealed class TabNode {
|
||||
abstract val id: String
|
||||
abstract val data: Int
|
||||
abstract val nameRes: Int
|
||||
abstract val bitmapRes: Int?
|
||||
|
||||
override fun toString() = "${id}/${data}"
|
||||
override fun toString() = id
|
||||
|
||||
data class Root(val amount: Int) : TabNode() {
|
||||
override val id = ID
|
||||
override val data = amount
|
||||
data object Root : TabNode() {
|
||||
override val id = "root"
|
||||
override val nameRes = R.string.info_app_name
|
||||
override val bitmapRes = null
|
||||
|
||||
companion object {
|
||||
const val ID = "root"
|
||||
}
|
||||
override fun toString() = id
|
||||
}
|
||||
|
||||
data class More(val remainder: Int) : TabNode() {
|
||||
override val id = ID
|
||||
override val data = remainder
|
||||
data object More : TabNode() {
|
||||
override val id = "more"
|
||||
override val nameRes = R.string.lbl_more
|
||||
override val bitmapRes = null
|
||||
|
||||
companion object {
|
||||
const val ID = "more"
|
||||
}
|
||||
}
|
||||
|
||||
data class Home(val type: MusicType) : TabNode() {
|
||||
override val id = ID
|
||||
override val data = type.intCode
|
||||
override val id = "$ID/${type.intCode}"
|
||||
override val bitmapRes: Int
|
||||
get() =
|
||||
when (type) {
|
||||
|
@ -73,15 +63,15 @@ sealed class TabNode {
|
|||
|
||||
companion object {
|
||||
fun fromString(str: String): TabNode? {
|
||||
val split = str.split("/", limit = 2)
|
||||
if (split.size != 2) {
|
||||
return null
|
||||
return when {
|
||||
str == Root.id -> Root
|
||||
str == More.id -> More
|
||||
str.startsWith(Home.ID) -> {
|
||||
val split = str.split("/")
|
||||
if (split.size != 2) return null
|
||||
val intCode = split[1].toIntOrNull() ?: return null
|
||||
Home(MusicType.fromIntCode(intCode) ?: return null)
|
||||
}
|
||||
val data = split[1].toIntOrNull() ?: return null
|
||||
return when (split[0]) {
|
||||
Root.ID -> Root(data)
|
||||
More.ID -> More(data)
|
||||
Home.ID -> Home(MusicType.fromIntCode(data) ?: return null)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue