Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
raamcosta committed Mar 4, 2024
1 parent 239c821 commit 6decd62
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 65 deletions.
6 changes: 0 additions & 6 deletions .idea/kotlinScripting.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@ package com.ramcosta.composedestinations.codegen.commons

class IllegalDestinationsSetup(message: String, cause: Throwable? = null) : RuntimeException(message, cause)

class MissingRequiredDependency(message: String) : RuntimeException(message)

class UnexpectedException(message: String) : RuntimeException(message)

const val DOCS_WEBSITE = "https://composedestinations.rafaelcosta.xyz/"
const val DOCS_WEBSITE_MULTI_MODULE_CONFIGS = "${DOCS_WEBSITE}codegenconfigs#multi-module-configs"
class MissingRequiredDependency(message: String) : RuntimeException(message)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.ramcosta.composedestinations.codegen.commons.ANIMATED_VISIBILITY_SCOP
import com.ramcosta.composedestinations.codegen.commons.BOTTOM_SHEET_DEPENDENCY
import com.ramcosta.composedestinations.codegen.commons.COLUMN_SCOPE_SIMPLE_NAME
import com.ramcosta.composedestinations.codegen.commons.IllegalDestinationsSetup
import com.ramcosta.composedestinations.codegen.commons.MissingRequiredDependency
import com.ramcosta.composedestinations.codegen.commons.OPEN_RESULT_RECIPIENT_QUALIFIED_NAME
import com.ramcosta.composedestinations.codegen.commons.RESULT_BACK_NAVIGATOR_QUALIFIED_NAME
import com.ramcosta.composedestinations.codegen.commons.RESULT_RECIPIENT_QUALIFIED_NAME
Expand Down Expand Up @@ -96,7 +97,7 @@ class InitialValidator(
private fun DestinationGeneratingParams.validateReceiverColumnScope() {
if (composableReceiverSimpleName == COLUMN_SCOPE_SIMPLE_NAME) {
if (!isBottomSheetDependencyPresent) {
throw IllegalDestinationsSetup(
throw MissingRequiredDependency(
"'${composableName}' composable: " +
"You need to include $BOTTOM_SHEET_DEPENDENCY dependency to use a $COLUMN_SCOPE_SIMPLE_NAME receiver!"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ internal class ModuleRegistryWriter(
) {
fun write(
destinations: List<CodeGenProcessedDestination>,
graphTrees: List<RawNavGraphTree>,
graphTrees: List<RawNavGraphTree>
) {
val resultBackTypesByDestination: List<Pair<CodeGenProcessedDestination, TypeInfo>> =
destinations.mapNotNull { destination ->
if (destination.visibility != Visibility.PUBLIC) return@mapNotNull null

) {
val resultBackTypesByDestination: List<Pair<CodeGenProcessedDestination, TypeInfo>> = destinations.mapNotNull { destination ->
if (destination.visibility != Visibility.PUBLIC) return@mapNotNull null

destination.parameters.firstOrNull { it.type.importable.qualifiedName == RESULT_BACK_NAVIGATOR_QUALIFIED_NAME }?.let {
val type = (it.type.typeArguments.firstOrNull() as? TypeArgument.Typed)?.type
?: return@mapNotNull null
destination.parameters.firstOrNull { it.type.importable.qualifiedName == RESULT_BACK_NAVIGATOR_QUALIFIED_NAME }
?.let {
val type =
(it.type.typeArguments.firstOrNull() as? TypeArgument.Typed)?.type
?: return@mapNotNull null

destination to type
destination to type
}
}
}

val registryId = moduleName.ifEmpty { UUID.randomUUID().toString().replace("-", "_") }
val importableHelper = ImportableHelper(
Expand Down Expand Up @@ -80,7 +82,11 @@ internal class ModuleRegistryWriter(
resultBackTypesByDestination.joinToString(",\n") { (destination, type) ->
"""
| _Destination_Result_Info_$registryId(
| destination = ${importableHelper.addAndGetPlaceholder(destination.destinationImportable)}::class,
| destination = ${
importableHelper.addAndGetPlaceholder(
destination.destinationImportable
)
}::class,
| resultType = ${importableHelper.addAndGetPlaceholder(type.importable)}::class,
| isResultNullable = ${type.isNullable}
| )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,7 @@ class Processor(
!navGraphAnnotations.iterator().hasNext() &&
!navHostGraphAnnotations.iterator().hasNext()
) {
val moduleRegistryFile: KSFile = resolver.getNewFiles()
.firstOrNull { it.fileName == "_ModuleRegistry.kt" }
?: return emptyList()
val moduleRegistryId = moduleRegistryFile
.declarations.first {
it.simpleName.asString().startsWith("_ModuleRegistry_")
}.simpleName.asString().removePrefix("_ModuleRegistry_")


CodeGenerator.generateModuleRegistryPathInfo(
KspCodeOutputStreamMaker(
codeGenerator,
MutableKSFileSourceMapper()
),
moduleRegistryFile.filePath,
moduleRegistryId
)
generateModuleRegistryPathInfo(resolver)
return emptyList()
}

Expand Down Expand Up @@ -109,6 +93,26 @@ class Processor(
return emptyList()
}

private fun generateModuleRegistryPathInfo(resolver: Resolver) {
val moduleRegistryFile: KSFile = resolver.getNewFiles()
.firstOrNull { it.fileName == "_ModuleRegistry.kt" }
?: return
val moduleRegistryId = moduleRegistryFile
.declarations.first {
it.simpleName.asString().startsWith("_ModuleRegistry_")
}.simpleName.asString().removePrefix("_ModuleRegistry_")


CodeGenerator.generateModuleRegistryPathInfo(
KspCodeOutputStreamMaker(
codeGenerator,
MutableKSFileSourceMapper()
),
moduleRegistryFile.filePath,
moduleRegistryId
)
}

@OptIn(KspExperimental::class)
private fun Resolver.getSubModuleInfos(): List<SubModuleInfo> {
val moduleRegistryDeclarations = getDeclarationsFromPackage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,28 @@ import android.annotation.SuppressLint
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.navigation.*
import androidx.wear.compose.navigation.*
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.Navigator
import androidx.wear.compose.navigation.SwipeDismissableNavHost
import androidx.wear.compose.navigation.SwipeDismissableNavHostState
import androidx.wear.compose.navigation.WearNavigator
import androidx.wear.compose.navigation.composable
import androidx.wear.compose.navigation.rememberSwipeDismissableNavHostState
import com.ramcosta.composedestinations.animations.NavHostAnimatedDestinationStyle
import com.ramcosta.composedestinations.manualcomposablecalls.DestinationLambda
import com.ramcosta.composedestinations.manualcomposablecalls.ManualComposableCalls
import com.ramcosta.composedestinations.navigation.DependenciesContainerBuilder
import com.ramcosta.composedestinations.rememberNavHostEngine
import com.ramcosta.composedestinations.scope.DestinationScopeImpl
import com.ramcosta.composedestinations.spec.*
import com.ramcosta.composedestinations.spec.DestinationStyle
import com.ramcosta.composedestinations.spec.NavGraphSpec
import com.ramcosta.composedestinations.spec.NavHostEngine
import com.ramcosta.composedestinations.spec.Route
import com.ramcosta.composedestinations.spec.TypedDestinationSpec

/**
* Returns the [WearNavHostEngine] to be used with Wear OS apps.
Expand Down Expand Up @@ -65,9 +78,10 @@ internal class WearNavHostEngine(

override fun NavGraphBuilder.navigation(
navGraph: NavGraphSpec,
manualComposableCalls: ManualComposableCalls,
builder: NavGraphBuilder.() -> Unit
) {
with(defaultNavHostEngine) { navigation(navGraph, builder) }
with(defaultNavHostEngine) { navigation(navGraph, manualComposableCalls, builder) }
}

override fun <T> NavGraphBuilder.composable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.ramcosta.composedestinations.spec.TypedDestinationSpec
@Composable
fun rememberNavHostEngine(
navHostContentAlignment: Alignment = Alignment.Center,
): NavHostEngine = remember {
): NavHostEngine = remember(navHostContentAlignment) {
DefaultNavHostEngine(
navHostContentAlignment = navHostContentAlignment,
)
Expand Down Expand Up @@ -68,9 +68,11 @@ internal class DefaultNavHostEngine(

override fun NavGraphBuilder.navigation(
navGraph: NavGraphSpec,
manualComposableCalls: ManualComposableCalls,
builder: NavGraphBuilder.() -> Unit
) {
val transitions = navGraph.defaultTransitions
val transitions = manualComposableCalls.manualAnimation(navGraph.route)
?: navGraph.defaultTransitions
if (transitions != null) {
with(transitions) {
navigation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private fun NavGraphBuilder.addNestedNavGraphs(
): Unit = with(engine) {

nestedNavGraphs.forEach { nestedGraph ->
navigation(nestedGraph) {
navigation(nestedGraph, manualComposableCalls) {
addNavGraphDestinations(
engine = engine,
navGraph = nestedGraph,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import androidx.compose.animation.fadeOut
import androidx.navigation.NavBackStackEntry
import com.ramcosta.composedestinations.animations.NavHostAnimatedDestinationStyle

/**
* Disables all animations for this NavHost by default.
*/
object NoTransitions : NavHostAnimatedDestinationStyle() {
override fun AnimatedContentTransitionScope<NavBackStackEntry>.enterTransition(): EnterTransition {
return EnterTransition.None
Expand All @@ -19,6 +22,9 @@ object NoTransitions : NavHostAnimatedDestinationStyle() {
}
}

/**
* Uses default fading animations for this NavHost by default.
*/
object DefaultFadingTransitions: NavHostAnimatedDestinationStyle() {
override fun AnimatedContentTransitionScope<NavBackStackEntry>.enterTransition(): EnterTransition {
return fadeIn(animationSpec = tween(700))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import com.ramcosta.composedestinations.scope.AnimatedDestinationScope
import com.ramcosta.composedestinations.scope.DestinationScope
import com.ramcosta.composedestinations.spec.DestinationSpec
import com.ramcosta.composedestinations.spec.DestinationStyle
import com.ramcosta.composedestinations.spec.NavGraphSpec
import com.ramcosta.composedestinations.spec.NavHostEngine
import com.ramcosta.composedestinations.spec.NavHostGraphSpec
import com.ramcosta.composedestinations.spec.TypedDestinationSpec

/**
Expand Down Expand Up @@ -67,6 +69,51 @@ fun <T> ManualComposableCallsBuilder.dialogComposable(

class ManualComposableCallsBuilder internal constructor(@InternalDestinationsApi val engineType: NavHostEngine.Type) {

/**
* Overrides the default animations of [this] [NavGraphSpec] at runtime to [animation].
* You should prefer to use the NavGraph annotation `defaultTransitions` unless there's a specific
* reason to use this.
*/
infix fun NavGraphSpec.animateWith(animation: DestinationStyle.Animated) {
if (this is NavHostGraphSpec) {
error("'animateWith' cannot be called for NavHostGraphs. Use DestinationsNavHost's 'defaultTransitions' for the same effect!")
}
add(route, animation)
}

/**
* Overrides the default animations of [this] [NavGraphSpec] at runtime to use [enterTransition],
* [exitTransition], [popEnterTransition] and [popExitTransition].
* You should prefer to use the NavGraph annotation `defaultTransitions` unless there's a specific
* reason to use this.
*/
fun NavGraphSpec.animateWith(
enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)? =
null,
exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?)? =
null,
popEnterTransition: (
AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?
)? = enterTransition,
popExitTransition: (
AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition?
)? = exitTransition,
) {
this animateWith object: DestinationStyle.Animated() {
override fun AnimatedContentTransitionScope<NavBackStackEntry>.enterTransition(): EnterTransition? =
enterTransition?.invoke(this)

override fun AnimatedContentTransitionScope<NavBackStackEntry>.exitTransition(): ExitTransition? =
exitTransition?.invoke(this)

override fun AnimatedContentTransitionScope<NavBackStackEntry>.popEnterTransition(): EnterTransition? =
popEnterTransition?.invoke(this)

override fun AnimatedContentTransitionScope<NavBackStackEntry>.popExitTransition(): ExitTransition? =
popExitTransition?.invoke(this)
}
}

/**
* Overrides the style of [this] [DestinationSpec] at runtime to [animation].
* You should prefer to use the Destination annotation `style` unless there's a specific
Expand All @@ -76,7 +123,7 @@ class ManualComposableCallsBuilder internal constructor(@InternalDestinationsApi
if (style !is DestinationStyle.Default && style !is DestinationStyle.Animated) {
error("'animateWith' can only be called for a destination of style 'Default' or 'Animated'")
}
add(this, animation)
add(route, animation)
}

/**
Expand Down Expand Up @@ -116,10 +163,10 @@ class ManualComposableCallsBuilder internal constructor(@InternalDestinationsApi
private val animations: MutableMap<String, DestinationStyle.Animated> = mutableMapOf()

internal fun add(
destination: DestinationSpec,
route: String,
animation: DestinationStyle.Animated,
) {
animations[destination.route] = animation
animations[route] = animation
}

// can be internal once bottom sheet functionality is built in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package com.ramcosta.composedestinations.spec

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.*
import androidx.navigation.NavDestination
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.Navigator
import com.ramcosta.composedestinations.animations.NavHostAnimatedDestinationStyle
import com.ramcosta.composedestinations.manualcomposablecalls.ManualComposableCalls
import com.ramcosta.composedestinations.navigation.DependenciesContainerBuilder
Expand Down Expand Up @@ -31,7 +34,7 @@ interface NavHostEngine {
}

/**
* Engine type between [Type.DEFAULT] or [Type.ANIMATED]
* Engine type between [Type.DEFAULT] or [Type.WEAR]
*/
val type: Type

Expand Down Expand Up @@ -61,6 +64,7 @@ interface NavHostEngine {
*/
fun NavGraphBuilder.navigation(
navGraph: NavGraphSpec,
manualComposableCalls: ManualComposableCalls,
builder: NavGraphBuilder.() -> Unit
)

Expand Down
4 changes: 0 additions & 4 deletions playground/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ android {
// // Module name.
// // It will be used as the generated sealed Destinations prefix
// arg("compose-destinations.moduleName", "featureX")
// TODO RACOSTA
// - Visualisation of nav graph even more important!
// - Removing default RootGraph annotation and allow start = true for trivial cases?! 🤔
// - means we could take configs like the codegen package name or module name there?

// // If you want to manually create the nav graphs, use this:
// arg("compose-destinations.generateNavGraphs", "false")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fun DestinationSpec.DrawerContent(
)
}

// TODO Don't do this, since we no longer create sealed Destination, it doesn't work very well
@get:StringRes
val DestinationSpec.requireTitle
get(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ import com.ramcosta.samples.playground.ui.screens.navgraphs.ProfileNavGraphArgs
import com.ramcosta.samples.playground.ui.screens.navgraphs.ProfileSettingsNavGraphArgs
import kotlin.reflect.KClass

/*
TODO RACOSTA:
- Enable multiple destinations in the same Composable - possibility to belong to multiple graphs as well
- Runtime animations that could depend on some app logic or state - maybe with manual composable calls, also accept animations to be set, in
which case we would call those instead of the ones from DestinationStyle.
*/

@NavGraph<RootGraph>(
defaultTransitions = DefaultFadingTransitions::class
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import com.ramcosta.composedestinations.generated.featurex.destinations.FeatureX
import com.ramcosta.composedestinations.generated.featurex.destinations.InternalArgsScreenDestination
import com.ramcosta.composedestinations.generated.featurex.navgraphs.FeatureXNavGraph
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.playground.featurex.FeatureXGraph
import com.ramcosta.playground.featurex.FeatureXWrapper

data class FeatureXHomeNavArgs(
val something2: String
)

@Destination<com.ramcosta.playground.featurex.FeatureXGraph>(
@Destination<FeatureXGraph>(
start = true,
navArgs = FeatureXHomeNavArgs::class,
wrappers = [FeatureXWrapper::class]
Expand Down

0 comments on commit 6decd62

Please sign in to comment.