Skip to content

Commit

Permalink
improve Empty default things and add realization for NavigationNodeFa…
Browse files Browse the repository at this point in the history
…ctory
  • Loading branch information
InsanusMokrassar committed Oct 2, 2024
1 parent 7dec737 commit d768946
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## 0.5.6

* `Core`:
* Add default config for `Empty` node (just empty config object)
* Add default factory for `Empty` node, based on default config
* Add `NavigationNodeFactory.Typed` default node factory with inline factory fun

## 0.5.5

* `Compose`:
Expand Down
7 changes: 7 additions & 0 deletions core/src/commonMain/kotlin/NavigationNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ abstract class NavigationNode<Config : Base, Base>(
config: T,
id: NavigationNodeId = NavigationNodeId()
) : NavigationNode<T, Base>(id) {
object Config
override val configState: StateFlow<T> = MutableStateFlow(config).asStateFlow()

companion object {
val DefaultFactory = NavigationNodeFactory.Companion.Typed<Any, Config> { chain, config ->
Empty(chain, config)
}
}
}
}
29 changes: 29 additions & 0 deletions core/src/commonMain/kotlin/NavigationNodeFactory.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
package dev.inmo.navigation.core

import kotlin.reflect.KClass

fun interface NavigationNodeFactory<Base> {
fun createNode(
navigationChain: NavigationChain<Base>,
config: Base
): NavigationNode<out Base, Base>?

companion object {
open class Typed<Base : Any, T : Base>(
private val kClass: KClass<T>,
private val block: (navigationChain: NavigationChain<Base>, config: T) -> NavigationNode<T, Base>
) : NavigationNodeFactory<Base> {
override fun createNode(
navigationChain: NavigationChain<Base>,
config: Base
): NavigationNode<out Base, Base>? {
return if (kClass.isInstance(config)) {
block(navigationChain, config as T)
} else {
null
}
}

companion object {
inline operator fun <Base : Any, reified T : Base> invoke(
noinline block: (navigationChain: NavigationChain<Base>, config: T) -> NavigationNode<T, Base>
) = Typed<Base, T>(
kClass = T::class,
block
)
}
}
}
}

0 comments on commit d768946

Please sign in to comment.