Repository: Automattic/pocket-casts-android
File: modules/features/account/src/main/java/au/com/shiftyjelly/pocketcasts/account/onboarding/upgrade/OnboardingUpgradeScreen.kt (Kotlin)

Code before:
private fun UpgradeContent(
    pages: List<UpgradePagerContent>,
    modifier: Modifier = Modifier,
) {
    val coroutineScope = rememberCoroutineScope()
    val listState = rememberLazyListState()
    FadedLazyColumn(
        modifier = modifier,
        state = listState,
    ) {
        itemsIndexed(pages) { index, page ->
            when (page) {
                is UpgradePagerContent.Features -> FeaturesContent(
                    features = page,
                    onCtaClick = {
                        coroutineScope.launch {
                            listState.scrollToItem(pages.size - index)
                        }
                    },
                )

                is UpgradePagerContent.TrialSchedule -> ScheduleContent(
                    trialSchedule = page,
                    onCtaClick = {
                        coroutineScope.launch {
                            listState.scrollToItem(pages.size - index)
                        }
                    },
                )
            }
        }
    }
}

@Composable
private fun FeaturesContent(
    features: UpgradePagerContent.Features,
    onCtaClick: () -> Unit,
) {
    Column {
        features.features.forEach { item ->
            UpgradeFeatureItem(
                item = item,
                iconColor = MaterialTheme.theme.colors.primaryText01,
                textColor = MaterialTheme.theme.colors.secondaryText02,
            )
        }
        if (features.showCta) {
            TextP40(
                text = stringResource(LR.string.onboarding_upgrade_features_trial_schedule),
                modifier = Modifier

Review comment (suggestion):
This logic isn't quite right. If there are 2 pages, the results will be:

* Page 0: `2 - 0` → `2`. There is no page 2, but it will scroll to the last item because `scrollToItem` has graceful logic.
* Page 1: `2 - 1` → `1`. It doesn't scrolls anywhere because the page scrolls to itself.

We need something like `val nextIndex = (index + 1) % pages.size`.

Code after:
private fun UpgradeContent(
    pages: List<UpgradePagerContent>,
    modifier: Modifier = Modifier,
) {
    val coroutineScope = rememberCoroutineScope()
    val listState = rememberLazyListState()
    FadedLazyColumn(
        modifier = modifier,
        state = listState,
    ) {
        itemsIndexed(pages) { index, page ->
            val scrollToNext: () -> Unit = {
                coroutineScope.launch {
                    listState.animateScrollToItem((index + 1) % pages.size)
                }
            }
            when (page) {
                is UpgradePagerContent.Features -> FeaturesContent(
                    features = page,
                    onCtaClick = scrollToNext,
                )

                is UpgradePagerContent.TrialSchedule -> ScheduleContent(
                    trialSchedule = page,
                    onCtaClick = scrollToNext,
                )
            }
        }
    }
}

@Composable
private fun FeaturesContent(
    features: UpgradePagerContent.Features,
    onCtaClick: () -> Unit,
) {
    Column {
        features.features.forEach { item ->
            UpgradeFeatureItem(
                item = item,
                iconColor = MaterialTheme.theme.colors.primaryText01,
                textColor = MaterialTheme.theme.colors.secondaryText02,
            )
        }
        if (features.showCta) {
            TextP40(
                text = stringResource(LR.string.onboarding_upgrade_features_trial_schedule),
                modifier = Modifier
                    .padding(vertical = 24.dp)
                    .clickable { onCtaClick() },
                color = MaterialTheme.theme.colors.primaryInteractive01,