Loading packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/ui/Category.kt +34 −3 Original line number Diff line number Diff line Loading @@ -78,6 +78,8 @@ fun CategoryTitle(title: String) { /** * A container that is used to group similar items. A [Category] displays a [CategoryTitle] and * visually separates groups of items. * * @param content The content of the category. */ @Composable fun Category( Loading Loading @@ -126,7 +128,8 @@ fun Category( * be decided by the index. * @param bottomPadding Optional. Bottom outside padding of the category. * @param state Optional. State of LazyList. * @param content Optional. Content to be shown at the top of the category. * @param footer Optional. Content to be shown at the bottom of the category. * @param header Optional. Content to be shown at the top of the category. */ @Composable fun LazyCategory( Loading @@ -136,7 +139,8 @@ fun LazyCategory( title: ((Int) -> String?)? = null, bottomPadding: Dp = SettingsDimension.paddingSmall, state: LazyListState = rememberLazyListState(), content: @Composable () -> Unit, footer: @Composable () -> Unit = {}, header: @Composable () -> Unit, ) { Column( Modifier.padding( Loading @@ -154,12 +158,14 @@ fun LazyCategory( verticalArrangement = Arrangement.spacedBy(SettingsDimension.paddingTiny), state = state, ) { item { CompositionLocalProvider(LocalIsInCategory provides true) { content() } } item { CompositionLocalProvider(LocalIsInCategory provides true) { header() } } items(count = list.size, key = key) { title?.invoke(it)?.let { title -> CategoryTitle(title) } CompositionLocalProvider(LocalIsInCategory provides true) { entry(it)() } } item { CompositionLocalProvider(LocalIsInCategory provides true) { footer() } } } } } Loading Loading @@ -189,3 +195,28 @@ private fun CategoryPreview() { } } } @Preview @Composable private fun LazyCategoryPreview() { SettingsTheme { LazyCategory( list = listOf(1, 2, 3), entry = { key -> @Composable { Preference( object : PreferenceModel { override val title = key.toString() } ) } }, footer = @Composable { Footer("Footer") }, header = @Composable { Text("Header") }, ) } } packages/SettingsLib/Spa/tests/src/com/android/settingslib/spa/widget/ui/CategoryTest.kt +12 −5 Original line number Diff line number Diff line Loading @@ -71,10 +71,17 @@ class CategoryTest { } @Test fun lazyCategory_content_displayed() { fun lazyCategory_headerDisplayed() { composeTestRule.setContent { TestLazyCategory() } composeTestRule.onNodeWithText("text").assertExists() composeTestRule.onNodeWithText("Header").assertExists() } @Test fun lazyCategory_footerDisplayed() { composeTestRule.setContent { TestLazyCategory() } composeTestRule.onNodeWithText("Footer").assertExists() } @Test Loading Loading @@ -102,8 +109,8 @@ private fun TestLazyCategory() { list = list, entry = { index: Int -> @Composable { Preference(list[index]) } }, title = { index: Int -> if (index == 0) "LazyCategory $index" else null }, ) { Text("text") } footer = @Composable { Footer("Footer") }, header = @Composable { Text("Header") }, ) } } Loading
packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/ui/Category.kt +34 −3 Original line number Diff line number Diff line Loading @@ -78,6 +78,8 @@ fun CategoryTitle(title: String) { /** * A container that is used to group similar items. A [Category] displays a [CategoryTitle] and * visually separates groups of items. * * @param content The content of the category. */ @Composable fun Category( Loading Loading @@ -126,7 +128,8 @@ fun Category( * be decided by the index. * @param bottomPadding Optional. Bottom outside padding of the category. * @param state Optional. State of LazyList. * @param content Optional. Content to be shown at the top of the category. * @param footer Optional. Content to be shown at the bottom of the category. * @param header Optional. Content to be shown at the top of the category. */ @Composable fun LazyCategory( Loading @@ -136,7 +139,8 @@ fun LazyCategory( title: ((Int) -> String?)? = null, bottomPadding: Dp = SettingsDimension.paddingSmall, state: LazyListState = rememberLazyListState(), content: @Composable () -> Unit, footer: @Composable () -> Unit = {}, header: @Composable () -> Unit, ) { Column( Modifier.padding( Loading @@ -154,12 +158,14 @@ fun LazyCategory( verticalArrangement = Arrangement.spacedBy(SettingsDimension.paddingTiny), state = state, ) { item { CompositionLocalProvider(LocalIsInCategory provides true) { content() } } item { CompositionLocalProvider(LocalIsInCategory provides true) { header() } } items(count = list.size, key = key) { title?.invoke(it)?.let { title -> CategoryTitle(title) } CompositionLocalProvider(LocalIsInCategory provides true) { entry(it)() } } item { CompositionLocalProvider(LocalIsInCategory provides true) { footer() } } } } } Loading Loading @@ -189,3 +195,28 @@ private fun CategoryPreview() { } } } @Preview @Composable private fun LazyCategoryPreview() { SettingsTheme { LazyCategory( list = listOf(1, 2, 3), entry = { key -> @Composable { Preference( object : PreferenceModel { override val title = key.toString() } ) } }, footer = @Composable { Footer("Footer") }, header = @Composable { Text("Header") }, ) } }
packages/SettingsLib/Spa/tests/src/com/android/settingslib/spa/widget/ui/CategoryTest.kt +12 −5 Original line number Diff line number Diff line Loading @@ -71,10 +71,17 @@ class CategoryTest { } @Test fun lazyCategory_content_displayed() { fun lazyCategory_headerDisplayed() { composeTestRule.setContent { TestLazyCategory() } composeTestRule.onNodeWithText("text").assertExists() composeTestRule.onNodeWithText("Header").assertExists() } @Test fun lazyCategory_footerDisplayed() { composeTestRule.setContent { TestLazyCategory() } composeTestRule.onNodeWithText("Footer").assertExists() } @Test Loading Loading @@ -102,8 +109,8 @@ private fun TestLazyCategory() { list = list, entry = { index: Int -> @Composable { Preference(list[index]) } }, title = { index: Int -> if (index == 0) "LazyCategory $index" else null }, ) { Text("text") } footer = @Composable { Footer("Footer") }, header = @Composable { Text("Header") }, ) } }