music: paginate browser results
Hopefully now that I'm self-rolling this it'll actually work.
This commit is contained in:
parent
1a78e973d7
commit
adfed98b71
2 changed files with 43 additions and 9 deletions
|
@ -23,6 +23,7 @@ import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
|
import android.support.v4.media.MediaBrowserCompat
|
||||||
import android.support.v4.media.MediaBrowserCompat.MediaItem
|
import android.support.v4.media.MediaBrowserCompat.MediaItem
|
||||||
import androidx.annotation.StringRes
|
import androidx.annotation.StringRes
|
||||||
import androidx.core.app.NotificationChannelCompat
|
import androidx.core.app.NotificationChannelCompat
|
||||||
|
@ -32,6 +33,7 @@ import androidx.core.app.ServiceCompat
|
||||||
import androidx.media.MediaBrowserServiceCompat
|
import androidx.media.MediaBrowserServiceCompat
|
||||||
import androidx.media.utils.MediaConstants
|
import androidx.media.utils.MediaConstants
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import org.oxycblt.auxio.music.service.MusicBrowser
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import org.oxycblt.auxio.music.service.MusicServiceFragment
|
import org.oxycblt.auxio.music.service.MusicServiceFragment
|
||||||
import org.oxycblt.auxio.playback.service.PlaybackServiceFragment
|
import org.oxycblt.auxio.playback.service.PlaybackServiceFragment
|
||||||
|
@ -96,14 +98,32 @@ class AuxioService :
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onLoadChildren(parentId: String, result: Result<MutableList<MediaItem>>) {
|
override fun onLoadChildren(parentId: String, result: Result<MutableList<MediaItem>>) {
|
||||||
val maximumRootChildLimit =
|
val maximumRootChildLimit = getRootChildrenLimit()
|
||||||
browserRootHints?.getInt(MediaConstants.BROWSER_ROOT_HINTS_KEY_ROOT_CHILDREN_LIMIT, 4)
|
musicFragment.getChildren(parentId, maximumRootChildLimit, result, null)
|
||||||
?: 4
|
}
|
||||||
musicFragment.getChildren(parentId, maximumRootChildLimit, result)
|
|
||||||
|
override fun onLoadChildren(
|
||||||
|
parentId: String,
|
||||||
|
result: Result<MutableList<MediaItem>>,
|
||||||
|
options: Bundle
|
||||||
|
) {
|
||||||
|
val maximumRootChildLimit = getRootChildrenLimit()
|
||||||
|
musicFragment.getChildren(parentId, maximumRootChildLimit, result, options.getPage())
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onSearch(query: String, extras: Bundle?, result: Result<MutableList<MediaItem>>) {
|
override fun onSearch(query: String, extras: Bundle?, result: Result<MutableList<MediaItem>>) {
|
||||||
musicFragment.search(query, result)
|
musicFragment.search(query, result, extras?.getPage())
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getRootChildrenLimit(): Int {
|
||||||
|
return browserRootHints?.getInt(MediaConstants.BROWSER_ROOT_HINTS_KEY_ROOT_CHILDREN_LIMIT, 4)
|
||||||
|
?: 4
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun Bundle.getPage(): MusicServiceFragment.Page? {
|
||||||
|
val page = getInt(MediaBrowserCompat.EXTRA_PAGE, -1).takeIf { it >= 0 } ?: return null
|
||||||
|
val pageSize = getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1).takeIf { it > 0 } ?: return null
|
||||||
|
return MusicServiceFragment.Page(page, pageSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun updateForeground(change: ForegroundListener.Change) {
|
override fun updateForeground(change: ForegroundListener.Change) {
|
||||||
|
|
|
@ -49,6 +49,8 @@ constructor(
|
||||||
private val dispatchJob = Job()
|
private val dispatchJob = Job()
|
||||||
private val dispatchScope = CoroutineScope(dispatchJob + Dispatchers.Default)
|
private val dispatchScope = CoroutineScope(dispatchJob + Dispatchers.Default)
|
||||||
|
|
||||||
|
data class Page(val num: Int, val size: Int)
|
||||||
|
|
||||||
class Factory
|
class Factory
|
||||||
@Inject
|
@Inject
|
||||||
constructor(
|
constructor(
|
||||||
|
@ -108,11 +110,12 @@ constructor(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getChildren(mediaId: String, maxTabs: Int, result: Result<MutableList<MediaItem>>) =
|
fun getChildren(mediaId: String, maxTabs: Int, result: Result<MutableList<MediaItem>>, page: Page?) =
|
||||||
result.dispatch { musicBrowser.getChildren(mediaId, maxTabs)?.toMutableList() }
|
result.dispatch { musicBrowser.getChildren(mediaId, maxTabs)?.expose(page) }
|
||||||
|
|
||||||
fun search(query: String, result: Result<MutableList<MediaItem>>) =
|
|
||||||
result.dispatchAsync { musicBrowser.search(query) }
|
fun search(query: String, result: Result<MutableList<MediaItem>>, page: Page?) =
|
||||||
|
result.dispatchAsync { musicBrowser.search(query).expose(page) }
|
||||||
|
|
||||||
private fun <T> Result<T>.dispatch(body: () -> T?) {
|
private fun <T> Result<T>.dispatch(body: () -> T?) {
|
||||||
try {
|
try {
|
||||||
|
@ -142,4 +145,15 @@ constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun <T> List<T>.expose(page: Page?): MutableList<T> {
|
||||||
|
if (page == null) return toMutableList()
|
||||||
|
val start = page.num * page.size
|
||||||
|
val end = start + page.size
|
||||||
|
return if (start >= size) {
|
||||||
|
mutableListOf()
|
||||||
|
} else {
|
||||||
|
subList(start, end.coerceAtMost(size)).toMutableList()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue