Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on printscreen when I have another text view class #763

Closed
danielvilha opened this issue May 2, 2023 · 4 comments
Closed

Error on printscreen when I have another text view class #763

danielvilha opened this issue May 2, 2023 · 4 comments
Labels
bug Something isn't working
Milestone

Comments

@danielvilha
Copy link

danielvilha commented May 2, 2023

Description
I'm migrating from MVVM to MVI Compose, and in one of the screens I have a string where I have two texts in bold. Well, my final code is working, however when I generate my printscreen, my screen generates after the first text that should be in bold.

Steps to Reproduce
In my string
<string name="lorem">Lorem ipsum dolor sit amet, <b>consectetur adipiscing elit</b>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. <b>Ut enim ad minim veniam</b>, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</string>

StringResources.kt file
@Composable
@ReadOnlyComposable
private fun getCurrentResources(): Resources = LocalContext.current.resources

fun Spanned.toHtmlWithoutParagraphs(): String {
return HtmlCompat.toHtml(this, HtmlCompat.TO_HTML_PARAGRAPH_LINES_CONSECUTIVE)
.substringAfter("<p dir=\"ltr\">").substringBeforeLast("</p>")
}

fun Resources.getText(@StringRes id: Int, vararg args: Any): CharSequence {
val escapedArgs = args.map {
if (it is Spanned) it.toHtmlWithoutParagraphs() else it
}.toTypedArray()
val resource = SpannedString(getText(id))
val htmlResource = resource.toHtmlWithoutParagraphs()
val formattedHtml = String.format(htmlResource, *escapedArgs)
return HtmlCompat.fromHtml(formattedHtml, HtmlCompat.FROM_HTML_MODE_LEGACY)
}

@Composable
fun annotatedStringResource(@StringRes id: Int, vararg formatArgs: Any): AnnotatedString {
val resources = getCurrentResources()
val density = LocalDensity.current
return remember(id, formatArgs) {
val text = resources.getText(id, *formatArgs)
spannableStringToAnnotatedString(text, density)
}
}

@Composable
fun annotatedStringResource(@StringRes id: Int): AnnotatedString {
val resources = getCurrentResources()
val density = LocalDensity.current
return remember(id) {
val text = resources.getText(id)
spannableStringToAnnotatedString(text, density)
}
}

private fun spannableStringToAnnotatedString(
text: CharSequence,
density: Density,
): AnnotatedString {
return if (text is Spanned) {
with(density) {
buildAnnotatedString {
append("$text")
text.getSpans(0, text.length, Any::class.java).forEach {
val start = text.getSpanStart(it)
val end = text.getSpanEnd(it)
when (it) {
is StyleSpan -> when (it.style) {
Typeface.NORMAL -> addStyle(
SpanStyle(
fontWeight = FontWeight.Normal,
fontStyle = FontStyle.Normal,
),
start,
end,
)

Typeface.BOLD -> addStyle(
SpanStyle(
fontWeight = FontWeight.Bold,
fontStyle = FontStyle.Normal,
),
start,
end,
)

Typeface.ITALIC -> addStyle(
SpanStyle(
fontWeight = FontWeight.Normal,
fontStyle = FontStyle.Italic,
),
start,
end,
)

Typeface.BOLD_ITALIC -> addStyle(
SpanStyle(
fontWeight = FontWeight.Bold,
fontStyle = FontStyle.Italic,
),
start,
end,
)
}

is TypefaceSpan -> addStyle(
SpanStyle(
fontFamily = when (it.family) {
FontFamily.SansSerif.name -> FontFamily.SansSerif
FontFamily.Serif.name -> FontFamily.Serif
FontFamily.Monospace.name -> FontFamily.Monospace
FontFamily.Cursive.name -> FontFamily.Cursive
else -> FontFamily.Default
},
),
start,
end,
)

is BulletSpan -> {
addStyle(SpanStyle(), start, end)
}

is AbsoluteSizeSpan -> addStyle(
SpanStyle(fontSize = if (it.dip) it.size.dp.toSp() else it.size.toSp()),
start,
end,
)

is RelativeSizeSpan -> addStyle(
SpanStyle(fontSize = it.sizeChange.em),
start,
end,
)

is StrikethroughSpan -> addStyle(
SpanStyle(textDecoration = TextDecoration.LineThrough),
start,
end,
)

is UnderlineSpan -> addStyle(
SpanStyle(textDecoration = TextDecoration.Underline),
start,
end,
)

is SuperscriptSpan -> addStyle(
SpanStyle(baselineShift = BaselineShift.Superscript),
start,
end,
)

is SubscriptSpan -> addStyle(
SpanStyle(baselineShift = BaselineShift.Subscript),
start,
end,
)

is ForegroundColorSpan -> addStyle(
SpanStyle(color = Color(it.foregroundColor)),
start,
end,
)

else -> addStyle(SpanStyle(), start, end)
}
}
}
}
} else {
AnnotatedString("$text")
}
}

@LightDarkPreview
@Composable
fun VtuScanHelpPreview() {
VtuScanHelpScreen()
}

/**
* Compose Screen UI.
*/
@Composable
fun VtuScanHelpScreen() {
VzcTheme {
Content()
}
}

My Screen.kt file
@Composable
private fun Content() {
Surface {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
) {
Text(
annotatedStringResource(
R.string.lorem,
),
)
}
}
}

Expected behavior
When I run my app it works fine with bold texts. In my Screen.kt design class it only shows me the text without the bold, but when I run ./gradlew clean lintReportDebug it only shows my text Lorem ipsum dolor sit amet, consectetur adipiscing elit without the bold defined in my string.

Additional information:

  • Paparazzi Version: 1.2.0
  • OS: Android API 33
  • Compile SDK: corretto version 11.0.19
  • Gradle Version: 7.3.0
  • Android Gradle Plugin Version:

Screenshots
Screenshot 2023-05-02 at 15 35 46
Screenshot 2023-05-02 at 15 35 07
Screenshot 2023-05-02 at 15 44 32

@danielvilha danielvilha added the bug Something isn't working label May 2, 2023
@TWiStErRob
Copy link
Contributor

The cutoff in the last screenshot reminds me of #316.

@jrodbx jrodbx added this to the 1.3.1 milestone Jun 10, 2023
@jrodbx jrodbx self-assigned this Jun 10, 2023
@jrodbx jrodbx removed their assignment Jul 6, 2023
@kevinzheng-ap
Copy link
Collaborator

@danielvilha For the truncated text in snapshot from Paparazzi, it is fixed on master when enabling the new resource loading mechanism, tracked in #524.

For the text not bold in both preview and Paparazzi, an issue has been filed to the layoutlib team and it is fixed in Android Studio Hedgehog

cc @jrodbx

@danielvilha
Copy link
Author

I will update my code. Thanks for letting me know.

@jrodbx
Copy link
Collaborator

jrodbx commented Jul 8, 2023

Closing as resolved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants