Skip to content

Commit

Permalink
Add functionality to allow setting/getting/clearing of results in a N…
Browse files Browse the repository at this point in the history
…avigationFlow or NavigationFlowScope
  • Loading branch information
isaac-udy committed Nov 22, 2023
1 parent 2e5efc3 commit c148162
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -108,6 +112,12 @@ public class NavigationFlow<T> internal constructor(
}
}

@PublishedApi
internal fun getSteps(): List<FlowStep<out Any>> = 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"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package dev.enro.core.result.flows

import dev.enro.annotations.AdvancedEnroApi
import dev.enro.core.NavigationKey

@AdvancedEnroApi
public class FlowStepActions<T: NavigationKey.WithResult<*>>(
private val resultManager: FlowResultManager,
private val step: FlowStep<out Any>
) {
private fun setResultUnsafe(result: Any) {
@Suppress("UNCHECKED_CAST")
resultManager
.set(step as FlowStep<Any>, result)
}

private fun getResultUnsafe(): Any? {
return resultManager
.get(step)
}

public fun clearResult() {
resultManager
.clear(step)
}

public companion object {
public fun <R : Any> FlowStepActions<out NavigationKey.WithResult<in R>>.setResult(result: R) {
setResultUnsafe(result)
}

public fun <R : Any> FlowStepActions<out NavigationKey.WithResult<out R>>.getResult(): R? {
val result = getResultUnsafe() ?: return null
@Suppress("UNCHECKED_CAST")
return result as R
}
}
}

@AdvancedEnroApi
public inline fun <reified T : NavigationKey.WithResult<*>> NavigationFlow<*>.getStep(
block: (T) -> Boolean = { true }
) : FlowStepActions<T>? {
return getSteps()
.firstOrNull {
it.key is T && block(it.key)
}
?.let {
FlowStepActions(getResultManager(), it)
}
}

@AdvancedEnroApi
public inline fun <reified T : NavigationKey.WithResult<*>> NavigationFlow<*>.requireStep(
block: (T) -> Boolean = { true }
) : FlowStepActions<T> {
return requireNotNull(getStep(block))
}

@AdvancedEnroApi
public inline fun <reified T : NavigationKey.WithResult<*>> NavigationFlowScope.getStep(
block: (T) -> Boolean = { true }
) : FlowStepActions<T>? {
return steps
.firstOrNull {
it.key is T && block(it.key)
}
?.let {
FlowStepActions(resultManager, it)
}
}

@AdvancedEnroApi
public inline fun <reified T : NavigationKey.WithResult<*>> NavigationFlowScope.requireStep(
block: (T) -> Boolean = { true }
) : FlowStepActions<T> {
return requireNotNull(getStep(block))
}

0 comments on commit c148162

Please sign in to comment.