-
Notifications
You must be signed in to change notification settings - Fork 61
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
AppyxComponent state not restored after configuration changes #694
Merged
manuel-martos
merged 8 commits into
bumble-tech:2.x
from
manuel-martos:2.x-fix-component-state-not-restored
Apr 12, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cb94a57
Merge branch '2.x' of github.com:manuel-martos/appyx into 2.x
mmartosdev 884b69a
Node initialisation wasn't adding necesary plugins
mmartosdev 482f932
Update CHANGELOG + add test
mmartosdev d4b87a7
Update CHANGELOG
mmartosdev 06e6d4e
Move test to UI
mmartosdev b43149e
Add extra test to make sure saveInstanceState has been invoked
mmartosdev c9cb504
Rollback coroutines version
mmartosdev b54d891
Fix coroutine version
mmartosdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
appyx-navigation/android/src/androidTest/kotlin/com/bumble/appyx/helpers/DummyComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.bumble.appyx.helpers | ||
|
||
import androidx.compose.animation.core.AnimationSpec | ||
import androidx.compose.animation.core.spring | ||
import com.bumble.appyx.helpers.DummyComponentModel.State | ||
import com.bumble.appyx.interactions.gesture.GestureFactory | ||
import com.bumble.appyx.interactions.gesture.GestureSettleConfig | ||
import com.bumble.appyx.interactions.model.BaseAppyxComponent | ||
import com.bumble.appyx.interactions.model.backpresshandlerstrategies.BackPressHandlerStrategy | ||
import com.bumble.appyx.interactions.model.backpresshandlerstrategies.DontHandleBackPress | ||
import com.bumble.appyx.interactions.state.MutableSavedStateMap | ||
import com.bumble.appyx.interactions.ui.Visualisation | ||
import com.bumble.appyx.interactions.ui.context.TransitionBounds | ||
import com.bumble.appyx.interactions.ui.context.UiContext | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
|
||
class DummyComponent<NavTarget : Any>( | ||
scope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main), | ||
val model: DummyComponentModel<NavTarget>, | ||
visualisation: (UiContext) -> Visualisation<NavTarget, State<NavTarget>>, | ||
animationSpec: AnimationSpec<Float> = spring(), | ||
gestureFactory: (TransitionBounds) -> GestureFactory<NavTarget, State<NavTarget>> = { | ||
GestureFactory.Noop() | ||
}, | ||
gestureSettleConfig: GestureSettleConfig = GestureSettleConfig(), | ||
backPressStrategy: BackPressHandlerStrategy<NavTarget, State<NavTarget>> = DontHandleBackPress(), | ||
disableAnimations: Boolean = false, | ||
) : BaseAppyxComponent<NavTarget, State<NavTarget>>( | ||
scope = scope, | ||
model = model, | ||
visualisation = visualisation, | ||
gestureFactory = gestureFactory, | ||
gestureSettleConfig = gestureSettleConfig, | ||
backPressStrategy = backPressStrategy, | ||
defaultAnimationSpec = animationSpec, | ||
disableAnimations = disableAnimations | ||
) { | ||
var saveInstanceStateInvoked: Int = 0 | ||
|
||
fun resetSaveInstanceState() { | ||
saveInstanceStateInvoked = 0 | ||
} | ||
|
||
override fun saveInstanceState(state: MutableSavedStateMap) { | ||
super.saveInstanceState(state) | ||
saveInstanceStateInvoked += 1 | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...navigation/android/src/androidTest/kotlin/com/bumble/appyx/helpers/DummyComponentModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.bumble.appyx.helpers | ||
|
||
import com.bumble.appyx.helpers.DummyComponentModel.State | ||
import com.bumble.appyx.interactions.model.Element | ||
import com.bumble.appyx.interactions.model.asElement | ||
import com.bumble.appyx.interactions.model.transition.BaseTransitionModel | ||
import com.bumble.appyx.utils.multiplatform.Parcelable | ||
import com.bumble.appyx.utils.multiplatform.Parcelize | ||
import com.bumble.appyx.utils.multiplatform.SavedStateMap | ||
|
||
class DummyComponentModel<NavTarget : Any>( | ||
initialTarget: NavTarget, | ||
savedStateMap: SavedStateMap?, | ||
) : BaseTransitionModel<NavTarget, State<NavTarget>>( | ||
savedStateMap = savedStateMap, | ||
) { | ||
@Parcelize | ||
data class State<NavTarget>( | ||
val target: Element<NavTarget> | ||
) : Parcelable | ||
|
||
override val initialState: State<NavTarget> = State(initialTarget.asElement()) | ||
|
||
override fun State<NavTarget>.availableElements(): Set<Element<NavTarget>> = setOf(target) | ||
|
||
override fun State<NavTarget>.removeDestroyedElement(element: Element<NavTarget>): State<NavTarget> = | ||
this | ||
|
||
override fun State<NavTarget>.removeDestroyedElements(): State<NavTarget> = this | ||
|
||
override fun State<NavTarget>.destroyedElements(): Set<Element<NavTarget>> = emptySet() | ||
} |
32 changes: 32 additions & 0 deletions
32
...-navigation/android/src/androidTest/kotlin/com/bumble/appyx/helpers/DummyVisualisation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.bumble.appyx.helpers | ||
|
||
import androidx.compose.animation.core.SpringSpec | ||
import com.bumble.appyx.helpers.DummyComponentModel.State | ||
import com.bumble.appyx.interactions.ui.DefaultAnimationSpec | ||
import com.bumble.appyx.interactions.ui.context.UiContext | ||
import com.bumble.appyx.interactions.ui.state.MatchedTargetUiState | ||
import com.bumble.appyx.transitionmodel.BaseVisualisation | ||
|
||
class DummyVisualisation<NavTarget : Any>( | ||
uiContext: UiContext, | ||
defaultAnimationSpec: SpringSpec<Float> = DefaultAnimationSpec | ||
) : BaseVisualisation<NavTarget, State<NavTarget>, TargetUiState, MutableUiState>( | ||
uiContext = uiContext, | ||
defaultAnimationSpec = defaultAnimationSpec, | ||
) { | ||
override fun State<NavTarget>.toUiTargets(): List<MatchedTargetUiState<NavTarget, TargetUiState>> = | ||
listOf( | ||
MatchedTargetUiState( | ||
element = target, | ||
targetUiState = TargetUiState(0) | ||
) | ||
) | ||
|
||
override fun mutableUiStateFor( | ||
uiContext: UiContext, | ||
targetUiState: TargetUiState | ||
): MutableUiState = | ||
targetUiState.toMutableUiState(uiContext) | ||
|
||
|
||
} |
33 changes: 33 additions & 0 deletions
33
appyx-navigation/android/src/androidTest/kotlin/com/bumble/appyx/helpers/RootNode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.bumble.appyx.helpers | ||
|
||
import androidx.compose.material.Text | ||
import com.bumble.appyx.helpers.RootNode.NavTarget | ||
import com.bumble.appyx.navigation.modality.NodeContext | ||
import com.bumble.appyx.navigation.node.Node | ||
import com.bumble.appyx.navigation.node.node | ||
|
||
class RootNode( | ||
nodeContext: NodeContext, | ||
appyxComponent: DummyComponent<NavTarget> = DummyComponent( | ||
model = DummyComponentModel( | ||
initialTarget = NavTarget.Child1, | ||
savedStateMap = nodeContext.savedStateMap | ||
), | ||
visualisation = { DummyVisualisation(it) } | ||
) | ||
) : Node<NavTarget>( | ||
nodeContext = nodeContext, | ||
appyxComponent = appyxComponent, | ||
) { | ||
sealed interface NavTarget { | ||
data object Child1 : NavTarget | ||
data object Child2 : NavTarget | ||
} | ||
|
||
override fun buildChildNode(navTarget: NavTarget, nodeContext: NodeContext): Node<*> = | ||
when (navTarget) { | ||
is NavTarget.Child1 -> node(nodeContext) { Text("Child 1") } | ||
is NavTarget.Child2 -> node(nodeContext) { Text("Child 2") } | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
appyx-navigation/android/src/androidTest/kotlin/com/bumble/appyx/helpers/TargetUiState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.bumble.appyx.helpers | ||
|
||
import androidx.compose.animation.core.SpringSpec | ||
import androidx.compose.ui.Modifier | ||
import com.bumble.appyx.interactions.ui.context.UiContext | ||
import com.bumble.appyx.interactions.ui.state.BaseMutableUiState | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
class TargetUiState( | ||
val id: Int, | ||
) | ||
|
||
class MutableUiState( | ||
uiContext: UiContext, | ||
val id: Int, | ||
) : BaseMutableUiState<TargetUiState>( | ||
uiContext, emptyList() | ||
) { | ||
override val combinedMotionPropertyModifier: Modifier = Modifier | ||
|
||
override suspend fun snapTo(target: TargetUiState) = Unit | ||
|
||
override fun lerpTo( | ||
scope: CoroutineScope, | ||
start: TargetUiState, | ||
end: TargetUiState, | ||
fraction: Float | ||
) = Unit | ||
|
||
override suspend fun animateTo( | ||
scope: CoroutineScope, | ||
target: TargetUiState, | ||
springSpec: SpringSpec<Float> | ||
) = Unit | ||
} | ||
|
||
fun TargetUiState.toMutableUiState(uiContext: UiContext): MutableUiState = | ||
MutableUiState( | ||
uiContext = uiContext, | ||
id = id, | ||
) |
50 changes: 50 additions & 0 deletions
50
appyx-navigation/android/src/androidTest/kotlin/com/bumble/appyx/navigation/node/NodeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.bumble.appyx.navigation.node | ||
|
||
import com.bumble.appyx.helpers.DummyComponent | ||
import com.bumble.appyx.helpers.DummyComponentModel | ||
import com.bumble.appyx.helpers.DummyVisualisation | ||
import com.bumble.appyx.helpers.RootNode | ||
import com.bumble.appyx.navigation.AppyxTestScenario | ||
import com.bumble.appyx.navigation.modality.NodeContext | ||
import org.junit.Assert.assertNotEquals | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class NodeTest { | ||
private var appyxComponent: DummyComponent<RootNode.NavTarget>? = null | ||
|
||
private val nodeFactory: (nodeContext: NodeContext) -> Node<*> = { nodeContext -> | ||
appyxComponent = DummyComponent( | ||
model = DummyComponentModel( | ||
initialTarget = RootNode.NavTarget.Child1, | ||
savedStateMap = nodeContext.savedStateMap | ||
), | ||
visualisation = { DummyVisualisation(it) } | ||
) | ||
RootNode(nodeContext = nodeContext, appyxComponent = appyxComponent!!) | ||
} | ||
|
||
@get:Rule | ||
val rule = AppyxTestScenario { nodeContext -> | ||
nodeFactory(nodeContext) | ||
} | ||
|
||
@Test | ||
fun WHEN_node_is_create_THEN_plugins_are_setup_as_expected() { | ||
rule.start() | ||
assertNotEquals("Node should have some predefined plugins", 0, rule.node.plugins.size) | ||
} | ||
|
||
@Test | ||
fun WHEN_node_is_create_THEN_appyx_component_state_is_saved_during_recreation() { | ||
rule.start() | ||
appyxComponent!!.resetSaveInstanceState() | ||
rule.activityScenario.recreate() | ||
assertNotEquals( | ||
"AppyxComponent state should be saved", | ||
0, | ||
appyxComponent!!.saveInstanceStateInvoked | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lateinit var can be used