@@ -3,63 +3,82 @@ package com.omega_r.libs.omegatypes
33import android.app.Activity
44import android.content.Context
55import android.content.res.Resources
6+ import android.os.Build
7+ import android.text.SpannableString
8+ import android.text.SpannableStringBuilder
9+ import android.text.style.ForegroundColorSpan
610import android.widget.EditText
711import android.widget.TextView
812import android.widget.Toast
913import java.io.Serializable
1014import java.util.*
1115import kotlin.text.StringBuilder
1216
13- open class Text : Serializable {
14-
15- open fun isEmpty (): Boolean = true
16- open fun getString (resources : Resources ): String? = null
17-
18- override fun equals (other : Any? ): Boolean {
19- if (this == = other) return true
20- if (javaClass != other?.javaClass) return false
21-
22- other as Text
23-
24- return other.isEmpty()
25- }
26-
27- override fun hashCode (): Int {
28- return 31 * 17
29- }
17+ open class Text (protected val defaultTextStyle : TextStyle ? ) : Serializable {
3018
3119 companion object {
3220 @JvmStatic
33- fun empty (): Text = Text ()
21+ fun empty (): Text = Text (null )
3422
3523 @JvmStatic
36- fun from (string : String ): Text = StringText (string)
24+ fun from (string : String , textStyle : TextStyle ? = null ): Text = StringText (string, textStyle )
3725
3826 @JvmStatic
39- fun from (stringRes : Int ): Text = ResourceText (stringRes)
27+ fun from (stringRes : Int , textStyle : TextStyle ? = null ): Text = ResourceText (stringRes, textStyle )
4028
4129 @JvmStatic
42- fun from (stringRes : Int , vararg formatArgs : Any ): Text = FormatResourceText (stringRes, * formatArgs)
30+ fun from (stringRes : Int , vararg formatArgs : Any , textStyle : TextStyle ? = null ): Text = FormatResourceText (stringRes, * formatArgs, textStyle = textStyle )
4331
4432 @JvmStatic
45- fun from (stringHolder : StringHolder ): Text = stringHolder.getStringText()?.let { StringText (it) } ? : empty()
33+ fun from (stringHolder : StringHolder , textStyle : TextStyle ? = null ): Text = stringHolder.getStringText()?.let { from (it, textStyle ) } ? : empty()
4634
4735 @JvmStatic
48- fun from (throwable : Throwable ): Text = StringText (throwable.message)
36+ fun from (throwable : Throwable , textStyle : TextStyle ? = null ): Text = StringText (throwable.message, textStyle )
4937
5038 @JvmStatic
51- fun from (vararg texts : Text ): Text = ArrayText (* texts)
39+ fun from (vararg texts : Text , textStyle : TextStyle ? = null ): Text = ArrayText (* texts, textStyle = textStyle )
5240
5341 @JvmStatic
54- fun from (texts : List <Text >): Text = ArrayText (* texts.toTypedArray())
42+ fun from (texts : List <Text >, textStyle : TextStyle ? = null ): Text = ArrayText (* texts.toTypedArray(), textStyle = textStyle )
5543
5644 }
5745
58- private class StringText internal constructor(private val string : String? ) : Text() {
46+ open fun isEmpty (): Boolean = true
47+
48+ open fun getString (context : Context ): String? = null
49+
50+ open fun getCharSequence (context : Context , textStyle : TextStyle ? = null): CharSequence? {
51+ return getString(context)?.let {
52+ (defaultTextStyle + textStyle)?.applyStyle(context, it) ? : it
53+ }
54+ }
55+
56+ open fun applyTo (textView : TextView , textStyle : TextStyle ? = null) {
57+ textView.text = getCharSequence(textView.context, textStyle)
58+ }
59+
60+ override fun equals (other : Any? ): Boolean {
61+ if (this == = other) return true
62+ if (javaClass != other?.javaClass) return false
63+
64+ other as Text
65+
66+ return other.isEmpty()
67+ }
68+
69+ override fun hashCode (): Int {
70+ return 31 * 17
71+ }
72+
73+
74+ private class StringText internal constructor(
75+ private val string : String? ,
76+ textStyle : TextStyle ?
77+ ) : Text(textStyle) {
5978
6079 override fun isEmpty (): Boolean = string.isNullOrEmpty()
6180
62- override fun getString (resources : Resources ): String? {
81+ override fun getString (context : Context ): String? {
6382 return string
6483 }
6584
@@ -80,12 +99,16 @@ open class Text : Serializable {
8099
81100 }
82101
83- private class ResourceText internal constructor(private val stringRes : Int ) : Text() {
102+ private class ResourceText internal constructor(
103+ private val stringRes : Int ,
104+ textStyle : TextStyle ?
105+
106+ ) : Text(textStyle) {
84107
85108 override fun isEmpty (): Boolean = stringRes <= 0
86109
87- override fun getString (resources : Resources ): String {
88- return resources .getString(stringRes)
110+ override fun getString (context : Context ): String {
111+ return context .getString(stringRes)
89112 }
90113
91114 override fun equals (other : Any? ): Boolean {
@@ -106,12 +129,13 @@ open class Text : Serializable {
106129 }
107130
108131 private class FormatResourceText internal constructor(private val stringRes : Int ,
109- private vararg val formatArgs : Any ) : Text() {
132+ private vararg val formatArgs : Any ,
133+ textStyle : TextStyle ? ) : Text(textStyle) {
110134
111135 override fun isEmpty (): Boolean = stringRes <= 0
112136
113- override fun getString (resources : Resources ): String {
114- return resources .getString(stringRes, * formatArgs)
137+ override fun getString (context : Context ): String {
138+ return context .getString(stringRes, * formatArgs)
115139 }
116140
117141 override fun equals (other : Any? ): Boolean {
@@ -131,7 +155,10 @@ open class Text : Serializable {
131155
132156 }
133157
134- private class ArrayText internal constructor(private vararg val texts : Text ): Text() {
158+ private class ArrayText internal constructor(
159+ private vararg val texts : Text ,
160+ textStyle : TextStyle ?
161+ ): Text(textStyle) {
135162
136163 override fun isEmpty (): Boolean {
137164 if (texts.isEmpty()) return false
@@ -143,14 +170,23 @@ open class Text : Serializable {
143170 return true
144171 }
145172
146- override fun getString (resources : Resources ): String? {
173+ override fun getString (context : Context ): String? {
147174 val stringBuilder = StringBuilder ()
148175 for (text in texts) {
149- stringBuilder.append(text.getString(resources ))
176+ stringBuilder.append(text.getString(context ))
150177 }
151178 return stringBuilder.toString()
152179 }
153180
181+ override fun getCharSequence (context : Context , textStyle : TextStyle ? ): CharSequence? {
182+ val stringBuilder = SpannableStringBuilder ()
183+ val newTextStyle = defaultTextStyle + textStyle
184+ for (text in texts) {
185+ stringBuilder.append(text.getCharSequence(context, newTextStyle))
186+ }
187+ return stringBuilder
188+ }
189+
154190 override fun equals (other : Any? ): Boolean {
155191 if (this == = other) return true
156192 if (javaClass != other?.javaClass) return false
@@ -180,32 +216,36 @@ open class Text : Serializable {
180216
181217}
182218
183- fun TextView.setText (text : Text ? ) {
184- this .text = text?.getString(this .resources)
219+ fun TextView.setText (text : Text ? , textStyle : TextStyle ? = null) {
220+ if (text == null ) {
221+ this .text = null
222+ } else {
223+ text.applyTo(this , textStyle)
224+ }
185225}
186226
187- fun EditText.setError (text : Text ? ) {
188- this .error = text?.getString( this .resources )
227+ fun EditText.setError (text : Text ? , textStyle : TextStyle ? = null ) {
228+ this .error = text?.getCharSequence(context, textStyle )
189229}
190230
191- fun EditText.setHint (text : Text ? ) {
192- this .hint = text?.getString( this .resources )
231+ fun EditText.setHint (text : Text ? , textStyle : TextStyle ? = null ) {
232+ this .hint = text?.getCharSequence(context, textStyle )
193233}
194234
195- fun Text?.applyTo (textView : TextView ) {
196- textView.setText(this )
235+ fun Text?.applyTo (textView : TextView , textStyle : TextStyle ? = null ) {
236+ textView.setText(this , textStyle )
197237}
198238
199- fun Text?.applyErrorTo (editText : EditText ) {
200- editText.setError(this )
239+ fun Text?.applyErrorTo (editText : EditText , textStyle : TextStyle ? = null ) {
240+ editText.setError(this , textStyle )
201241}
202242
203- fun Activity.setTitle (text : Text ? ) {
204- title = text?.getString(resources )
243+ fun Activity.setTitle (text : Text ? , textStyle : TextStyle ? = null ) {
244+ title = text?.getCharSequence( this , textStyle )
205245}
206246
207- fun Context.toast (text : Text , duration : Int = Toast .LENGTH_SHORT ): Toast {
208- return Toast .makeText(this , text.getString(resources ), duration).apply { show() }
247+ fun Context.toast (text : Text , duration : Int = Toast .LENGTH_SHORT , textStyle : TextStyle ? = null ): Toast {
248+ return Toast .makeText(this , text.getCharSequence( this , textStyle ), duration).apply { show() }
209249}
210250
211251operator fun Text.plus (text : Text ) : Text {
0 commit comments