@@ -14,7 +14,7 @@ import java.io.Serializable
1414import java.util.*
1515import kotlin.text.StringBuilder
1616
17- open class Text (protected val defaultTextStyle : TextStyle ? ) : Serializable {
17+ open class Text (protected val defaultTextStyle : TextStyle ? ) : Serializable {
1818
1919 companion object {
2020 @JvmStatic
@@ -24,6 +24,11 @@ open class Text(protected val defaultTextStyle: TextStyle? ) : Serializable {
2424 @JvmOverloads
2525 fun from (string : String , textStyle : TextStyle ? = null): Text = StringText (string, textStyle)
2626
27+
28+ @JvmStatic
29+ @JvmOverloads
30+ fun from (charSequence : CharSequence , textStyle : TextStyle ? = null): Text = CharSequenceText (charSequence, textStyle)
31+
2732 @JvmStatic
2833 @JvmOverloads
2934 fun from (stringRes : Int , textStyle : TextStyle ? = null): Text = ResourceText (stringRes, textStyle)
@@ -34,7 +39,8 @@ open class Text(protected val defaultTextStyle: TextStyle? ) : Serializable {
3439
3540 @JvmStatic
3641 @JvmOverloads
37- fun from (stringHolder : StringHolder , textStyle : TextStyle ? = null): Text = stringHolder.getStringText()?.let { from(it, textStyle) } ? : empty()
42+ fun from (stringHolder : StringHolder , textStyle : TextStyle ? = null): Text = stringHolder.getStringText()?.let { from(it, textStyle) }
43+ ? : empty()
3844
3945 @JvmStatic
4046 @JvmOverloads
@@ -54,16 +60,30 @@ open class Text(protected val defaultTextStyle: TextStyle? ) : Serializable {
5460
5561 open fun getString (context : Context ): String? = null
5662
63+ @JvmOverloads
5764 open fun getCharSequence (context : Context , textStyle : TextStyle ? = null): CharSequence? {
5865 return getString(context)?.let {
5966 (defaultTextStyle + textStyle)?.applyStyle(context, it) ? : it
6067 }
6168 }
6269
70+ @JvmOverloads
6371 open fun applyTo (textView : TextView , textStyle : TextStyle ? = null) {
6472 textView.text = getCharSequence(textView.context, textStyle)
6573 }
6674
75+ open operator fun plus (text : Text ): Text {
76+ return TextBuilder .BuilderText (this ) + text
77+ }
78+
79+ open operator fun plus (string : String ): Text {
80+ return TextBuilder .BuilderText (this ) + string
81+ }
82+
83+ operator fun plus (textStyle : TextStyle ? ): Text {
84+ return textStyle?.let { from(this , textStyle = textStyle) } ? : this
85+ }
86+
6787 override fun equals (other : Any? ): Boolean {
6888 if (this == = other) return true
6989 if (javaClass != other?.javaClass) return false
@@ -106,6 +126,38 @@ open class Text(protected val defaultTextStyle: TextStyle? ) : Serializable {
106126
107127 }
108128
129+
130+ private class CharSequenceText internal constructor(
131+ private val charSequence : CharSequence ,
132+ textStyle : TextStyle ? ) : Text(textStyle) {
133+
134+ override fun isEmpty (): Boolean = charSequence.isEmpty()
135+
136+ override fun getString (context : Context ): String? {
137+ return charSequence.toString()
138+ }
139+
140+ override fun getCharSequence (context : Context , textStyle : TextStyle ? ): CharSequence? {
141+ return (defaultTextStyle + textStyle)?.applyStyle(context, charSequence) ? : charSequence
142+ }
143+
144+ override fun equals (other : Any? ): Boolean {
145+ if (this == = other) return true
146+ if (javaClass != other?.javaClass) return false
147+
148+ other as CharSequenceText
149+
150+ return charSequence == other.charSequence
151+ }
152+
153+ override fun hashCode (): Int {
154+ var result = super .hashCode()
155+ result = 31 * result + charSequence.hashCode()
156+ return result
157+ }
158+
159+ }
160+
109161 private class ResourceText internal constructor(
110162 private val stringRes : Int ,
111163 textStyle : TextStyle ?
@@ -142,6 +194,16 @@ open class Text(protected val defaultTextStyle: TextStyle? ) : Serializable {
142194 override fun isEmpty (): Boolean = stringRes <= 0
143195
144196 override fun getString (context : Context ): String {
197+ if (formatArgs.firstOrNull { it is Text } != null ) {
198+ val list = formatArgs.map {
199+ when (it) {
200+ is Text -> it.getString(context)
201+ else -> it
202+ }
203+ }
204+ return context.getString(stringRes, * list.toTypedArray())
205+ }
206+
145207 return context.getString(stringRes, * formatArgs)
146208 }
147209
@@ -165,7 +227,7 @@ open class Text(protected val defaultTextStyle: TextStyle? ) : Serializable {
165227 private class ArrayText internal constructor(
166228 private vararg val texts : Text ,
167229 textStyle : TextStyle ?
168- ): Text(textStyle) {
230+ ) : Text(textStyle) {
169231
170232 override fun isEmpty (): Boolean {
171233 if (texts.isEmpty()) return false
@@ -255,20 +317,18 @@ fun Context.toast(text: Text, duration: Int = Toast.LENGTH_SHORT, textStyle: Tex
255317 return Toast .makeText(this , text.getCharSequence(this , textStyle), duration).apply { show() }
256318}
257319
258- operator fun Text.plus (text : Text ) : Text {
259- if (this is TextBuilder .BuilderText ) {
260- return this + text
261- }
262- return TextBuilder .BuilderText (this ) + text
263- }
320+ fun String.toText (textStyle : TextStyle ? = null) = Text .from(this , textStyle)
321+
322+ fun CharSequence.toText (textStyle : TextStyle ? = null) = Text .from(this , textStyle)
264323
265- operator fun Text.plus (string : String ) : Text {
266- if (this is TextBuilder .BuilderText ) {
267- return this + string
324+ operator fun Text?.plus (text : Text ? ): Text ? {
325+ return when {
326+ this == null -> text
327+ text == null -> this
328+ else -> this + text
268329 }
269- return TextBuilder .BuilderText (this ) + string
270330}
271331
272- fun String. toText (textStyle : TextStyle ? = null) = Text .from( this , textStyle)
273-
274- operator fun Text. plus ( textStyle : TextStyle ) = Text .from( this , textStyle = textStyle)
332+ operator fun Text?. plus (textStyle : TextStyle ? ): Text ? {
333+ return this ?. let { this + textStyle }
334+ }
0 commit comments