TP-34874 | lottie and pager optimisations (#105)
This commit is contained in:
committed by
GitHub
parent
bf9bc4c348
commit
dcd8d8d60a
@@ -16,6 +16,7 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.layout.layoutId
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.airbnb.lottie.LottieComposition
|
||||
import com.airbnb.lottie.compose.LottieAnimation
|
||||
import com.airbnb.lottie.compose.LottieCompositionSpec
|
||||
import com.airbnb.lottie.compose.animateLottieCompositionAsState
|
||||
@@ -49,16 +50,9 @@ class LottieRenderer : Renderer<LottieProperty> {
|
||||
|
||||
val spec = LottieCompositionSpec.Url(url = uiTronLottieData?.lottieUrl.orEmpty())
|
||||
val composition by rememberLottieComposition(spec)
|
||||
val progress by animateLottieCompositionAsState(composition)
|
||||
if (progress == 1.0f && uiTronLottieData?.onAnimationEnd != null) {
|
||||
LaunchedEffect(Unit) {
|
||||
uiTronViewModel.handleActions(uiTronLottieData?.onAnimationEnd)
|
||||
}
|
||||
} else if (progress > 0 && uiTronLottieData?.onAnimationStart != null) {
|
||||
LaunchedEffect(Unit) {
|
||||
uiTronViewModel.handleActions(uiTronLottieData?.onAnimationStart)
|
||||
}
|
||||
}
|
||||
|
||||
ObserveProgress(composition, uiTronLottieData, uiTronViewModel)
|
||||
|
||||
if (property.isStateFul.orFalse()) {
|
||||
val state = uiTronViewModel.handle.getStateFlow<String?>(
|
||||
property.getPropertyId(),
|
||||
@@ -121,4 +115,19 @@ class LottieRenderer : Renderer<LottieProperty> {
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ObserveProgress(composition: LottieComposition?, uiTronLottieData: LottieData?, uiTronViewModel: UiTronViewModel) {
|
||||
val progress by animateLottieCompositionAsState(composition)
|
||||
if (progress == 1.0f && uiTronLottieData?.onAnimationEnd != null) {
|
||||
LaunchedEffect(Unit) {
|
||||
uiTronViewModel.handleActions(uiTronLottieData.onAnimationEnd)
|
||||
}
|
||||
} else if (progress > 0 && uiTronLottieData?.onAnimationStart != null) {
|
||||
LaunchedEffect(Unit) {
|
||||
uiTronViewModel.handleActions(uiTronLottieData.onAnimationStart)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import androidx.compose.ui.unit.dp
|
||||
import androidx.constraintlayout.compose.layoutId
|
||||
import com.google.accompanist.pager.ExperimentalPagerApi
|
||||
import com.google.accompanist.pager.HorizontalPager
|
||||
import com.google.accompanist.pager.PagerState
|
||||
import com.google.accompanist.pager.VerticalPager
|
||||
import com.google.accompanist.pager.rememberPagerState
|
||||
import com.navi.uitron.model.data.PagerData
|
||||
@@ -161,25 +162,7 @@ class PagerRenderer(
|
||||
}
|
||||
}
|
||||
}
|
||||
LaunchedEffect(key1 = pagerState.currentPage) {
|
||||
launch {
|
||||
snapshotFlow { pagerState.currentPage }.collect { page ->
|
||||
uiTronViewModel.handleActions(pagerData?.pageChangeActions?.get(page))
|
||||
}
|
||||
}
|
||||
launch {
|
||||
if (property.scrollDelay.orZero() > 0) {
|
||||
delay(property.scrollDelay.orZero())
|
||||
with(pagerState) {
|
||||
val target = if (currentPage < pageCount - 1) currentPage + 1 else 0
|
||||
animateScrollToPage(
|
||||
page = target,
|
||||
pageOffset = 0.0f
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ObservePageChange(pagerState, property, uiTronViewModel, pagerData)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,4 +197,28 @@ class PagerRenderer(
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ObservePageChange(pagerState: PagerState, property: PagerProperty, uiTronViewModel: UiTronViewModel, pagerData: PagerData?){
|
||||
LaunchedEffect(key1 = pagerState.currentPage) {
|
||||
launch {
|
||||
snapshotFlow { pagerState.currentPage }.collect { page ->
|
||||
uiTronViewModel.handleActions(pagerData?.pageChangeActions?.get(page))
|
||||
}
|
||||
}
|
||||
launch {
|
||||
if (property.scrollDelay.orZero() > 0) {
|
||||
delay(property.scrollDelay.orZero())
|
||||
with(pagerState) {
|
||||
val target = if (currentPage < pageCount - 1) currentPage + 1 else 0
|
||||
animateScrollToPage(
|
||||
page = target,
|
||||
pageOffset = 0.0f
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user