Skip to content

Commit d764f26

Browse files
committed
Added multi text
1 parent 2ffdcc3 commit d764f26

File tree

2 files changed

+194
-5
lines changed

2 files changed

+194
-5
lines changed

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

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import android.widget.TextView
88
import android.widget.Toast
99
import java.io.Serializable
1010
import java.util.*
11+
import kotlin.text.StringBuilder
1112

12-
open class Text private constructor() : Serializable {
13+
open class Text : Serializable {
1314

1415
open fun isEmpty(): Boolean = true
1516
open fun getString(resources: Resources): String? = null
@@ -45,6 +46,13 @@ open class Text private constructor() : Serializable {
4546

4647
@JvmStatic
4748
fun from(throwable: Throwable): Text = StringText(throwable.message)
49+
50+
@JvmStatic
51+
fun from(vararg texts: Text): Text = ArrayText(*texts)
52+
53+
@JvmStatic
54+
fun from(texts: List<Text>): Text = ArrayText(*texts.toTypedArray())
55+
4856
}
4957

5058
private class StringText internal constructor(private val string: String?) : Text() {
@@ -65,7 +73,9 @@ open class Text private constructor() : Serializable {
6573
}
6674

6775
override fun hashCode(): Int {
68-
return 31 * 17 + (string?.hashCode() ?: 0)
76+
var result = super.hashCode()
77+
result = 31 * result + (string?.hashCode() ?: 0)
78+
return result
6979
}
7080

7181
}
@@ -88,7 +98,9 @@ open class Text private constructor() : Serializable {
8898
}
8999

90100
override fun hashCode(): Int {
91-
return 31 * 17 + stringRes
101+
var result = super.hashCode()
102+
result = 31 * result + stringRes
103+
return result
92104
}
93105

94106
}
@@ -112,13 +124,54 @@ open class Text private constructor() : Serializable {
112124
}
113125

114126
override fun hashCode(): Int {
115-
var result = 31 * 17 + stringRes
127+
var result = super.hashCode() + stringRes
116128
result = 31 * result + Arrays.hashCode(formatArgs)
117129
return result
118130
}
119131

120132
}
121133

