Skip to content

Instantly share code, notes, and snippets.

View igorescodro's full-sized avatar
♥️
Coding with love

Igor Escodro igorescodro

♥️
Coding with love
View GitHub Profile
internal class NavGraphProvider(
private val navEventController: NavEventController,
private val navGraphs: List<NavGraph>,
) {
val navigationGraph: NavGraphBuilder.() -> Unit = {
navGraphs.forEach { graph ->
graph.navGraph(this, navEventController)
}
}
// NavEventController.kt
interface NavEventController {
val destinationState: SharedFlow<Destination>
fun sendEvent(event: Event)
}
// NavEventControllerImpl.kt
internal class NavEventControllerImpl(
private val appCoroutineScope: AppCoroutineScope,
// NavGraph.kt
interface NavGraph {
val navGraph: NavGraphBuilder.(NavEventController) -> Unit
}
// ShoppingNavGraph.kt
internal class ShoppingNavGraph : NavGraph {
override val navGraph: NavGraphBuilder.(NavEventController) -> Unit = { navEventController ->
composable<ShoppingDestination.Details> {
val route: ShoppingDestination.Details = navEntry.toRoute()
// Event.kt
sealed interface Event {
fun nextDestination(): Destination
data object OnBack : Event {
override fun nextDestination(): Destination = Destination.Back
}
}
// ShoppingEvent.kt
// Destination.kt
sealed interface Destination {
@Serializable
data object Back : Destination
}
// ShoppingDestination.kt
object ShoppingDestination {
@Serializable
// [...] Composable screen code
when (state) {
MainState.Loading -> {
ShimmerProvider {
ItemCard(item = fakeData)
}
}
is MainState.Success -> {
ItemCard(item = state.itemData)
@Composable
fun Modifier.shimmerable(
shape: Shape = RoundedCornerShape(8.dp),
color: Color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f)
): Modifier {
if (!LocalShimmerState.current.isLoading) return this
return this
.shimmer()
.background(color = color, shape = shape)
// [...] Composable screen code
when (state) {
MainState.Loading -> {
ItemCard(
item = fakeData,
isLoading = true,
)
}
is MainState.Success -> {
@Composable
private fun ItemCard(
item: ItemData,
isLoading: Boolean = false
modifier: Modifier = Modifier
) {
Text(
text = item.description,
style = MaterialTheme.typography.bodyMedium,
modifier = Modifier.shimmerable(enabled = isLoading)
@PreviewLightDark
@Composable
private fun ShimmerablePreview() {
ExampleTheme {
Column(
modifier = Modifier
.background(MaterialTheme.colorScheme.surface)
.padding(8.dp)
) {
Text(