@@ -2,19 +2,15 @@ package com.omega_r.libs.omegatypes
22
33import android.app.Activity
44import android.content.Context
5- import android.content.res.Resources
6- import android.os.Build
7- import android.text.SpannableString
85import android.text.SpannableStringBuilder
9- import android.text.style.ForegroundColorSpan
106import android.widget.EditText
117import android.widget.TextView
128import android.widget.Toast
139import java.io.Serializable
1410import java.util.*
15- import kotlin.text.StringBuilder
1611
17- open class Text (protected val defaultTextStyle : TextStyle ? ) : Serializable {
12+ open class Text (protected val defaultTextStyle : TextStyle ? ) : Serializable, Textable {
13+
1814
1915 companion object {
2016 @JvmStatic
@@ -76,6 +72,10 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable {
7672 return TextBuilder .BuilderText (this ) + text
7773 }
7874
75+ open operator fun plus (text : Textable ): Text {
76+ return this + text.toText()
77+ }
78+
7979 open operator fun plus (string : String ): Text {
8080 return TextBuilder .BuilderText (this ) + string
8181 }
@@ -84,6 +84,10 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable {
8484 return textStyle?.let { from(this , textStyle = textStyle) } ? : this
8585 }
8686
87+ override fun toText (): Text {
88+ return this
89+ }
90+
8791 override fun equals (other : Any? ): Boolean {
8892 if (this == = other) return true
8993 if (javaClass != other?.javaClass) return false
@@ -283,8 +287,16 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable {
283287
284288 }
285289
290+
291+ }
292+
293+ interface Textable {
294+
295+ fun toText (): Text
296+
286297}
287298
299+
288300fun TextView.setText (text : Text ? , textStyle : TextStyle ? = null) {
289301 if (text == null ) {
290302 this .text = null
@@ -339,4 +351,25 @@ operator fun Text?.plus(string: String?): Text? {
339351
340352operator fun Text?.plus (textStyle : TextStyle ? ): Text ? {
341353 return this ?.let { this + textStyle }
354+ }
355+
356+ fun List<Textable>.join (separator : String = ", ", prefix : String = "", postfix : String = "", limit : Int = -1, truncated : String = "..."): Text {
357+ var buffer = if (prefix.isNotEmpty()) Text .from(prefix) else Text .from()
358+
359+ var count = 0
360+
361+ for (element in this ) {
362+ if (++ count > 1 ) buffer + = separator
363+ if (limit < 0 || count <= limit) {
364+ buffer + = element
365+ } else break
366+ }
367+
368+ if (limit in 0 until count) buffer + = truncated
369+
370+ if (postfix.isNotEmpty()) {
371+ buffer + = postfix
372+ }
373+
374+ return buffer
342375}
0 commit comments