-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #606 from KovalevAndrey/1.x-rework-permanent-child
Do not create permanentNavModel inside ParentNode. Provide it via constructor to ParentNode
- Loading branch information
Showing
8 changed files
with
239 additions
and
101 deletions.
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
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
64 changes: 64 additions & 0 deletions
64
libraries/core/src/main/kotlin/com/bumble/appyx/core/composable/PermanentChild.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,64 @@ | ||
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 scope = rememberCoroutineScope() | ||
val child by remember(navTarget, permanentNavModel) { | ||
permanentNavModel | ||
.elements | ||
// 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)) | ||
} | ||
|
||
} | ||
|
||
@Composable | ||
fun <NavTarget : Any> ParentNode<NavTarget>.PermanentChild( | ||
permanentNavModel: PermanentNavModel<NavTarget>, | ||
navTarget: NavTarget, | ||
) { | ||
PermanentChild(permanentNavModel, navTarget) { child -> child() } | ||
} | ||
|
||
private class PermanentChildRender(private val node: Node) : ChildRenderer { | ||
|
||
@Suppress( | ||
"ComposableNaming" // This wants to be 'Invoke' but that won't work with 'operator'. | ||
) | ||
@Composable | ||
override operator fun invoke(modifier: Modifier) { | ||
node.Compose(modifier) | ||
} | ||
|
||
@Suppress( | ||
"ComposableNaming" // This wants to be 'Invoke' but that won't work with 'operator'. | ||
) | ||
@Composable | ||
override operator fun invoke() { | ||
invoke(modifier = Modifier) | ||
} | ||
} |
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
Oops, something went wrong.