Add navigation route with argument contract

pull/156/head
ratobing 3 years ago
parent f651908b4f
commit 263b5adde3

@ -36,3 +36,11 @@ interface NiaNavigationDestination {
*/
val destination: String
}
interface NiaNavigationWithArguments {
/**
* Defines the arguments on the screen, which will be combined with the screen route.
*/
val routeWithArguments: String
}

@ -21,19 +21,23 @@ import androidx.navigation.NavType
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.google.samples.apps.nowinandroid.core.navigation.NiaNavigationDestination
import com.google.samples.apps.nowinandroid.core.navigation.NiaNavigationWithArguments
import com.google.samples.apps.nowinandroid.feature.author.AuthorRoute
object AuthorDestination : NiaNavigationDestination {
object AuthorDestination : NiaNavigationDestination, NiaNavigationWithArguments {
override val route = "author_route"
override val destination = "author_destination"
const val authorIdArg = "authorId"
override val routeWithArguments: String
get() = route.plus("/{$authorIdArg}")
}
fun NavGraphBuilder.authorGraph(
onBackClick: () -> Unit
) {
composable(
route = "${AuthorDestination.route}/{${AuthorDestination.authorIdArg}}",
route = AuthorDestination.routeWithArguments,
arguments = listOf(
navArgument(AuthorDestination.authorIdArg) {
type = NavType.StringType
@ -42,4 +46,4 @@ fun NavGraphBuilder.authorGraph(
) {
AuthorRoute(onBackClick = onBackClick)
}
}
}

@ -21,19 +21,23 @@ import androidx.navigation.NavType
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.google.samples.apps.nowinandroid.core.navigation.NiaNavigationDestination
import com.google.samples.apps.nowinandroid.core.navigation.NiaNavigationWithArguments
import com.google.samples.apps.nowinandroid.feature.topic.TopicRoute
object TopicDestination : NiaNavigationDestination {
object TopicDestination : NiaNavigationDestination, NiaNavigationWithArguments {
override val route = "topic_route"
override val destination = "topic_destination"
const val topicIdArg = "topicId"
override val routeWithArguments: String
get() = route.plus("/{$topicIdArg}")
}
fun NavGraphBuilder.topicGraph(
onBackClick: () -> Unit
) {
composable(
route = "${TopicDestination.route}/{${TopicDestination.topicIdArg}}",
route = TopicDestination.routeWithArguments,
arguments = listOf(
navArgument(TopicDestination.topicIdArg) {
type = NavType.StringType
@ -42,4 +46,4 @@ fun NavGraphBuilder.topicGraph(
) {
TopicRoute(onBackClick = onBackClick)
}
}
}
Loading…
Cancel
Save