134+
private class ArrayText internal constructor(private vararg val texts: Text): Text() {
135+
136+
override fun isEmpty(): Boolean {
137+
if (texts.isEmpty()) return false
138+
for (text in texts) {
139+
if (!text.isEmpty()) {
140+
return false
141+
}
142+
}
143+
return true
144+
}
145+
146+
override fun getString(resources: Resources): String? {
147+
val stringBuilder = StringBuilder()
148+
for (text in texts) {
149+
stringBuilder.append(text.getString(resources))
150+
}
151+
return stringBuilder.toString()
152+
}
153+
154+
override fun equals(other: Any?): Boolean {
155+
if (this === other) return true
156+
if (javaClass != other?.javaClass) return false
157+
if (!super.equals(other)) return false
158+
159+
other as ArrayText
160+
161+
if (!texts.contentEquals(other.texts)) return false
162+
163+
return true
164+
}
165+
166+
override fun hashCode(): Int {
167+
var result = super.hashCode()
168+
result = 31 * result + texts.contentHashCode()
169+
return result
170+
}
171+
172+
173+
}
174+
122175
interface StringHolder {
123176

124177
fun getStringText(): String?
@@ -135,6 +188,10 @@ fun EditText.setError(text: Text) {
135188
this.error = text.getString(this.resources)
136189
}
137190

191+
fun EditText.setHint(text: Text) {
192+
this.hint = text.getString(this.resources)
193+
}
194+
138195
fun Text.applyTo(textView: TextView) {
139196
textView.setText(this)
140197
}
@@ -149,4 +206,20 @@ fun Activity.setTitle(text: Text) {
149206

150207
fun Context.toast(text: Text, duration: Int = Toast.LENGTH_SHORT): Toast {
151208
return Toast.makeText(this, text.getString(resources), duration).apply { show() }
152-
}
209+
}
210+
211+
operator fun Text.plus(text: Text) : Text {
212+
if (this is TextBuilder.BuilderText) {
213+
return this + text
214+
}
215+
return TextBuilder.BuilderText(this) + text
216+
}
217+
218+
operator fun Text.plus(string: String) : Text {
219+
if (this is TextBuilder.BuilderText) {
220+
return this + string
221+
}
222+
return TextBuilder.BuilderText(this) + string
223+
}
224+
225+
fun String.toText() = Text.from(this)
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.omega_r.libs.omegatypes
2+
3+
import android.content.res.Resources
4+
5+
/**
6+
* Created by Anton Knyazev on 29.03.2019.
7+
*/
8+
private const val DEFAULT_CAPACITY = 10
9+
class TextBuilder(capacity: Int) {
10+
11+
private val list: MutableList<Text> = ArrayList(capacity)
12+
13+
constructor(textList: List<Text>): this(textList.size + DEFAULT_CAPACITY) {
14+
list.addAll(textList)
15+
}
16+
17+
constructor(vararg text: Text): this(text.size + DEFAULT_CAPACITY) {
18+
list.addAll(text)
19+
}
20+
21+
constructor(): this(DEFAULT_CAPACITY)
22+
23+
fun append(text: Text): TextBuilder {
24+
list += text
25+
return this
26+
}
27+
28+
fun append(string: String) = append(Text.from(string))
29+
30+
fun append(stringRes: Int) = append(Text.from(stringRes))
31+
32+
fun append(stringHolder: Text.StringHolder) = append(Text.from(stringHolder))
33+
34+
fun append(throwable: Throwable) = append(Text.from(throwable))
35+
36+
fun append(stringRes: Int, vararg formatArgs: Any) = append(Text.from(stringRes, formatArgs))
37+
38+
fun append(vararg texts: Text) = append(Text.from(*texts))
39+
40+
fun insert(text: Text, index: Int): TextBuilder {
41+
list.add(index, text)
42+
return this
43+
}
44+
45+
fun toText(): Text {
46+
return Text.from(list)
47+
}
48+
49+
override fun equals(other: Any?): Boolean {
50+
if (this === other) return true
51+
if (javaClass != other?.javaClass) return false
52+
53+
other as TextBuilder
54+
55+
if (list != other.list) return false
56+
57+
return true
58+
}
59+
60+
override fun hashCode(): Int {
61+
return list.hashCode()
62+
}
63+
64+
65+
class BuilderText(private val textBuilder: TextBuilder): Text() {
66+
67+
constructor(text: Text) : this(TextBuilder(text))
68+
69+
override fun isEmpty(): Boolean {
70+
if (textBuilder.list.isEmpty()) {
71+
return true
72+
}
73+
if (textBuilder.list.firstOrNull { !it.isEmpty() } != null) return false
74+
return false
75+
}
76+
77+
override fun getString(resources: Resources): String? {
78+
return textBuilder.toText().getString(resources)
79+
}
80+
81+
override fun equals(other: Any?): Boolean {
82+
if (this === other) return true
83+
if (javaClass != other?.javaClass) return false
84+
if (!super.equals(other)) return false
85+
86+
other as BuilderText
87+
88+
if (textBuilder != other.textBuilder) return false
89+
90+
return true
91+
}
92+
93+
override fun hashCode(): Int {
94+
var result = super.hashCode()
95+
result = 31 * result + textBuilder.hashCode()
96+
return result
97+
}
98+
99+
fun insert(text: Text, index: Int): BuilderText {
100+
textBuilder.insert(text, index)
101+
return this
102+
}
103+
104+
operator fun plus(text: Text): Text {
105+
textBuilder.append(text)
106+
return this
107+
}
108+
109+
operator fun plus(string: String): Text{
110+
textBuilder.append(string)
111+
return this
112+
}
113+
114+
115+
}
116+
}

0 commit comments

Comments
 (0)