-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display server name in bold in server certificate error screen
- Loading branch information
Showing
6 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...ose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/resources/StringResources.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/text/AnnotatedStrings.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...common/src/test/kotlin/app/k9mail/core/ui/compose/common/resources/StringResourcesTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
) | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ose/common/src/test/kotlin/app/k9mail/core/ui/compose/common/text/AnnotatedStringsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters