-
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.
Add functionality to allow setting/getting/clearing of results in a N…
…avigationFlow or NavigationFlowScope
- Loading branch information
Showing
2 changed files
with
89 additions
and
1 deletion.
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
78 changes: 78 additions & 0 deletions
78
enro-core/src/main/java/dev/enro/core/result/flows/FlowStepActions.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,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)) | ||
} |