Display server name in bold in server certificate error screen

This commit is contained in:
cketti 2024-01-11 14:38:42 +01:00
parent e9ee254ec9
commit c6bf02ad9a
6 changed files with 112 additions and 2 deletions

View file

@ -0,0 +1,31 @@
package app.k9mail.core.ui.compose.common.resources
import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.buildAnnotatedString
private const val PLACE_HOLDER = "{placeHolder}"
/**
* Loads a string resource with a single string parameter that will be replaced with the given [AnnotatedString].
*/
@Composable
@ReadOnlyComposable
fun annotatedStringResource(@StringRes id: Int, argument: AnnotatedString): AnnotatedString {
val stringWithPlaceHolder = stringResource(id, PLACE_HOLDER)
return buildAnnotatedString {
val placeHolderStartIndex = stringWithPlaceHolder.indexOf(PLACE_HOLDER)
require(placeHolderStartIndex != -1)
append(text = stringWithPlaceHolder, start = 0, end = placeHolderStartIndex)
append(argument)
append(
text = stringWithPlaceHolder,
start = placeHolderStartIndex + PLACE_HOLDER.length,
end = stringWithPlaceHolder.length,
)
}
}

View file

@ -0,0 +1,18 @@
package app.k9mail.core.ui.compose.common.text
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 androidx.compose.ui.text.withStyle
/**
* Converts a [String] into an [AnnotatedString] with all of the text being bold.
*/
fun String.bold(): AnnotatedString {
return buildAnnotatedString {
withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
append(this@bold)
}
}
}

View file

@ -0,0 +1,28 @@
package app.k9mail.core.ui.compose.common.resources
import androidx.compose.ui.text.buildAnnotatedString
import app.k9mail.core.ui.compose.common.text.bold
import app.k9mail.core.ui.compose.testing.ComposeTest
import assertk.assertThat
import assertk.assertions.isEqualTo
import org.junit.Test
import app.k9mail.core.ui.compose.common.test.R
class StringResourcesTest : ComposeTest() {
@Test
fun `annotatedStringResource() with bold text`() = runComposeTest {
val argument = "text".bold()
setContent {
val result = annotatedStringResource(id = R.string.StringResourcesTest, argument)
assertThat(result).isEqualTo(
buildAnnotatedString {
append("prefix ")
append(argument)
append(" suffix")
}
)
}
}
}

View file

@ -0,0 +1,27 @@
package app.k9mail.core.ui.compose.common.text
import androidx.compose.ui.text.AnnotatedString.Range
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.font.FontWeight
import assertk.assertThat
import assertk.assertions.containsExactly
import assertk.assertions.isEqualTo
import kotlin.test.Test
class AnnotatedStringsTest {
@Test
fun bold() {
val input = "text"
val result = input.bold()
assertThat(result.toString()).isEqualTo(input)
assertThat(result.spanStyles).containsExactly(
Range(
item = SpanStyle(fontWeight = FontWeight.Bold),
start = 0,
end = input.length,
),
)
}
}

View file

@ -0,0 +1,4 @@
<?xml version='1.0' encoding='UTF-8'?>
<resources>
<string name="StringResourcesTest">prefix %s suffix</string>
</resources>

View file

@ -17,6 +17,8 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import app.k9mail.core.ui.compose.common.baseline.withBaseline
import app.k9mail.core.ui.compose.common.resources.annotatedStringResource
import app.k9mail.core.ui.compose.common.text.bold
import app.k9mail.core.ui.compose.designsystem.atom.Icon
import app.k9mail.core.ui.compose.designsystem.atom.text.TextBody1
import app.k9mail.core.ui.compose.designsystem.atom.text.TextHeadline4
@ -105,9 +107,9 @@ private fun CertificateErrorDescription(
serverNameFormatter: ServerNameFormatter,
) {
TextBody1(
text = stringResource(
text = annotatedStringResource(
id = R.string.account_server_certificate_unknown_error_description_format,
serverNameFormatter.format(certificateError.hostname),
serverNameFormatter.format(certificateError.hostname).bold(),
),
)
}