Repository: Automattic/pocket-casts-android
File: modules/features/transcripts/src/main/kotlin/au/com/shiftyjelly/pocketcasts/transcripts/ui/TranscriptLines.kt (Kotlin)

Code before:
    }
}

@Composable
private fun TranscriptLine(
    entryIndex: Int,
    entry: TranscriptEntry,
    searchState: SearchState,
    theme: TranscriptTheme,
    modifier: Modifier = Modifier,
) {
    val entryText = entry.text()
    val searchHighlights = remember(entryIndex, entryText, searchState) {
        val searchTermLength = searchState.searchTerm?.length ?: 0
        searchState
            .searchResultIndices[entryIndex]
            ?.map { start -> start to start + searchTermLength }
            ?.filter { (start, end) -> isValidHighlightRange(start, end, entryText.length) }
            .orEmpty()
    }

    Text(
        text = buildAnnotatedString {
            append(entryText)
            searchHighlights.forEach { (startIndex, endIndex) ->
                val highlightCoordinates = entryIndex to startIndex
                val style = if (highlightCoordinates == searchState.selectedSearchCoordinates) {
                    theme.searchHighlightSpanStyle
                } else {
                    theme.searchDefaultSpanStyle
                }
                addStyle(style, startIndex, endIndex)
            }
        },
        style = entry.textStyle(),
        color = theme.text,
        modifier = modifier,
    )
}

private fun TranscriptEntry.text() = when (this) {
    is TranscriptEntry.Text -> value
    is TranscriptEntry.Speaker -> name
}

private fun TranscriptEntry.textStyle() = when (this) {
    is TranscriptEntry.Text -> SimpleTextStyle
    is TranscriptEntry.Speaker -> SpeakerTextStyle
}

private fun TranscriptEntry.padding() = when (this) {

Review comment (suggestion):
⛏️ no need to deconstruct the pair here if you would reconstruct it later for `hightlightCoordinates`

Code after:
        }
    }
}

@Composable
private fun TranscriptLine(
    entryIndex: Int,
    entry: TranscriptEntry,
    searchState: SearchState,
    theme: TranscriptTheme,
    modifier: Modifier = Modifier,
) {
    val entryText = entry.text()
    val searchHighlights = remember(entryIndex, entryText, searchState) {
        val searchTermLength = searchState.searchTerm?.length ?: 0
        searchState
            .searchResultIndices[entryIndex]
            ?.map { start -> start to start + searchTermLength }
            ?.filter { (start, end) -> isValidHighlightRange(start, end, entryText.length) }
            .orEmpty()
    }

    Text(
        text = buildAnnotatedString {
            append(entryText)
            searchHighlights.forEach { (startIndex, endIndex) ->
                val highlightCoordinates = SearchCoordinates(
                    lineIndex = entryIndex,
                    matchIndex = startIndex,
                )
                val style = if (highlightCoordinates == searchState.selectedSearchCoordinates) {
                    theme.searchHighlightSpanStyle
                } else {
                    theme.searchDefaultSpanStyle
                }
                addStyle(style, startIndex, endIndex)
            }
        },
        style = entry.textStyle(),
        color = theme.text,
        modifier = modifier,
    )
}

private fun TranscriptEntry.text() = when (this) {
    is TranscriptEntry.Text -> value
    is TranscriptEntry.Speaker -> name
}

private fun TranscriptEntry.textStyle() = when (this) {
    is TranscriptEntry.Text -> SimpleTextStyle