main

square/leakcanary

Last updated at: 29/12/2023 09:38

UiUtils.kt

TLDR

This file contains a single class called UiUtils with a single method called replaceUrlSpanWithAction.

Methods

replaceUrlSpanWithAction

This method takes a SpannableStringBuilder object called title and a lambda function called urlAction as parameters. It replaces all instances of URLSpan within the title object with clickable spans that invoke the urlAction lambda function when clicked. The urlAction lambda function takes a String parameter and returns another lambda function that takes no parameters and performs a specific action.

Classes

No classes found in the file.

package leakcanary.internal.activity.ui

import android.text.SpannableStringBuilder
import android.text.style.ClickableSpan
import android.text.style.URLSpan
import android.view.View

internal object UiUtils {

  internal fun replaceUrlSpanWithAction(
    title: SpannableStringBuilder,
    urlAction: (String) -> (() -> Unit)?
  ) {
    val urlSpans = title.getSpans(0, title.length, URLSpan::class.java)
    for (span in urlSpans) {
      val action: (() -> Unit)? = urlAction(span.url)
      if (action != null) {
        val start = title.getSpanStart(span)
        val end = title.getSpanEnd(span)
        val flags = title.getSpanFlags(span)
        title.removeSpan(span)
        val newSpan = object : ClickableSpan() {
          override fun onClick(widget: View) {
            action()
          }
        }
        title.setSpan(newSpan, start, end, flags)
      }
    }
  }
}