Skip to content

Commit

Permalink
Display server name in bold in server certificate error screen
Browse files Browse the repository at this point in the history
  • Loading branch information
cketti committed Jan 11, 2024
1 parent e9ee254 commit c6bf02a
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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,
)
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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")
}
)
}
}
}
Original file line number Diff line number Diff line change
@@ -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,
),
)
}
}
4 changes: 4 additions & 0 deletions core/ui/compose/common/src/test/res/values/test_strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version='1.0' encoding='UTF-8'?>
<resources>
<string name="StringResourcesTest">prefix %s suffix</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
),
)
}
Expand Down

0 comments on commit c6bf02a

Please sign in to comment.