Skip to content

Commit

Permalink
Mention breaking change in the CHANGELOG.md and allow to add keys to …
Browse files Browse the repository at this point in the history
…PermanentNavModel later
  • Loading branch information
KovalevAndrey committed Oct 2, 2023
1 parent c9de405 commit 60eb43f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Pending changes

- [#606](https://github.com/bumble-tech/appyx/pull/606)**Fixed**: Do not create permanentNavModel inside ParentNode. Provide it via constructor to ParentNode
- [#606](https://github.com/bumble-tech/appyx/pull/606)**Breaking change**: Do not create permanentNavModel inside ParentNode. Provide it via constructor to ParentNode

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class PermanentChildTest {
rule.onNode(hasTestTag(TestParentNode.NavTarget::class.java.name)).assertExists()
}

@Test
fun `WHEN_permanent_model_does_not_contain_relevant_nav_key_THEN_permanent_child_is_not_rendered`() {
rule.start()

rule.onNode(hasTestTag(TestParentNode.NavTarget::class.java.name)).assertDoesNotExist()
}

@Test
fun `WHEN_visibility_switched_THEN_permanent_child_is_reused`() {
createPermanentNavModelWithNavKey()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
package com.bumble.appyx.core.composable

import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import com.bumble.appyx.core.mapState
import com.bumble.appyx.core.navigation.model.permanent.PermanentNavModel
import com.bumble.appyx.core.node.Node
import com.bumble.appyx.core.node.ParentNode
import kotlinx.coroutines.flow.SharingStarted

@Composable
fun <NavTarget : Any> ParentNode<NavTarget>.PermanentChild(
permanentNavModel: PermanentNavModel<NavTarget>,
navTarget: NavTarget,
decorator: @Composable (child: ChildRenderer) -> Unit
) {
val child = remember(navTarget, permanentNavModel) {
val scope = rememberCoroutineScope()
val child by remember(navTarget, permanentNavModel) {
permanentNavModel
.elements
.value
.find { it.key.navTarget == navTarget }
?.let { childOrCreate(it.key) }
?: throw IllegalStateException(
"No child found for $navTarget in $permanentNavModel. " +
"Add $navTarget to $permanentNavModel before calling PermanentChild."
)
// use WhileSubscribed or Lazy otherwise desynchronisation issue
.mapState(scope, SharingStarted.WhileSubscribed()) { navElements ->
navElements
.find { it.key.navTarget == navTarget }
?.let { childOrCreate(it.key) }
}
}.collectAsState()

child?.let {
decorator(PermanentChildRender(it.node))
}

decorator(PermanentChildRender(child.node))
}

@Composable
Expand Down

0 comments on commit 60eb43f

Please sign in to comment.