Repository: android/compose-samples
File: JetNews/app/src/main/java/com/example/jetnews/ui/interests/InterestsScreen.kt (Kotlin)

Code before:
        color = MaterialTheme.colors.onSurface.copy(alpha = 0.1f)
    )
}

/**
 * TabRow for the InterestsScreen
 */
@Composable
private fun InterestsTabRow(
    selectedTabIndex: Int,
    updateSection: (Sections) -> Unit,
    tabContent: List<TabContent>
) {
    // TODO: Make the tabs narrower and aligned to the left in large screens
    TabRow(
        selectedTabIndex = selectedTabIndex,
        backgroundColor = MaterialTheme.colors.onPrimary,
        contentColor = MaterialTheme.colors.primary
    ) {
        tabContent.forEachIndexed { index, tabContent ->
            val colorText = if (selectedTabIndex == index) {
                MaterialTheme.colors.primary
            } else {
                MaterialTheme.colors.onSurface.copy(alpha = 0.8f)
            }
            Tab(
                selected = selectedTabIndex == index,
                onClick = { updateSection(tabContent.section) },
                modifier = Modifier.heightIn(min = 48.dp)
            ) {
                Text(
                    text = stringResource(id = tabContent.section.titleResId),
                    color = colorText,
                    style = MaterialTheme.typography.subtitle2,
                    modifier = Modifier.paddingFromBaseline(top = 20.dp)
                )
            }
        }
    }
}

/**
 * Returns the max width for a Topic Item given maxWidth and number of columns constraints.
 */
@Composable
private fun rememberItemMaxWidth(windowMaxWidth: Dp, columns: Int) =
    remember(windowMaxWidth, columns) {
        if (columns == 1) {
            // If the space available is small, fill the screen
            if (windowMaxWidth < 600.dp) Dp.Infinity else 600.dp
        } else {

Review comment (suggestion):
Are these `remember`s a bit of a premature optimization? It seems like this logic should be pretty simple to compute, and it avoids forgetting a necessary key here.

Code after:
/**
 * Return the interest sections grouped given the number of columns that fill the screen.
 * Sections are distributed to balance the number of items on each column.
 */
@Composable
private fun rememberSectionsGroupedInColumns(
    sections: List<InterestSection>,
    columns: Int
): List<List<InterestSection>> = remember(sections, columns) {
    val sectionsInColumns = List<MutableList<InterestSection>>(columns) { mutableListOf() }
    val elementsInColumns = IntArray(columns)
    // For each section, find the column with less elements and add it there
    sections.forEach { section ->
        val shortestColumnIndex = elementsInColumns.withIndex().minByOrNull { it.value }!!.index
        sectionsInColumns[shortestColumnIndex] += section
        elementsInColumns[shortestColumnIndex] += section.interests.size
    }
    sectionsInColumns.toList()
}

/**
 * Returns the max width for a Topic Item given maxWidth and number of columns constraints.
 */
@Composable
private fun rememberItemMaxWidth(windowMaxWidth: Dp, columns: Int) =
    remember(windowMaxWidth, columns) {
        if (columns == 1) {
            // If the space available is small, fill the screen
            if (windowMaxWidth < 600.dp) Dp.Infinity else 600.dp
        } else {
            // Calculate the max width each item can take given the number of columns
            val itemMaxWidth = windowMaxWidth / columns
            // Limit the width of the item for them to not be too big given the available space
            if (windowMaxWidth < 800.dp) min(itemMaxWidth, 350.dp) else min(itemMaxWidth, 450.dp)
        }
    }

@Composable
fun rememberTabContent(
    interestsViewModel: InterestsViewModel
): List<TabContent> {
    // UiState of the InterestsScreen
    val uiState by interestsViewModel.uiState.collectAsState()

    // Describe the screen sections here since each section needs 2 states and 1 event.
    // Pass them to the stateless InterestsScreen using a tabContent.
    val topicsSection = TabContent(Sections.Topics) {
        val selectedTopics by interestsViewModel.selectedTopics.collectAsState()
        TopicList(
            topics = uiState.topics,
            selectedTopics = selectedTopics,