Skip to content

Commit

Permalink
add docs for compose common part
Browse files Browse the repository at this point in the history
  • Loading branch information
InsanusMokrassar committed Jun 21, 2024
1 parent 8a42036 commit ea1ebb1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
6 changes: 5 additions & 1 deletion compose/src/commonMain/kotlin/CommonNodeFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import dev.inmo.navigation.core.NavigationNodeFactory
import dev.inmo.navigation.core.configs.NavigationNodeDefaultConfig
import org.koin.core.Koin


/**
* Helper method to get [NavigationNodeFactory] from [Koin]. It uses [getAllDistinct] to get all [NavigationNodeFactory]
* from [Koin] and registering [NavigationNodeFactory] which taking first created [dev.inmo.navigation.core.NavigationNode]
* from factories from [Koin]
*/
fun <Base> Koin.nodeFactory(): NavigationNodeFactory<Base> {
val factories = getAllDistinct<NavigationNodeFactory<Base>>()
return NavigationNodeFactory<Base> { chainHolder, config ->
Expand Down
27 changes: 27 additions & 0 deletions compose/src/commonMain/kotlin/ComposeNavigationExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ import kotlinx.coroutines.flow.dropWhile
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.take

/**
* Collecting [NavigationChain.stackFlow] and always [StartInCompose] only the last element in [NavigationChain.stackFlow]
*/
@Composable
internal fun <Base> NavigationChain<Base>.defaultStackHandling() {
val stack = stackFlow.collectAsState()
stack.value.lastOrNull() ?.StartInCompose()
}

/**
* @param onDismiss Will be called when [this] [NavigationChain] will be removed from its parent. [onDismiss] will
* never be called if [this] [NavigationChain] is the root one
*/
@Composable
internal fun <Base> NavigationChain<Base>.StartInCompose(
onDismiss: (suspend NavigationChain<Base>.() -> Unit)? = null,
Expand All @@ -43,6 +50,9 @@ internal fun <Base> NavigationChain<Base>.StartInCompose(
}
}

/**
* Creates [NavigationChain] in current composition and call [StartInCompose]
*/
@Composable
fun <Base> NavigationNode<*, Base>.NavigationSubChain(
onDismiss: (suspend NavigationChain<Base>.() -> Unit)? = null,
Expand All @@ -51,6 +61,9 @@ fun <Base> NavigationNode<*, Base>.NavigationSubChain(
remember { createEmptySubChain() }.StartInCompose(onDismiss, block)
}

/**
* Trying to get [NavigationNode] using [LocalNavigationNodeProvider] and calling [NavigationSubChain] with it
*/
@Composable
fun <Base> NavigationSubChain(
onDismiss: (suspend NavigationChain<Base>.() -> Unit)? = null,
Expand All @@ -59,6 +72,12 @@ fun <Base> NavigationSubChain(
LocalNavigationNodeProvider<Base>().current.NavigationSubChain(onDismiss, block)
}

/**
* **If [this] is [ComposeNode]** provides [this] with [LocalNavigationNodeProvider] in [CompositionLocalProvider] and
* calls [this] [ComposeNode.drawerState] value invoke
*
* @param onDismiss Will be called when [this] [NavigationNode] will be removed from its [NavigationChain]
*/
@Composable
internal fun <Base> NavigationNode<*, Base>?.StartInCompose(
onDismiss: (suspend NavigationNode<*, Base>.() -> Unit)? = null,
Expand Down Expand Up @@ -88,6 +107,10 @@ internal fun <Base> NavigationNode<*, Base>?.StartInCompose(
}
}

/**
* Trying to create [NavigationNode] in [this] [NavigationChain] and do [StartInCompose] with passing of [onDismiss] in
* this call
*/
@Composable
fun <Base> NavigationChain<Base>.NavigationSubNode(
config: Base,
Expand All @@ -97,6 +120,10 @@ fun <Base> NavigationChain<Base>.NavigationSubNode(
node.StartInCompose(onDismiss)
}

/**
* Trying to get current [NavigationChain] using [LocalNavigationChainProvider] and calls [NavigationSubNode] with
* passing both [config] and [onDismiss]
*/
@Composable
fun <Base> NavigationSubNode(
config: Base,
Expand Down
6 changes: 6 additions & 0 deletions compose/src/commonMain/kotlin/ComposeNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlin.jvm.JvmName

/**
* Provides [onDraw] open function which will be called by the navigation system to draw content in the place it added
*/
abstract class ComposeNode<Config : Base, Base>(
config: Config,
override val chain: NavigationChain<Base>,
Expand All @@ -37,6 +40,9 @@ abstract class ComposeNode<Config : Base, Base>(
@Composable
protected open fun onDraw() {}

/**
* Provides place for [NavigationChain] which placed in [subchainsFlow]
*/
@Composable
protected open fun SubchainsHost(filter: (NavigationChain<Base>) -> Boolean) {
val subchainsState = subchainsFlow.collectAsState()
Expand Down
12 changes: 12 additions & 0 deletions compose/src/commonMain/kotlin/InitNavigation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ import dev.inmo.navigation.core.repo.enableSavingHierarchy
import dev.inmo.navigation.core.repo.restoreHierarchy
import kotlinx.coroutines.CoroutineScope

/**
* Creates root of navigation in current place
*
* @param defaultStartChain Config of default tree for navigation in case [configsRepo] contains no any information
* about last used navigation
* @param configsRepo Contains information about last saved navigation tree
* @param nodesFactory Provides opportunity to create [dev.inmo.navigation.core.NavigationNode] from their configs
* @param scope Will be used to create [LinkedSupervisorScope] which will be the root [CoroutineScope] for all navigation
* operations
* @param dropRedundantChainsOnRestore Drops chains with empty content
* @param rootChain Default root chain where navigation will be rooted
*/
@Composable
fun <Base> initNavigation(
defaultStartChain: ConfigHolder.Chain<Base>,
Expand Down
2 changes: 1 addition & 1 deletion core/src/commonMain/kotlin/NavigationChain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class NavigationChain<Base>(
private val parentNodeState: NavigationNodeState
get() = parentNode ?.state ?: NavigationNodeState.RESUMED

private val _stackFlow = MutableStateFlow<List<NavigationNode<out Base, Base>>>(emptyList())
private val _stackFlow = SpecialMutableStateFlow<List<NavigationNode<out Base, Base>>>(emptyList())
val stackFlow: StateFlow<List<NavigationNode<out Base, Base>>> = _stackFlow.asStateFlow()
internal val stack
get() = stackFlow.value
Expand Down
2 changes: 1 addition & 1 deletion core/src/commonMain/kotlin/NavigationNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class NavigationNode<Config : Base, Base>(
open val storableInNavigationHierarchy: Boolean
get() = (config as? NavigationNodeDefaultConfig) ?.storableInNavigationHierarchy ?: true

internal val _subchainsFlow = MutableStateFlow<List<NavigationChain<Base>>>(emptyList())
internal val _subchainsFlow = SpecialMutableStateFlow<List<NavigationChain<Base>>>(emptyList())
val subchainsFlow: StateFlow<List<NavigationChain<Base>>> = _subchainsFlow.asStateFlow()
val subchains: List<NavigationChain<Base>>
get() = _subchainsFlow.value.toList()
Expand Down

0 comments on commit ea1ebb1

Please sign in to comment.