Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove destinationId and replace it with the class name of routes #46

Merged
merged 3 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions navigator/runtime-compose/api/runtime-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ public abstract interface class com/freeletics/mad/navigator/compose/NavDestinat

public final class com/freeletics/mad/navigator/compose/NavDestination$Activity : com/freeletics/mad/navigator/compose/NavDestination {
public static final field $stable I
public fun <init> (Lkotlin/reflect/KClass;ILandroid/content/Intent;)V
public fun <init> (Lkotlin/reflect/KClass;Landroid/content/Intent;)V
}

public final class com/freeletics/mad/navigator/compose/NavDestination$BottomSheet : com/freeletics/mad/navigator/compose/NavDestination {
public static final field $stable I
public fun <init> (Lkotlin/reflect/KClass;ILkotlin/jvm/functions/Function3;)V
public fun <init> (Lkotlin/reflect/KClass;Lkotlin/jvm/functions/Function3;)V
}

public final class com/freeletics/mad/navigator/compose/NavDestination$Dialog : com/freeletics/mad/navigator/compose/NavDestination {
public static final field $stable I
public fun <init> (Lkotlin/reflect/KClass;ILkotlin/jvm/functions/Function3;)V
public fun <init> (Lkotlin/reflect/KClass;Lkotlin/jvm/functions/Function3;)V
}

public final class com/freeletics/mad/navigator/compose/NavDestination$Screen : com/freeletics/mad/navigator/compose/NavDestination {
public static final field $stable I
public fun <init> (Lkotlin/reflect/KClass;ILkotlin/jvm/functions/Function3;)V
public fun <init> (Lkotlin/reflect/KClass;Lkotlin/jvm/functions/Function3;)V
}

public final class com/freeletics/mad/navigator/compose/NavHostKt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,116 +15,105 @@ import kotlin.reflect.KClass

/**
* Creates a new [NavDestination] that represents a full screen. The class of [T] will be used
* as a unique identifier together with [destinationId]. The given [screenContent] will be shown
* when the screen is being navigated to using an instance of [T].
* as a unique identifier. The given [screenContent] will be shown when the screen is being
* navigated to using an instance of [T].
*/
@Suppress("FunctionName")
public inline fun <reified T : BaseRoute> ScreenDestination(
destinationId: Int,
noinline screenContent: @Composable (T) -> Unit,
): NavDestination = Screen(T::class, destinationId, screenContent)
): NavDestination = Screen(T::class, screenContent)

/**
* Creates a new [NavDestination] that represents a dialog. The class of [T] will be used
* as a unique identifier together with [destinationId]. The given [dialogContent] will be shown
* inside the dialog window when navigating to it by using an instance of [T].
* as a unique identifier. The given [dialogContent] will be shown inside the dialog window when
* navigating to it by using an instance of [T].
*/
@Suppress("FunctionName")
public inline fun <reified T : NavRoute> DialogDestination(
destinationId: Int,
noinline dialogContent: @Composable (T) -> Unit,
): NavDestination = Dialog(T::class, destinationId, dialogContent)
): NavDestination = Dialog(T::class, dialogContent)

/**
* Creates a new [NavDestination] that represents a bottom sheet. The class of [T] will be used
* as a unique identifier together with [destinationId]. The given [bottomSheetContent] will be
* shown inside the bottom sheet when navigating to it by using an instance of [T].
* as a unique identifier. The given [bottomSheetContent] will be shown inside the bottom sheet
* when navigating to it by using an instance of [T].
*/
@Suppress("FunctionName")
@ExperimentalMaterialNavigationApi
public inline fun <reified T : NavRoute> BottomSheetDestination(
destinationId: Int,
noinline bottomSheetContent: @Composable (T) -> Unit,
): NavDestination = BottomSheet(T::class, destinationId, bottomSheetContent)
): NavDestination = BottomSheet(T::class, bottomSheetContent)

/**
* Creates a new [NavDestination] that represents an `Activity`. The class of [T] will be used
* as a unique identifier together with [destinationId]. The given [intent] will be
* used to launch the `Activity` when using an instance of [T] for navigation.
* as a unique identifier. The given [intent] will be used to launch the `Activity` when using an
* instance of [T] for navigation.
*/
@Suppress("FunctionName")
public inline fun <reified T : NavRoute> ActivityDestination(
destinationId: Int,
intent: Intent,
): NavDestination = Activity(T::class, destinationId, intent)
): NavDestination = Activity(T::class, intent)

