-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
EmbeddedNavigationDestination
as an experimental API, which a…
…llows a `NavigationKey.SupportsPush` to be rendered as an embedded destination within another Composable.
- Loading branch information
Showing
3 changed files
with
180 additions
and
0 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
69 changes: 69 additions & 0 deletions
69
enro-core/src/main/java/dev/enro/destination/compose/EmbeddedNavigationDestination.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,69 @@ | ||
package dev.enro.destination.compose | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.rememberUpdatedState | ||
import androidx.compose.ui.Modifier | ||
import dev.enro.annotations.ExperimentalEnroApi | ||
import dev.enro.core.NavigationKey | ||
import dev.enro.core.compose.rememberNavigationContainer | ||
import dev.enro.core.container.EmptyBehavior | ||
import dev.enro.core.container.acceptNone | ||
|
||
@Composable | ||
@ExperimentalEnroApi | ||
public fun EmbeddedNavigationDestination( | ||
navigationKey: NavigationKey.SupportsPush, | ||
onClosed: (() -> Unit), | ||
modifier: Modifier = Modifier, | ||
) { | ||
val rememberedOnClosed = rememberUpdatedState(onClosed) | ||
val container = rememberNavigationContainer( | ||
root = navigationKey, | ||
emptyBehavior = EmptyBehavior.CloseParent, | ||
filter = acceptNone(), | ||
interceptor = { | ||
onClosed<NavigationKey> { | ||
if (it != navigationKey) return@onClosed continueWithClose() | ||
rememberedOnClosed.value.invoke() | ||
cancelClose() | ||
} | ||
} | ||
) | ||
Box(modifier = modifier) { | ||
container.Render() | ||
} | ||
} | ||
|
||
@Composable | ||
@ExperimentalEnroApi | ||
public inline fun <reified T: Any> EmbeddedNavigationDestination( | ||
navigationKey: NavigationKey.SupportsPush.WithResult<T>, | ||
noinline onClosed: (() -> Unit), | ||
modifier: Modifier = Modifier, | ||
noinline onResult: (T) -> Unit = {}, | ||
) { | ||
val rememberedOnClosed = rememberUpdatedState(onClosed) | ||
val rememberedOnResult = rememberUpdatedState(onResult) | ||
|
||
val container = rememberNavigationContainer( | ||
emptyBehavior = EmptyBehavior.CloseParent, | ||
root = navigationKey, | ||
filter = acceptNone(), | ||
interceptor = { | ||
onClosed<NavigationKey> { | ||
if (it != navigationKey) return@onClosed continueWithClose() | ||
rememberedOnClosed.value.invoke() | ||
cancelClose() | ||
} | ||
onResult<NavigationKey.WithResult<T>, T> { key, result -> | ||
if (key != navigationKey) return@onResult continueWithClose() | ||
rememberedOnResult.value.invoke(result) | ||
cancelResult() | ||
} | ||
} | ||
) | ||
Box(modifier = modifier) { | ||
container.Render() | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
tests/application/src/main/java/dev/enro/tests/application/compose/EmbeddedDestination.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,110 @@ | ||
package dev.enro.tests.application.compose | ||
|
||
import androidx.compose.animation.Crossfade | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import dev.enro.annotations.ExperimentalEnroApi | ||
import dev.enro.annotations.NavigationDestination | ||
import dev.enro.core.NavigationKey | ||
import dev.enro.core.close | ||
import dev.enro.core.closeWithResult | ||
import dev.enro.core.compose.navigationHandle | ||
import dev.enro.destination.compose.EmbeddedNavigationDestination | ||
import dev.enro.tests.application.compose.common.TitledColumn | ||
import kotlinx.parcelize.Parcelize | ||
|
||
@Parcelize | ||
object EmbeddedDestination : NavigationKey.SupportsPush { | ||
@Parcelize | ||
internal data object NoResult: NavigationKey.SupportsPush | ||
|
||
@Parcelize | ||
internal data object WithResult: NavigationKey.SupportsPush.WithResult<String> | ||
} | ||
|
||
@OptIn(ExperimentalEnroApi::class) | ||
@Composable | ||
@NavigationDestination(EmbeddedDestination::class) | ||
fun EmbeddedDestination() { | ||
val navigation = navigationHandle() | ||
var withResult by remember { | ||
mutableStateOf(false) | ||
} | ||
|
||
var lastResult by remember { | ||
mutableStateOf("(None)") | ||
} | ||
|
||
TitledColumn(title = "Embedded Destination") { | ||
|
||
Text(text = "Last result: $lastResult") | ||
|
||
Button(onClick = { withResult = !withResult }) { | ||
Text(text = "Toggle Result") | ||
} | ||
|
||
Crossfade( | ||
modifier = Modifier.weight(1f), | ||
targetState = withResult | ||
) { withResult -> | ||
if (withResult) { | ||
EmbeddedNavigationDestination( | ||
navigationKey = EmbeddedDestination.WithResult, | ||
modifier = Modifier.fillMaxSize(), | ||
onClosed = { | ||
navigation.close() | ||
}, | ||
onResult = { | ||
lastResult = it | ||
} | ||
) | ||
} | ||
else { | ||
EmbeddedNavigationDestination( | ||
navigationKey = EmbeddedDestination.NoResult, | ||
onClosed = { | ||
navigation.close() | ||
}, | ||
modifier = Modifier.fillMaxSize(), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
@NavigationDestination(EmbeddedDestination.NoResult::class) | ||
fun EmbeddedDestinationNoResult() { | ||
val navigation = navigationHandle() | ||
TitledColumn(title = "No Result Destination") { | ||
Text(text = "This destination has no result") | ||
Button(onClick = { navigation.close() }) { | ||
Text(text = "Close") | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
@NavigationDestination(EmbeddedDestination.WithResult::class) | ||
fun EmbeddedDestinationWithResult() { | ||
val navigation = navigationHandle<EmbeddedDestination.WithResult>() | ||
TitledColumn(title = "No Result Destination") { | ||
Text(text = "This destination can return results") | ||
Button(onClick = { navigation.closeWithResult("A") }) { | ||
Text(text = "Send Result (A)") | ||
} | ||
Button(onClick = { navigation.closeWithResult("B") }) { | ||
Text(text = "Send Result (B)") | ||
} | ||
Button(onClick = { navigation.close() }) { | ||
Text(text = "Close") | ||
} | ||
} | ||
} |