Skip to content

Commit

Permalink
Rename TextUtils to Strings (#5110)
Browse files Browse the repository at this point in the history
Co-authored-by: Siddharth Agarwal <sagarwal@rtsl.org>
  • Loading branch information
siddh1004 and Siddharth Agarwal authored Nov 7, 2024
1 parent e0c467a commit 85f16bd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import org.simple.clinic.R
import org.simple.clinic.common.ui.theme.SimpleTheme
import org.simple.clinic.common.ui.util.toAnnotatedString
import org.simple.clinic.util.toAnnotatedString


@Composable
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/org/simple/clinic/util/Strings.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package org.simple.clinic.util

import android.graphics.Typeface
import android.os.Build
import android.text.Html
import android.text.Spanned
import android.text.style.StyleSpan
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import java.util.UUID

fun String?.nullIfBlank(): String? {
Expand All @@ -17,3 +26,25 @@ fun String?.asUuid(): UUID? = try {
} catch (e: IllegalArgumentException) {
null
}

fun String.toAnnotatedString() = getSpannedText(this).toAnnotatedString()

private fun Spanned.toAnnotatedString(): AnnotatedString = buildAnnotatedString {
val spanned = this@toAnnotatedString
append(spanned.toString())
getSpans(0, spanned.length, Any::class.java).forEach { span ->
val start = getSpanStart(span)
val end = getSpanEnd(span)
if (span is StyleSpan && span.style == Typeface.BOLD) {
addStyle(SpanStyle(fontWeight = FontWeight.Bold), start, end)
}
}
}

private fun getSpannedText(text: String): Spanned {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT)
} else {
Html.fromHtml(text)
}
}

This file was deleted.

0 comments on commit 85f16bd

Please sign in to comment.