/**
* A destination that can be navigated to. See [NavHost] for how to configure a `NavGraph` with it.
*/
public sealed interface NavDestination {
/**
* Represents a full screen. The [route] will be used as a unique identifier together
* with [destinationId]. The given [screenContent] will be shown when the screen is being
* Represents a full screen. The [route] will be used as a unique identifier.
* The given [screenContent] will be shown when the screen is being
* navigated to using an instance of [route].
*/
public class Screen<T : BaseRoute> @ObsoleteNavigatorApi constructor(
internal val route: KClass<T>,
internal val destinationId: Int,
internal val defaultArguments: Bundle?,
internal val screenContent: @Composable (T) -> Unit,
) : NavDestination {
public constructor(
route: KClass<T>,
destinationId: Int,
screenContent: @Composable (T) -> Unit,
) : this(route, destinationId, null, screenContent)
) : this(route, null, screenContent)
}

/**
* Represents a dialog. The [route] will be used as a unique identifier together
* with [destinationId]. The given [dialogContent] will be shown inside the dialog window
* Represents a dialog. The [route] will be used as a unique identifier together.
* The given [dialogContent] will be shown inside the dialog window
* when navigating to it by using an instance of [route].
*/
public class Dialog<T : NavRoute> @ObsoleteNavigatorApi constructor(
internal val route: KClass<T>,
internal val destinationId: Int,
internal val defaultArguments: Bundle?,
internal val dialogContent: @Composable (T) -> Unit,
) : NavDestination {
public constructor(
route: KClass<T>,
destinationId: Int,
dialogContent: @Composable (T) -> Unit,
) : this(route, destinationId, null, dialogContent)
) : this(route, null, dialogContent)
}

/**
* Represents a bottom sheet. The [route] will be used as a unique identifier together
* with [destinationId]. The given [bottomSheetContent] will be shown inside the bottom sheet
* Represents a bottom sheet. The [route] will be used as a unique identifier.
* The given [bottomSheetContent] will be shown inside the bottom sheet
* when navigating to it by using an instance of [route].
*/
@ExperimentalMaterialNavigationApi
public class BottomSheet<T : NavRoute> @ObsoleteNavigatorApi constructor(
internal val route: KClass<T>,
internal val destinationId: Int,
internal val defaultArguments: Bundle?,
internal val bottomSheetContent: @Composable (T) -> Unit,
) : NavDestination {
public constructor(
route: KClass<T>,
destinationId: Int,
bottomSheetContent: @Composable (T) -> Unit,
) : this(route, destinationId, null, bottomSheetContent)
) : this(route, null, bottomSheetContent)
}

/**
* Represents an `Activity`. The [route] will be used as a unique identifier together
* with [destinationId]. The given [intent] will be used to launch the `Activity` when using
* Represents an `Activity`. The [route] will be used as a unique identifier.
* The given [intent] will be used to launch the `Activity` when using
* an instance of [route] for navigation.
*/
public class Activity(
internal val route: KClass<out NavRoute>,
internal val destinationId: Int,
internal val intent: Intent,
) : NavDestination
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ import com.freeletics.mad.navigator.compose.NavDestination.BottomSheet
import com.freeletics.mad.navigator.compose.NavDestination.Dialog
import com.freeletics.mad.navigator.compose.NavDestination.Screen
import com.freeletics.mad.navigator.internal.ObsoleteNavigatorApi
import com.freeletics.mad.navigator.internal.destinationId
import com.freeletics.mad.navigator.internal.getArguments
import com.freeletics.mad.navigator.internal.toRoute
import com.google.accompanist.navigation.material.BottomSheetNavigator
import com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi
import com.google.accompanist.navigation.material.ModalBottomSheetLayout
import com.google.accompanist.navigation.material.rememberBottomSheetNavigator
import kotlin.reflect.KClass

