Skip to content

Instantly share code, notes, and snippets.

@brianwernick
Created October 2, 2023 14:59
Show Gist options
  • Save brianwernick/0a0e276bd072252b2c7a80f90ce41e43 to your computer and use it in GitHub Desktop.
Save brianwernick/0a0e276bd072252b2c7a80f90ce41e43 to your computer and use it in GitHub Desktop.
// This Gist is a simple setup repro for https://github.com/rive-app/rive-android/issues/306
// using Rive-Android 8.3.0 ("app.rive:rive-android:8.3.0")
/**
* Displays [Rive](https://rive.app/) animations
*
* NOTE:
* Rive native support for Compose is tracked at https://github.com/rive-app/rive-android/issues/259
*/
@Composable
fun RiveImage(
@ResId animationRessourceId: Int,
modifier: Modifier = Modifier,
stateController: RiveStateController? = null,
) {
// We wrap the AndroidView in a Box to correct positioning, for some reason when it was just
// the AndroidView it wouldn't center the RiveAnimationView as expected (slightly lower vertically)
Box(modifier = modifier) {
AndroidView(
factory = { context ->
RiveAnimationView(context)
},
modifier = Modifier.align(Alignment.Center)
) { animationView ->
animationView.setRiveResource(animationResourceId)
}
}
}
@Composable
fun LoadingContent(
loading: State<Boolean>,
modifier: Modifier = Modifier,
content: @Composable () -> Unit
) {
Box(modifier = modifier) {
content()
AnimatedVisibility(
visible = loading.value,
) {
RiveImage(
animation = RiveAnimation.Resource(R.raw.tangrams),
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colors.surface)
.contentDescription("Loading...")
)
}
}
}
@umberto-sonnino
Copy link

@umberto-sonnino The code above is essentially what we have implemented in production that has the crashes; which to echo what @marcus-hobbs has said, has been pretty difficult to reproduce locally (I've encountered it a few times on a Pixel 7 emulator during development in the surrounding code).

That said, the code above is a simplification of our actual usage that focuses on the Rive specific implementation. In our app the LoadingContent is in a slightly more complex scenario since the screen this is contained by is under a jetpack navigation (compose) graph and a scaffold so there are more upstream transitions occurring. So it is possible that this crash occurs during a recomposition or potentially a canceled composition.

@brianwernick Mmh yeah, I'm wondering if this is tied more to the lifecycle of the View than the example itself. We're trying to build a repro by stress-testing the view lifecycle a bit.

Also, there's a reference to a RiveStateController, but in the code snippet that's not being used. That might be relevant to reproduce the crash. Are you saving/restoring state? If so, could you share those bits too?

@brianwernick
Copy link
Author

The RiveStateController was unintentionally left in the example; it's from a more recent branch that we were adding support for changing the stateMachine, animations, and listeners but it's not currently used or tied-in to the code that we are running in production.

I'll play around and see if we can find any relevant patterns from crash reporting, etc. and update rive-app/rive-android#306 with any information we find.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment