diff --git a/enro-core/src/main/java/dev/enro/core/result/flows/FlowResultChannel.kt b/enro-core/src/main/java/dev/enro/core/result/flows/FlowResultChannel.kt index a8798431..c77d5799 100644 --- a/enro-core/src/main/java/dev/enro/core/result/flows/FlowResultChannel.kt +++ b/enro-core/src/main/java/dev/enro/core/result/flows/FlowResultChannel.kt @@ -4,8 +4,12 @@ import android.os.Bundle import androidx.core.os.bundleOf import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel -import dev.enro.core.* +import dev.enro.core.NavigationDirection +import dev.enro.core.NavigationHandle +import dev.enro.core.NavigationInstruction +import dev.enro.core.NavigationKey import dev.enro.core.container.toBackstack +import dev.enro.core.onActiveContainer import dev.enro.core.result.NavigationResultChannel import dev.enro.core.result.internal.ResultChannelImpl import dev.enro.core.result.registerForNavigationResultWithKey @@ -108,6 +112,12 @@ public class NavigationFlow internal constructor( } } + @PublishedApi + internal fun getSteps(): List> = steps + + @PublishedApi + internal fun getResultManager(): FlowResultManager = resultManager + internal companion object { const val IS_PUSHED_IN_FLOW = "NavigationFlow.IS_PUSHED_IN_FLOW" const val STEPS_KEY = "NavigationFlow.STEPS_KEY" diff --git a/enro-core/src/main/java/dev/enro/core/result/flows/FlowStepActions.kt b/enro-core/src/main/java/dev/enro/core/result/flows/FlowStepActions.kt new file mode 100644 index 00000000..3fa960da --- /dev/null +++ b/enro-core/src/main/java/dev/enro/core/result/flows/FlowStepActions.kt @@ -0,0 +1,78 @@ +package dev.enro.core.result.flows + +import dev.enro.annotations.AdvancedEnroApi +import dev.enro.core.NavigationKey + +@AdvancedEnroApi +public class FlowStepActions>( + private val resultManager: FlowResultManager, + private val step: FlowStep +) { + private fun setResultUnsafe(result: Any) { + @Suppress("UNCHECKED_CAST") + resultManager + .set(step as FlowStep, result) + } + + private fun getResultUnsafe(): Any? { + return resultManager + .get(step) + } + + public fun clearResult() { + resultManager + .clear(step) + } + + public companion object { + public fun FlowStepActions>.setResult(result: R) { + setResultUnsafe(result) + } + + public fun FlowStepActions>.getResult(): R? { + val result = getResultUnsafe() ?: return null + @Suppress("UNCHECKED_CAST") + return result as R + } + } +} + +@AdvancedEnroApi +public inline fun > NavigationFlow<*>.getStep( + block: (T) -> Boolean = { true } +) : FlowStepActions? { + return getSteps() + .firstOrNull { + it.key is T && block(it.key) + } + ?.let { + FlowStepActions(getResultManager(), it) + } +} + +@AdvancedEnroApi +public inline fun > NavigationFlow<*>.requireStep( + block: (T) -> Boolean = { true } +) : FlowStepActions { + return requireNotNull(getStep(block)) +} + +@AdvancedEnroApi +public inline fun > NavigationFlowScope.getStep( + block: (T) -> Boolean = { true } +) : FlowStepActions? { + return steps + .firstOrNull { + it.key is T && block(it.key) + } + ?.let { + FlowStepActions(resultManager, it) + } +} + +@AdvancedEnroApi +public inline fun > NavigationFlowScope.requireStep( + block: (T) -> Boolean = { true } +) : FlowStepActions { + return requireNotNull(getStep(block)) +} \ No newline at end of file