/**
* Create a new [androidx.navigation.compose.NavHost] with a [androidx.navigation.NavGraph]
Expand All @@ -47,13 +49,12 @@ public fun NavHost(
startRoute: BaseRoute,
destinations: Set<NavDestination>,
) {

val bottomSheetNavigator = rememberBottomSheetNavigator()
val navController = rememberNavController(bottomSheetNavigator)

val graph = remember(navController, startRoute, destinations) {
@Suppress("deprecation")
navController.createGraph(startDestination = startRoute.destinationId) {
navController.createGraph(startDestination = startRoute.destinationId()) {
destinations.forEach { destination ->
addDestination(navController, destination, startRoute)
}
Expand Down Expand Up @@ -92,7 +93,7 @@ private fun <T : BaseRoute> Screen<T>.toDestination(
): ComposeNavigator.Destination {
val navigator = controller.navigatorProvider[ComposeNavigator::class]
return ComposeNavigator.Destination(navigator) { screenContent(it.arguments!!.toRoute()) }.also {
it.id = destinationId
it.id = route.destinationId()
it.addDefaultArguments(defaultArguments)
if (startRoute::class == route) {
it.addDefaultArguments(startRoute.getArguments())
Expand All @@ -105,7 +106,7 @@ private fun <T : NavRoute> Dialog<T>.toDestination(
): DialogNavigator.Destination {
val navigator = controller.navigatorProvider[DialogNavigator::class]
return DialogNavigator.Destination(navigator) { dialogContent(it.arguments!!.toRoute()) }.also {
it.id = destinationId
it.id = route.destinationId()
it.addDefaultArguments(defaultArguments)
}
}
Expand All @@ -118,7 +119,7 @@ private fun <T : NavRoute> BottomSheet<T>.toDestination(
): BottomSheetNavigator.Destination {
val navigator = controller.navigatorProvider[BottomSheetNavigator::class]
return BottomSheetNavigator.Destination(navigator) { bottomSheetContent(it.arguments!!.toRoute()) }.also {
it.id = destinationId
it.id = route.destinationId()
it.addDefaultArguments(defaultArguments)
}
}
Expand All @@ -128,7 +129,7 @@ private fun Activity.toDestination(
): ActivityNavigator.Destination {
val navigator = controller.navigatorProvider[ActivityNavigator::class]
return ActivityNavigator.Destination(navigator).also {
it.id = destinationId
it.id = route.destinationId()
it.setIntent(intent)
}
}
Expand Down
6 changes: 3 additions & 3 deletions navigator/runtime-fragment/api/runtime-fragment.api
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public abstract interface class com/freeletics/mad/navigator/fragment/NavDestina
}

public final class com/freeletics/mad/navigator/fragment/NavDestination$Activity : com/freeletics/mad/navigator/fragment/NavDestination {
public fun <init> (Lkotlin/reflect/KClass;ILandroid/content/Intent;)V
public fun <init> (Lkotlin/reflect/KClass;Landroid/content/Intent;)V
}

public final class com/freeletics/mad/navigator/fragment/NavDestination$Dialog : com/freeletics/mad/navigator/fragment/NavDestination {
public fun <init> (Lkotlin/reflect/KClass;ILkotlin/reflect/KClass;)V
public fun <init> (Lkotlin/reflect/KClass;Lkotlin/reflect/KClass;)V
}

public final class com/freeletics/mad/navigator/fragment/NavDestination$Screen : com/freeletics/mad/navigator/fragment/NavDestination {
public fun <init> (Lkotlin/reflect/KClass;ILkotlin/reflect/KClass;)V
public fun <init> (Lkotlin/reflect/KClass;Lkotlin/reflect/KClass;)V
}

public final class com/freeletics/mad/navigator/fragment/NavHostFragmentKt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,86 +14,77 @@ import kotlin.reflect.KClass

/**
* Creates a new [NavDestination] that represents a full screen. The class of [T] will be used
* as a unique identifier together with [destinationId]. The given `Fragment` class [F] will be
* shown when the screen is being navigated to using an instance of [T].
* as a unique identifier. The given `Fragment` class [F] will be shown when the screen is being
* navigated to using an instance of [T].
*/
@Suppress("FunctionName")
public inline fun <reified T : NavRoute, reified F : Fragment> ScreenDestination(
destinationId: Int,
): NavDestination = Screen(T::class, destinationId, F::class)
public inline fun <reified T : BaseRoute, reified F : Fragment> ScreenDestination():
NavDestination = Screen(T::class, F::class)

