mirror of
https://github.com/sudoxnym/audiobookshelf-atv.git
synced 2026-04-14 19:46:30 +00:00
Merge pull request #1675 from laurisvr/remove-busy-loops
Remove busy loops
This commit is contained in:
commit
48a299de45
2 changed files with 65 additions and 25 deletions
|
|
@ -15,6 +15,7 @@ import org.json.JSONException
|
|||
import org.json.JSONObject
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
class MediaManager(private var apiHandler: ApiHandler, var ctx: Context) {
|
||||
val tag = "MediaManager"
|
||||
|
|
@ -175,21 +176,20 @@ class MediaManager(private var apiHandler: ApiHandler, var ctx: Context) {
|
|||
* Load personalized shelves from server for all libraries.
|
||||
* [cb] resolves when all libraries are processed
|
||||
*/
|
||||
fun populatePersonalizedDataForAllLibraries(cb: () -> Unit ) {
|
||||
serverLibraries.forEach {
|
||||
libraryPersonalizationsDone++
|
||||
Log.d(tag, "Loading personalization for library ${it.name} - ${it.id} - ${it.mediaType}")
|
||||
populatePersonalizedDataForLibrary(it.id) {
|
||||
Log.d(tag, "Loaded personalization for library ${it.name} - ${it.id} - ${it.mediaType}")
|
||||
libraryPersonalizationsDone--
|
||||
fun populatePersonalizedDataForAllLibraries(cb: () -> Unit) {
|
||||
val remaining = AtomicInteger(serverLibraries.size)
|
||||
|
||||
serverLibraries.forEach { lib ->
|
||||
Log.d(tag, "Loading personalization for library ${lib.name}")
|
||||
populatePersonalizedDataForLibrary(lib.id) {
|
||||
Log.d(tag, "Loaded personalization for library ${lib.name}")
|
||||
if (remaining.decrementAndGet() == 0) {
|
||||
Log.d(tag, "Finished loading all library personalization data")
|
||||
allLibraryPersonalizationsDone = true
|
||||
cb()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (libraryPersonalizationsDone > 0) { }
|
||||
|
||||
Log.d(tag, "Finished loading all library personalization data")
|
||||
allLibraryPersonalizationsDone = true
|
||||
cb()
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1087,6 +1087,26 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
|
|||
private val DOWNLOADS_ROOT = "__DOWNLOADS__"
|
||||
private val CONTINUE_ROOT = "__CONTINUE__"
|
||||
private lateinit var browseTree: BrowseTree
|
||||
private val browseTreeInitListeners = mutableListOf<() -> Unit>()
|
||||
|
||||
private fun waitForBrowseTree(cb: () -> Unit)
|
||||
{
|
||||
if (this::browseTree.isInitialized)
|
||||
{
|
||||
cb()
|
||||
}
|
||||
else
|
||||
{
|
||||
browseTreeInitListeners += cb
|
||||
}
|
||||
}
|
||||
|
||||
private fun onBrowseTreeInitialized()
|
||||
{
|
||||
// Called after browseTree is assigned for the first time
|
||||
browseTreeInitListeners.forEach { it.invoke() }
|
||||
browseTreeInitListeners.clear()
|
||||
}
|
||||
|
||||
// Only allowing android auto or similar to access media browser service
|
||||
// normal loading of audiobooks is handled in webview (not natively)
|
||||
|
|
@ -1257,6 +1277,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
|
|||
mediaManager.serverLibraries,
|
||||
mediaManager.allLibraryPersonalizationsDone
|
||||
)
|
||||
onBrowseTreeInitialized()
|
||||
val children =
|
||||
browseTree[parentMediaId]?.map { item ->
|
||||
Log.d(tag, "Found top menu item: ${item.description.title}")
|
||||
|
|
@ -1291,6 +1312,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
|
|||
mediaManager.serverLibraries,
|
||||
mediaManager.allLibraryPersonalizationsDone
|
||||
)
|
||||
onBrowseTreeInitialized()
|
||||
val children =
|
||||
browseTree[parentMediaId]?.map { item ->
|
||||
Log.d(tag, "Found top menu item: ${item.description.title}")
|
||||
|
|
@ -1303,22 +1325,40 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
|
|||
AbsLogger.info(tag, "onLoadChildren: Android auto data loaded")
|
||||
result.sendResult(children as MutableList<MediaBrowserCompat.MediaItem>?)
|
||||
}
|
||||
} else if (parentMediaId == LIBRARIES_ROOT || parentMediaId == RECENTLY_ROOT) {
|
||||
} else if (parentMediaId == LIBRARIES_ROOT || parentMediaId == RECENTLY_ROOT)
|
||||
{
|
||||
Log.d(tag, "First load done: $firstLoadDone")
|
||||
if (!firstLoadDone) {
|
||||
if (!firstLoadDone)
|
||||
{
|
||||
result.sendResult(null)
|
||||
return
|
||||
}
|
||||
// Wait until top-menu is initialized
|
||||
while (!this::browseTree.isInitialized) {}
|
||||
val children =
|
||||
browseTree[parentMediaId]?.map { item ->
|
||||
Log.d(tag, "[MENU: $parentMediaId] Showing list item ${item.description.title}")
|
||||
MediaBrowserCompat.MediaItem(
|
||||
item.description,
|
||||
MediaBrowserCompat.MediaItem.FLAG_BROWSABLE
|
||||
)
|
||||
}
|
||||
|
||||
if (!this::browseTree.isInitialized)
|
||||
{
|
||||
// ✅ good: detach and wait for init
|
||||
result.detach()
|
||||
waitForBrowseTree {
|
||||
val children = browseTree[parentMediaId]?.map { item ->
|
||||
Log.d(tag, "[MENU: $parentMediaId] Showing list item ${item.description.title}")
|
||||
MediaBrowserCompat.MediaItem(
|
||||
item.description,
|
||||
MediaBrowserCompat.MediaItem.FLAG_BROWSABLE
|
||||
)
|
||||
}
|
||||
result.sendResult(children as MutableList<MediaBrowserCompat.MediaItem>?)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Already initialized: just return
|
||||
val children = browseTree[parentMediaId]?.map { item ->
|
||||
Log.d(tag, "[MENU: $parentMediaId] Showing list item ${item.description.title}")
|
||||
MediaBrowserCompat.MediaItem(
|
||||
item.description,
|
||||
MediaBrowserCompat.MediaItem.FLAG_BROWSABLE
|
||||
)
|
||||
}
|
||||
result.sendResult(children as MutableList<MediaBrowserCompat.MediaItem>?)
|
||||
} else if (mediaManager.getIsLibrary(parentMediaId)) { // Load library items for library
|
||||
Log.d(tag, "Loading items for library $parentMediaId")
|
||||
|
|
|
|||
Loading…
Reference in a new issue