Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regressions: Local UI state is not persisted and flickering #684

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

- [#670](https://github.com/bumble-tech/appyx/pull/670) - Fixes ios lifecycle
- [#673](https://github.com/bumble-tech/appyx/pull/673) – Fix canHandeBackPress typo
- [#671](https://github.com/bumble-tech/appyx/issue/671) – Fix ui state saving issue

### Enhancement

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.SaveableStateHolder
import androidx.compose.runtime.saveable.rememberSaveableStateHolder
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
Expand All @@ -34,12 +35,13 @@ import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.round
import com.bumble.appyx.interactions.model.Element
import com.bumble.appyx.interactions.gesture.GestureReferencePoint
import com.bumble.appyx.interactions.gesture.GestureValidator
import com.bumble.appyx.interactions.gesture.GestureValidator.Companion.defaultValidator
import com.bumble.appyx.interactions.gesture.detectDragGesturesOrCancellation
import com.bumble.appyx.interactions.gesture.onPointerEvent
import com.bumble.appyx.interactions.model.BaseAppyxComponent
import com.bumble.appyx.interactions.model.Element
import com.bumble.appyx.interactions.model.removedElements
import com.bumble.appyx.interactions.ui.LocalBoxScope
import com.bumble.appyx.interactions.ui.LocalMotionProperties
Expand All @@ -48,7 +50,6 @@ import com.bumble.appyx.interactions.ui.context.UiContext
import com.bumble.appyx.interactions.ui.output.ElementUiModel
import com.bumble.appyx.interactions.ui.property.impl.position.PositionAlignment
import com.bumble.appyx.interactions.ui.property.motionPropertyRenderValue
import com.bumble.appyx.interactions.gesture.GestureReferencePoint

private val defaultExtraTouch = 48f.dp

Expand Down Expand Up @@ -126,35 +127,37 @@ fun <InteractionTarget : Any, ModelState : Any> AppyxInteractionsContainer(
key(elementUiModel.element.id) {
val isVisible by elementUiModel.visibleState.collectAsState()
elementUiModel.persistentContainer()
saveableStateHolder.SaveableStateProvider(key = elementUiModel.element) {
if (isVisible) {
CompositionLocalProvider(
LocalMotionProperties provides elementUiModel.motionProperties,
) {
when {
!isGesturesEnabled -> {
Box(modifier = elementUiModel.modifier) {
if (isVisible) {
CompositionLocalProvider(
LocalMotionProperties provides elementUiModel.motionProperties,
) {
when {
!isGesturesEnabled -> {
Box(modifier = elementUiModel.modifier) {
saveableStateHolder.SaveableStateProvider(key = elementUiModel.element) {
elementUi(elementUiModel.element)
}
}
}

gestureRelativeTo == GestureReferencePoint.Element ->
ElementWithGestureTransformedBoundingBox(
appyxComponent = appyxComponent,
containerSize = containerSize,
gestureExtraTouchAreaPx = gestureExtraTouchAreaPx,
gestureValidator = gestureValidator,
elementUiModel = elementUiModel,
elementUi = elementUi
)
gestureRelativeTo == GestureReferencePoint.Element ->
ElementWithGestureTransformedBoundingBox(
saveableStateHolder = saveableStateHolder,
appyxComponent = appyxComponent,
containerSize = containerSize,
gestureExtraTouchAreaPx = gestureExtraTouchAreaPx,
gestureValidator = gestureValidator,
elementUiModel = elementUiModel,
elementUi = elementUi
)

gestureRelativeTo == GestureReferencePoint.Container ->
ElementWithGesture(
appyxComponent = appyxComponent,
elementUiModel = elementUiModel,
elementUi = elementUi
)
}
gestureRelativeTo == GestureReferencePoint.Container ->
ElementWithGesture(
saveableStateHolder = saveableStateHolder,
appyxComponent = appyxComponent,
elementUiModel = elementUiModel,
elementUi = elementUi
)
}
}
}
Expand All @@ -166,6 +169,7 @@ fun <InteractionTarget : Any, ModelState : Any> AppyxInteractionsContainer(

@Composable
private fun <InteractionTarget : Any, ModelState : Any> ElementWithGesture(
saveableStateHolder: SaveableStateHolder,
appyxComponent: BaseAppyxComponent<InteractionTarget, ModelState>,
elementUiModel: ElementUiModel<InteractionTarget>,
elementUi: @Composable BoxScope.(Element<InteractionTarget>) -> Unit
Expand All @@ -192,12 +196,15 @@ private fun <InteractionTarget : Any, ModelState : Any> ElementWithGesture(
}
.then(elementUiModel.modifier)
) {
elementUi(elementUiModel.element)
saveableStateHolder.SaveableStateProvider(key = elementUiModel.element) {
elementUi(elementUiModel.element)
}
}
}

@Composable
private fun <InteractionTarget : Any, ModelState : Any> ElementWithGestureTransformedBoundingBox(
saveableStateHolder: SaveableStateHolder,
appyxComponent: BaseAppyxComponent<InteractionTarget, ModelState>,
containerSize: State<IntSize>,
gestureExtraTouchAreaPx: Float,
Expand Down Expand Up @@ -257,7 +264,9 @@ private fun <InteractionTarget : Any, ModelState : Any> ElementWithGestureTransf
transformedBoundingBox.center - localCenter
}
) {
elementUi(elementUiModel.element)
saveableStateHolder.SaveableStateProvider(key = elementUiModel.element) {
elementUi(elementUiModel.element)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.bumble.appyx.navigation.composable

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.ui.Modifier

@Stable
interface ChildRenderer {

@Suppress("ComposableNaming") // This wants to be 'Invoke' but that won't work with 'operator'.
Expand Down
Loading