Skip to content

Instantly share code, notes, and snippets.

@OliverVicente
Created February 10, 2025 21:24
Show Gist options
  • Select an option

  • Save OliverVicente/3e16096de3dc6e092c478e62a8e6d771 to your computer and use it in GitHub Desktop.

Select an option

Save OliverVicente/3e16096de3dc6e092c478e62a8e6d771 to your computer and use it in GitHub Desktop.
OBookScaffold for navigation testing
package com.example.obook.ui.component.scaffold
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.safeDrawingPadding
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScaffold
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScaffoldDefaults
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteType
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.PreviewScreenSizes
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.obook.ui.component.menu.MenuIcon
import com.example.obook.ui.component.menu.getMenuItems
import com.example.obook.ui.navigation.NavigationGraph
import com.example.obook.ui.navigation.NavigationRoutes
import com.example.obook.ui.preview.getNavigationSuiteType
import com.example.obook.ui.theme.OBookTheme
@Composable
fun OBookScaffold(
navController: NavHostController = rememberNavController(),
layoutType: NavigationSuiteType = NavigationSuiteScaffoldDefaults.calculateFromAdaptiveInfo(
currentWindowAdaptiveInfo()
)
) {
var selectedIndex by remember { mutableIntStateOf(0) }
val menuItems = getMenuItems(
onNavigateToBook = {
navController.navigate(route = NavigationRoutes.BOOK_SEARCH)
selectedIndex = it
},
onNavigateToCart = {
navController.navigate(route = NavigationRoutes.SHOPPING_CART)
selectedIndex = it
},
onNavigateToUser = {
navController.navigate(route = NavigationRoutes.USER)
selectedIndex = it
}
)
NavigationSuiteScaffold(
layoutType = layoutType,
navigationSuiteItems = {
menuItems.forEachIndexed { index, navItem ->
item(
icon = { MenuIcon(icon = navItem.icon, label = navItem.label) },
label = { Text(navItem.label) },
selected = selectedIndex == index,
onClick = { navItem.navigationCallback(index) }
)
}
},
modifier = Modifier.fillMaxSize().safeDrawingPadding()
) {
NavigationGraph(navController = navController)
}
}
@PreviewScreenSizes
@Composable
internal fun PreviewOBookScaffold() {
OBookTheme {
Surface {
OBookScaffold(layoutType = getNavigationSuiteType())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment