Skip to content

Commit ae42598

Browse files
committed
Add support text format
1 parent 8c29ed4 commit ae42598

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

app/src/main/java/omega_r/com/omegatypesexample/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class MainActivity : BaseActivity() {
1616
override fun onCreate(savedInstanceState: Bundle?) {
1717
super.onCreate(savedInstanceState)
1818
setContentView(R.layout.activity_main)
19-
val text = Text.from("test ") + Text.from(R.string.hello_world, textStyle = TextStyle.bold())
19+
val text = Text.from("test ") + Text.from(R.string.hello_world, Text.from(R.string.app_name), textStyle = TextStyle.bold())
2020
text.applyTo(exampleTextView) // or exampleTextView.setText(text)
2121
val image = Image.from("https://avatars1.githubusercontent.com/u/28600571")
2222

omegatypes/src/main/java/com/omega_r/libs/omegatypes/Text.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ open class Text(protected val defaultTextStyle: TextStyle? ) : Serializable {
142142
override fun isEmpty(): Boolean = stringRes <= 0
143143

144144
override fun getString(context: Context): String {
145+
if (formatArgs.firstOrNull { it is Text} != null) {
146+
val list = formatArgs.map {
147+
when (it) {
148+
is Text -> it.getString(context)
149+
else -> it
150+
}
151+
}
152+
return context.getString(stringRes, *list.toTypedArray())
153+
}
154+
145155
return context.getString(stringRes, *formatArgs)
146156
}
147157

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<resources>
22
<string name="app_name">lib</string>
3-
<string name="hello_world">Hello world</string>
3+
<string name="hello_world">Hello world %s super</string>
44
</resources>

0 commit comments

Comments
 (0)