Skip to content

Commit

Permalink
Improve Quick Start stylize logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Horta committed Feb 5, 2024
1 parent dd1d1e9 commit b898590
Showing 1 changed file with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,31 @@ object QuickStartUtils {
val spanTagEnd = activityContext.getString(R.string.quick_start_span_end)
var formattedMessage = activityContext.getString(messageId, spanTagOpen, spanTagEnd)

val startOfHighlight = formattedMessage.indexOf(spanTagOpen)
// Use an html version of the message to get the start and end of the highlighted area otherwise the
// are will have offsets because of HTML tags like <b> or <i> from the original string resource that will
// be eventually removed when we convert the string to Spannable
var tempHtmlFormattedMessage = HtmlCompat.fromHtml(formattedMessage, HtmlCompat.FROM_HTML_MODE_COMPACT)
.toString()

val startOfHighlight = tempHtmlFormattedMessage.indexOf(spanTagOpen)

// remove the <span> tag
formattedMessage = formattedMessage.replaceFirst(spanTagOpen, "")
tempHtmlFormattedMessage = tempHtmlFormattedMessage.replaceFirst(spanTagOpen, "")
/**
* Some string resources contain whitespaces before and after the placeholder tags.
* For example: `Tap %1$s Customize %2$s to start` becomes `Tap <span> Customize </span> to start`.
* However, when we remove "<span>" the string ends up having two whitespaces.
*/
formattedMessage = formattedMessage.replaceFirst(" ", " ")
tempHtmlFormattedMessage = tempHtmlFormattedMessage.replaceFirst(" ", " ")

val endOfHighlight = tempHtmlFormattedMessage.indexOf(spanTagEnd)

val endOfHighlight = formattedMessage.indexOf(spanTagEnd)
// remove all span tags from the original formattedMessage
formattedMessage = formattedMessage.replace(spanTagOpen, "")
formattedMessage = formattedMessage.replace(spanTagEnd, "")

// remove the </span> tag
formattedMessage = formattedMessage.replaceFirst(spanTagEnd, "")
formattedMessage = formattedMessage.replaceFirst(" ", " ")
// remove any remaining double whitespaces
formattedMessage = formattedMessage.replace(" ", " ")

val mutableSpannedMessage = SpannableStringBuilder(
HtmlCompat.fromHtml(formattedMessage, HtmlCompat.FROM_HTML_MODE_COMPACT)
Expand Down

0 comments on commit b898590

Please sign in to comment.