fun bindCaption()

in app/src/main/java/com/amazonaws/services/chime/sdkdemo/adapter/CaptionAdapter.kt [53:89]


    fun bindCaption(caption: Caption, position: Int) {
        val speakerName = caption.speakerName ?: ""

        val captionTextBackgroundColor = caption.speakerName?.let { R.color.colorWhite } ?: R.color.colorMissingSpeaker
        binding.captionText.setBackgroundResource(captionTextBackgroundColor)
        binding.speakerName.text = speakerName
        binding.captionText.text = caption.content
        val spannable = SpannableString(caption.content)
        caption.entities?.let { contents ->
            // Highlight PII identified and redacted words.
            contents.forEach { word ->
                spannable.setSpan(
                    ForegroundColorSpan(Color.GREEN),
                    caption.content.indexOf(word),
                    caption.content.indexOf(word) + word.length,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                )
            }
        } ?: run { binding.captionText.setTextColor(Color.BLACK) }
        caption.items?.let { items ->
            // Underline words with low confidence score.
            items.forEach { item ->
                val word = item.content
                val hasLowConfidence = item.confidence?.let { it > LOWER_THRESHOLD && it < UPPER_THRESHOLD } ?: run { false }
                val isCorrectContentType = !word.startsWith(FILTERED_CAPTION_FIRST_INDEX) && item.type != TranscriptItemType.Punctuation
                if (hasLowConfidence && isCorrectContentType && caption.content.contains(word)) {
                    spannable.setSpan(
                        CustomUnderlineSpan(Color.RED, caption.content.indexOf(word), caption.content.indexOf(word) + word.length),
                        caption.content.indexOf(word),
                        caption.content.indexOf(word) + word.length,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                }
            }
        }
        binding.captionText.text = spannable
        binding.captionText.contentDescription = "caption-$position"
    }