-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4b097d
commit 6d289e9
Showing
3 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
22 changes: 20 additions & 2 deletions
22
compose/common/src/dev/programadorthi/routing/compose/ComposeRoutingExt.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 |
---|---|---|
@@ -1,10 +1,28 @@ | ||
package dev.programadorthi.routing.compose | ||
|
||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.ProvidableCompositionLocal | ||
import androidx.compose.runtime.staticCompositionLocalOf | ||
import dev.programadorthi.routing.core.Routing | ||
|
||
public val LocalPopResult: ProvidableCompositionLocal<ComposeRoutingPopResult?> = | ||
staticCompositionLocalOf { null } | ||
|
||
public fun Routing.canPop(): Boolean = contentList.size > 1 | ||
|
||
public fun Routing.pop() { | ||
public fun Routing.pop(result: Any? = null) { | ||
contentList.removeLastOrNull() | ||
// TODO: add support to send result | ||
if (result == null) return | ||
|
||
val lastIndex = contentList.lastIndex | ||
if (lastIndex < 0) return | ||
|
||
val lastContent = contentList[lastIndex] | ||
val popResult = ComposeRoutingPopResult(content = result) | ||
|
||
contentList[lastIndex] = { | ||
CompositionLocalProvider(LocalPopResult provides popResult) { | ||
lastContent() | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
compose/common/src/dev/programadorthi/routing/compose/ComposeRoutingPopResult.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,11 @@ | ||
package dev.programadorthi.routing.compose | ||
|
||
/** | ||
* Add support to a previous [androidx.compose.runtime.Composable] get the popped result | ||
*/ | ||
@Suppress("UNCHECKED_CAST") | ||
public class ComposeRoutingPopResult internal constructor( | ||
internal val content: Any? = null, | ||
) { | ||
public fun <T> result(): T? = content as? T | ||
} |
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