Created
October 2, 2022 15:02
-
-
Save hrach/69307869e5678a6d0dbc6725dd9e66c3 to your computer and use it in GitHub Desktop.
Navigation Compose Typed article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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