Skip to content

Instantly share code, notes, and snippets.

@hrach
Created October 2, 2022 15:02
Show Gist options
  • Save hrach/69307869e5678a6d0dbc6725dd9e66c3 to your computer and use it in GitHub Desktop.
Save hrach/69307869e5678a6d0dbc6725dd9e66c3 to your computer and use it in GitHub Desktop.
Navigation Compose Typed article
import com.kiwi.navigationcompose.typed.Destination
import kotlinx.serialization.Serializable
sealed interface Destinations : Destination {
@Serializable
object BookingList: Destinations
@Serializable
data class BookingDetail(
val bookingId: Long,
) : Destinations
}
import com.kiwi.navigationcompose.typed.composable
import com.kiwi.navigationcompose.typed.createRoutePattern
val navController = rememberNavController()
NavHost(
navController = navController,
startDestination = createRoutePattern<Destinations.BookingList>(),
) {
composable<Destinations.BookingList> {
Home(navController::navigate)
}
composable<Destinations.BookingDetail> {
// this is Destinations.BookingDetail
BookingDetail(bookingId)
}
}
import com.kiwi.navigationcompose.typed.Destination
import com.kiwi.navigationcompose.typed.navigate
@Composable
internal fun Home(navigate: (Destination) -> Unit) {
Home(
onBookingClick = { id -> navigate(Destinations.BookingDetail(id)) },
)
}
@Composable
private fun Home(onBookingClick: (id: Long) -> Unit) {
// ...
}
import com.kiwi.navigationcompose.typed.createRoutePattern
import com.kiwi.navigationcompose.typed.createNavArguments
import com.kiwi.navigationcompose.typed.decodeArguments
import com.kiwi.navigationcompose.typed.Destination
import com.kiwi.navigationcompose.typed.registerDestinationType
private inline fun <reified T : Destination> NavGraphBuilder.bottomSheet(
noinline content: @Composable T.(NavBackStackEntry) -> Unit,
) {
val serializer = serializer<T>()
registerDestinationType(T::class, serializer)
bottomSheet(
route = createRoutePattern(serializer),
arguments = createNavArguments(serializer),
) {
val arguments = decodeArguments(serializer, it)
arguments.content(it)
}
}
@Composable
fun App() {
val bottomSheetNavigator = rememberBottomSheetNavigator()
val navController = rememberNavController(bottomSheetNavigator)
ModalBottomSheetLayout(bottomSheetNavigator) {
NavGraph(
navController = navControoler,
startDestination = createRoutePattern<Destinations.Home>(),
) {
bottomSheet<Destinations.Article> {
Article(id)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment