Skip to content

Commit

Permalink
Updated Navigation Components and Flow
Browse files Browse the repository at this point in the history
  • Loading branch information
chRyNaN committed Jul 20, 2022
1 parent b7f5cf2 commit 806f272
Show file tree
Hide file tree
Showing 36 changed files with 721 additions and 1,312 deletions.
Original file line number Diff line number Diff line change
@@ -1,49 +1,26 @@
package com.chrynan.navigation.compose

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import com.chrynan.navigation.ExperimentalNavigationApi
import com.chrynan.navigation.NavigationContext
import com.chrynan.navigation.NavigationDestination
import com.chrynan.navigation.Navigator

@Suppress("unused")
@Composable
@ExperimentalNavigationApi
internal actual fun <Context, Key> InternalNavContainer(
navigator: ComposeNavigatorByContentViewModel<Context, Key>
fun <Destination : NavigationDestination, Context : NavigationContext<Destination>> NavContainer(
navigator: Navigator<Destination, Context>,
modifier: Modifier,
content: @Composable ComposeNavigationScope.(context: Context, destination: Destination) -> Unit
) {
val contentKey = rememberSaveable(navigator.keySaver) { mutableStateOf(navigator.initialKey) }

navigator.keyChanges.collectAsStateIn(state = contentKey)
val context = navigator.state.currentContextAsState()
val destination = navigator.state.currentDestinationAsState()

val scope = object : ComposeNavigationContentScope<Context, Key> {

override val navigator: ComposeNavigatorByContentViewModel<Context, Key> = navigator
}

Box {
navigator.apply {
scope.content(contentKey.value)
}
Box(modifier = modifier) {
content(ComposeNavigationScope, context.value, destination.value)
}

BackHandler(enabled = navigator.canGoBack()) { navigator.goBack() }
}

@Composable
@ExperimentalNavigationApi
internal actual fun <Context, Key, NavigationScope : ComposeNavigationKeyScope<Context, Key>> InternalNavContainer(
navigator: BaseComposeNavigatorByKeyViewModel<Context, Key, NavigationScope>,
scope: NavigationScope
) {
val contentKey = rememberSaveable(navigator.keySaver) { mutableStateOf(navigator.initialKey) }

navigator.keyChanges.collectAsStateIn(state = contentKey)

Box {
navigator.apply {
scope.content(contentKey.value)
}
}

BackHandler(enabled = navigator.canGoBack()) { navigator.goBack() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

package com.chrynan.navigation.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState

actual typealias Saver<Original, Saveable> = androidx.compose.runtime.saveable.Saver<Original, Saveable>

actual fun <T> autoSaver(): Saver<T, Any> = androidx.compose.runtime.saveable.autoSaver()

@Composable
internal actual fun <T> internalRememberSaveable(
vararg inputs: Any?,
stateSaver: Saver<T, out Any>,
key: String?,
init: () -> MutableState<T>
): MutableState<T> =
androidx.compose.runtime.saveable.rememberSaveable(inputs = inputs, stateSaver = stateSaver, key = key, init = init)

@Composable
internal actual fun <T : Any> internalRememberSaveable(
vararg inputs: Any?,
saver: Saver<T, out Any>,
key: String?,
init: () -> T
): T = androidx.compose.runtime.saveable.rememberSaveable(inputs = inputs, saver = saver, key = key, init = init)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
package com.chrynan.navigation.compose

import com.chrynan.navigation.ExperimentalNavigationApi
import com.chrynan.navigation.NavigationIntent
import com.chrynan.navigation.NavigationScope

@ExperimentalNavigationApi
interface ComposeNavigationScope : NavigationScope {

companion object : ComposeNavigationScope
}

@ExperimentalNavigationApi
interface ComposeNavigationKeyScope<Context, Key> : ComposeNavigationScope {

val navigator: ComposeNavigatorByKey<Context, Key>

companion object
}

@ExperimentalNavigationApi
interface ComposeNavigationIntentScope<Context, Intent : NavigationIntent> : ComposeNavigationScope,
ComposeNavigationKeyScope<Context, Intent> {

override val navigator: ComposeNavigationIntentStackNavigatorByKey<Context, Intent>

companion object
}

@ExperimentalNavigationApi
interface ComposeNavigationContentScope<Context, Key> : ComposeNavigationScope {

val navigator: ComposeNavigatorByContent<Context, Key>

companion object
}

This file was deleted.

Loading

0 comments on commit 806f272

Please sign in to comment.