KotlinLeaner
05/01/2023, 2:15 PMList
of Pairs
. I am using this list in buildAnnotatedString
. Finding which one is just text and which one is link.
val listOfPair = listOf(
Pair("Text 1", "app"),
Pair("Text 2", "link"),
Pair("Text 3", "app"),
Pair("Text 4", "link"),
Pair("Text 5", "app"),
....//// more items in here
)
KotlinLeaner
05/01/2023, 2:16 PMval multipleString = buildAnnotatedString {
listOfPair.forEachIndexed { _, item ->
val (text, type) = item
if (type == "app") {
withStyle(style = SpanStyle(color = Color.DarkGray)) {
append("$text\n")
}
} else if (type == "link") {
withStyle(style = SpanStyle(color = Color.Blue)) {
append("$text\n")
}
}
}
}
Is there any better way to improve this?El Anthony
05/01/2023, 2:49 PMval typeToColorMap = mapOf(
"app" to Color.DarkGray,
"link" to Color.Blue
)
val multipleString2 = buildAnnotatedString {
listOfPair.forEach { item ->
val (text, type) = item
withStyle(style = SpanStyle(color = typeToColorMap[type])) {
append("$text\n")
}
}
}
KotlinLeaner
05/01/2023, 2:52 PMRuckus
05/01/2023, 3:02 PMlistOfPair.forEach { (text, type) ->
withStyle(style = SpanStyle(color = typeToColorMap[type])) {
append("$text\n")
}
}
KotlinLeaner
05/01/2023, 3:05 PMEl Anthony
05/01/2023, 3:13 PMenum class TypeToColorMap( val color: Color) {
app( Color.DarkGray),
link (Color.Blue)
}
val listOfPair = listOf(
Pair("Text 1", <http://TypeToColorMap.app|TypeToColorMap.app>),
Pair("Text 2", TypeToColorMap.link),
Pair("Text 3", <http://TypeToColorMap.app|TypeToColorMap.app>),
//....more items in here
)
val multipleString = buildAnnotatedString {
listOfPair.forEach { (text, type) ->
withStyle(style = SpanStyle(color = type.color)) {
AttributedStringBuilder.append("$text\n")
}
}
}
KotlinLeaner
05/01/2023, 3:16 PM