Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 714cfc56 authored by Chaohui Wang's avatar Chaohui Wang
Browse files

[Spa] Add LazyCategory footer

Since LazyCategory will take all remaining space on the page, so in
order to add a footer, need to be put into LazyCategory.

Bug: 401143659
Flag: EXEMPT library change
Test: visual - compose preview
Test: unit test
Change-Id: I5a5958c2231dca00c2292f682f53242bd77fb75a
parent f66a357b
Loading
Loading
Loading
Loading
+34 −3
Original line number Diff line number Diff line
@@ -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(
@@ -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(
@@ -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(
@@ -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() } }
        }
    }
}
@@ -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")
            },
        )
    }
}
+12 −5
Original line number Diff line number Diff line
@@ -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
@@ -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") },
        )
    }
}