/**
* Creates a new [NavDestination] that represents a dialog. The class of [T] will be used
* as a unique identifier together with [destinationId]. The given `Fragment` class [F] will be
* shown when the screen is being navigated to using an instance of [T].
* as a unique identifier. The given `Fragment` class [F] will be shown when the screen is being
* navigated to using an instance of [T].
*/
@Suppress("FunctionName")
public inline fun <reified T : NavRoute, reified F : DialogFragment> DialogDestination(
destinationId: Int,
): NavDestination = Dialog(T::class, destinationId, F::class)
public inline fun <reified T : NavRoute, reified F : DialogFragment> DialogDestination():
NavDestination = Dialog(T::class, F::class)

/**
* Creates a new [NavDestination] that represents an `Activity`. The class of [T] will be used
* as a unique identifier together with [destinationId]. The given [intent] will be
* used to launch the `Activity` when using an instance of [T] for navigation.
* as a unique identifier. The given [intent] will be used to launch the `Activity` when using an
* instance of [T] for navigation.
*/
@Suppress("FunctionName")
public inline fun <reified T : NavRoute> ActivityDestination(
destinationId: Int,
intent: Intent,
): NavDestination = Activity(T::class, destinationId, intent)
): NavDestination = Activity(T::class, intent)

/**
* A destination that can be navigated to. See [setGraph] for how to configure a `NavGraph` with it.
*
* [route] will be used as a unique identifier together with [destinationId]. The destination can
* be reached by navigating using an instance of [route].
* [route] will be used as a unique identifier. The destination can be reached by navigating using
* an instance of [route].
*/
public sealed interface NavDestination {
/**
* Represents a full screen. The [route] will be used as a unique identifier together
* with [destinationId]. The given [fragmentClass] will be shown when the screen is being
* Represents a full screen. The [route] will be used as a unique identifier. The given
* [fragmentClass] will be shown when the screen is being
* navigated to using an instance of [route].
*/
public class Screen @ObsoleteNavigatorApi constructor(
internal val route: KClass<out BaseRoute>,
internal val destinationId: Int,
public class Screen<T : BaseRoute> @ObsoleteNavigatorApi constructor(
internal val route: KClass<T>,
internal val fragmentClass: KClass<out Fragment>,
internal val defaultArguments: Bundle?,
) : NavDestination {
public constructor(
route: KClass<out BaseRoute>,
destinationId: Int,
route: KClass<T>,
fragmentClass: KClass<out Fragment>,
) : this(route, destinationId, fragmentClass, null)
) : this(route, fragmentClass, null)
}

/**
* Represents a dialog. The [route] will be used as a unique identifier together
* with [destinationId]. The given [fragmentClass] will be shown when it's being navigated to
* using an instance of [route].
* Represents a dialog. The [route] will be used as a unique identifier. The given
* [fragmentClass] will be shown when it's being navigated to using an instance of [route].
*/
public class Dialog @ObsoleteNavigatorApi constructor(
internal val route: KClass<out NavRoute>,
internal val destinationId: Int,
public class Dialog<T : NavRoute> @ObsoleteNavigatorApi constructor(
internal val route: KClass<T>,
internal val fragmentClass: KClass<out DialogFragment>,
internal val defaultArguments: Bundle?,
) : NavDestination {
public constructor(
route: KClass<out NavRoute>,
destinationId: Int,
route: KClass<T>,
fragmentClass: KClass<out DialogFragment>,
) : this(route, destinationId, fragmentClass, null)
) : this(route, fragmentClass, null)
}

/**
* Represents an `Activity`. The [route] will be used as a unique identifier together
* with [destinationId]. The given [intent] will be used to launch the `Activity` when using
* an instance of [route] for navigation.
* Represents an `Activity`. The [route] will be used as a unique identifier. The given
* [intent] will be used to launch the `Activity` when using an instance of [route] for
* navigation.
*/
public class Activity(
internal val route: KClass<out NavRoute>,
internal val destinationId: Int,
public class Activity<T : NavRoute>(
internal val route: KClass<T>,
internal val intent: Intent,
) : NavDestination
}