Skip to content

Commit dc60713

Browse files
committed
Add join method for Text
1 parent b94ecc6 commit dc60713

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class MainActivity : BaseActivity() {
2828
textStyle = TextStyle.font(ResourcesCompat.getFont(this, R.font.noto_sans_semi_bold)!!)
2929
)
3030
text.applyTo(exampleTextView) // or exampleTextView.setText(text)
31+
32+
val list = listOf(Text.from("1"), Text.from("2"), Text.from("3"))
33+
34+
title = list.join(",", postfix = ".").getCharSequence(this)
35+
3136
val image = Image.from("https://avatars1.githubusercontent.com/u/28600571")
3237

3338
thread {

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@ package com.omega_r.libs.omegatypes
22

33
import android.app.Activity
44
import android.content.Context
5-
import android.content.res.Resources
6-
import android.os.Build
7-
import android.text.SpannableString
85
import android.text.SpannableStringBuilder
9-
import android.text.style.ForegroundColorSpan
106
import android.widget.EditText
117
import android.widget.TextView
128
import android.widget.Toast
139
import java.io.Serializable
1410
import 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+
288300
fun 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

340352
operator 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
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
<resources>
2+
<string name="hello_world">Hello world!</string>
23
</resources>

0 commit comments

Comments
 (0)