Skip to content

Commit

Permalink
add navigation node factory providing
Browse files Browse the repository at this point in the history
  • Loading branch information
InsanusMokrassar committed Jun 21, 2024
1 parent ea1ebb1 commit 24ede3b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
5 changes: 5 additions & 0 deletions compose/src/commonMain/kotlin/ComposeProviders.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.inmo.navigation.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.ComposeCompilerApi
import androidx.compose.runtime.ProvidableCompositionLocal
import androidx.compose.runtime.compositionLocalOf
import dev.inmo.navigation.core.NavigationChain
Expand All @@ -8,6 +10,7 @@ import dev.inmo.navigation.core.NavigationNodeFactory

val LocalNavigationNodeFactory = compositionLocalOf<NavigationNodeFactory<*>> { NavigationNodeFactory<Any> { _, _ -> null } }

internal val InternalLocalNavigationNodeFactory: ProvidableCompositionLocal<NavigationNodeFactory<*>> = compositionLocalOf<NavigationNodeFactory<*>> { NavigationNodeFactory<Any> { _, _ -> null } }
internal val InternalLocalNavigationChainProvider: ProvidableCompositionLocal<NavigationChain<*>> = compositionLocalOf<NavigationChain<*>> { NavigationChain<Any>(null, { _, _ -> null }) }
internal val InternalLocalNavigationNodeProvider: ProvidableCompositionLocal<NavigationNode<*, *>> = compositionLocalOf {
NavigationNode.Empty<Any, Any>(
Expand All @@ -16,6 +19,8 @@ internal val InternalLocalNavigationNodeProvider: ProvidableCompositionLocal<Nav
)
}

@Suppress("UNCHECKED_CAST")
internal fun <Base> InternalLocalNavigationNodeFactory(): ProvidableCompositionLocal<NavigationNodeFactory<Base>> = InternalLocalNavigationNodeFactory as ProvidableCompositionLocal<NavigationNodeFactory<Base>>
@Suppress("UNCHECKED_CAST")
internal fun <Base> LocalNavigationChainProvider(): ProvidableCompositionLocal<NavigationChain<Base>> = InternalLocalNavigationChainProvider as ProvidableCompositionLocal<NavigationChain<Base>>
@Suppress("UNCHECKED_CAST")
Expand Down
37 changes: 20 additions & 17 deletions compose/src/commonMain/kotlin/InitNavigation.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.inmo.navigation.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import dev.inmo.kslog.common.TagLogger
Expand Down Expand Up @@ -55,26 +56,28 @@ fun <Base> initNavigation(
}
}
}
logger.d { "Hierarchy saving enabled" }
CompositionLocalProvider(InternalLocalNavigationNodeFactory<Base>() provides resultNodesFactory) {
logger.d { "Hierarchy saving enabled" }

val existsChain = configsRepo.get()
val existsChain = configsRepo.get()

logger.d {
if (existsChain == null) {
"Can't find exists chain. Using default one: $defaultStartChain"
} else {
"Took exists stored chain $existsChain"
logger.d {
if (existsChain == null) {
"Can't find exists chain. Using default one: $defaultStartChain"
} else {
"Took exists stored chain $existsChain"
}
}
}

val restoredRootChain = remember {
restoreHierarchy<Base>(
existsChain ?: defaultStartChain,
factory = resultNodesFactory,
rootChain = rootChain,
dropRedundantChainsOnRestore = dropRedundantChainsOnRestore
)
val restoredRootChain = remember {
restoreHierarchy<Base>(
existsChain ?: defaultStartChain,
factory = resultNodesFactory,
rootChain = rootChain,
dropRedundantChainsOnRestore = dropRedundantChainsOnRestore
)
}
restoredRootChain ?.start(subscope)
restoredRootChain ?.StartInCompose({ savingJob.cancel() })
}
restoredRootChain ?.start(subscope)
restoredRootChain ?.StartInCompose({ savingJob.cancel() })
}

0 comments on commit 24ede3b

Please sign in to comment.