Skip to content

Commit

Permalink
preserve old rendering path for non-suspenable assets
Browse files Browse the repository at this point in the history
  • Loading branch information
sugarmanz committed Mar 6, 2024
1 parent bc01b43 commit ac38f48
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext

/**
Expand Down Expand Up @@ -145,26 +146,31 @@ public abstract class PlayerFragment : Fragment(), ManagedPlayerState.Listener {
/** Default suspendable implementation of [handleAssetUpdate] */
@ExperimentalPlayerApi
protected open suspend fun renderIntoPlayerCanvas(asset: RenderableAsset?, animateTransition: Boolean) {
val startTime = System.currentTimeMillis()
val view = asset?.render(requireContext())?.let {
// unwrap if we know we have an async view stub, and just wait on the actual view
if (it is SuspendableAsset.AsyncViewStub) it.awaitView() else it
}
try {
val startTime = System.currentTimeMillis()
val view = asset?.render(requireContext())?.let {
// unwrap if we know we have an async view stub, and just wait on the actual view
if (it is SuspendableAsset.AsyncViewStub) it.awaitView() else it
}

view?.doOnLayout {
playerViewModel.logRenderTime(asset, System.currentTimeMillis() - startTime)
}
view?.doOnLayout {
playerViewModel.logRenderTime(asset, System.currentTimeMillis() - startTime)
}

// swap to main
withContext(Dispatchers.Main) {
if (asset is RenderableAsset.ViewportAsset) binding.scrollContainer.isFillViewport = true
// swap to main
withContext(Dispatchers.Main) {
if (asset is RenderableAsset.ViewportAsset) binding.scrollContainer.isFillViewport = true

animateTransition
.takeIf { it }
?.let { binding.scrollContainer.scrollTo(0, 0) }
?.let { buildTransitionAnimation() }
?.let { view.transitionInto(binding.playerCanvas, it) }
?: (view into binding.playerCanvas)
animateTransition
.takeIf { it }
?.let { binding.scrollContainer.scrollTo(0, 0) }
?.let { buildTransitionAnimation() }
?.let { view.transitionInto(binding.playerCanvas, it) }
?: (view into binding.playerCanvas)
}
} catch (exception: Exception) {
exception.printStackTrace()
playerViewModel.fail("Error rendering asset", exception)
}
}

Expand All @@ -174,16 +180,16 @@ public abstract class PlayerFragment : Fragment(), ManagedPlayerState.Listener {
* styles and inject that into the view tree.
*/
protected open fun handleAssetUpdate(asset: RenderableAsset?, animateTransition: Boolean) {
lifecycleScope.launch(Dispatchers.Default) {
try {
renderIntoPlayerCanvas(asset, animateTransition)
} catch (exception: Exception) {
exception.printStackTrace()
playerViewModel.fail("Error rendering asset", exception)
}
if (asset is SuspendableAsset<*>) lifecycleScope.launch(Dispatchers.Default) {
renderIntoPlayerCanvas(asset, animateTransition)
} else runBlocking(Dispatchers.Main) {
renderIntoPlayerCanvas(asset, animateTransition)
}
}




public open fun buildTransitionAnimation(): Transition? = null

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public constructor(
runtime.add("player", player)

// we only have access to the logger after we have the player instance
runtime.checkBlockingThread = {
if (runtime.config.debuggable) runtime.checkBlockingThread = {
if (name == "main") {
scope.launch {
logger.warn(
Expand Down

0 comments on commit ac38f48

Please sign in to comment.