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

Code before:
    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
                    .padding(
                        top = 24.dp,

Review comment (refactor):
It could be extracted to a separate lambda and reused across different items.

```kotlin
val scrollToNext = {
  // Function body
}

onCtaClick = scrollToNext
```

Code after:
    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,
            )
        }