Skip to content

Commit

Permalink
Updated NavigationFlowScope to use a "FlowStepLambda" anonymous class…
Browse files Browse the repository at this point in the history
… for lambda identification.
  • Loading branch information
isaac-udy committed Oct 23, 2024
1 parent eaaa631 commit 08b89d2
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import java.io.Serializable

public open class NavigationFlowScope internal constructor(
@PublishedApi
Expand All @@ -27,7 +28,11 @@ public open class NavigationFlowScope internal constructor(
noinline block: FlowStepBuilderScope<T>.() -> NavigationKey.SupportsPush.WithResult<T>,
): T = step(
direction = NavigationDirection.Push,
block = block,
block = object : FlowStepLambda<T> {
override fun FlowStepBuilderScope<T>.invoke(): NavigationKey.WithResult<T> {
return block()
}
},
)

public inline fun <reified T : Any> pushWithExtras(
Expand All @@ -41,7 +46,11 @@ public open class NavigationFlowScope internal constructor(
noinline block: FlowStepBuilderScope<T>.() -> NavigationKey.SupportsPresent.WithResult<T>,
): T = step(
direction = NavigationDirection.Present,
block = block,
block = object : FlowStepLambda<T> {
override fun FlowStepBuilderScope<T>.invoke(): NavigationKey.WithResult<T> {
return block()
}
},
)

public inline fun <reified T : Any> presentWithExtras(
Expand Down Expand Up @@ -135,12 +144,12 @@ public open class NavigationFlowScope internal constructor(
@PublishedApi
internal inline fun <reified T: Any> step(
direction: NavigationDirection,
noinline block: FlowStepBuilderScope<T>.() -> NavigationKey.WithResult<T>,
block: FlowStepLambda<T>,
) : T {
val baseId = block::class.java.name
val count = steps.count { it.stepId.startsWith(baseId) }
val builder = FlowStepBuilder<T>()
val key = builder.scope.run(block)
val key = block.run { builder.scope.invoke() }
val step = builder.build(
stepId = "$baseId@$count",
navigationKey = key,
Expand Down Expand Up @@ -189,3 +198,12 @@ public open class NavigationFlowScope internal constructor(
@PublishedApi
internal class Escape : RuntimeException()
}


// TODO create a re-usable identifiable lambda class for more than just flow steps
@PublishedApi
internal interface FlowStepLambda<T: Any> : Serializable {
fun FlowStepBuilderScope<T>.invoke(): NavigationKey.WithResult<T>
}


0 comments on commit 08b89d2

Please sign in to comment.