Skip to content

Commit 9f6509b

Browse files
committed
Add support text style
1 parent 57c3c94 commit 9f6509b

File tree

6 files changed

+197
-61
lines changed

6 files changed

+197
-61
lines changed
-28 Bytes
Binary file not shown.

.idea/misc.xml

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import android.graphics.BitmapFactory
55
import android.os.Bundle
66
import android.widget.ImageView
77
import android.widget.TextView
8-
import com.omega_r.libs.omegatypes.Image
9-
import com.omega_r.libs.omegatypes.Text
10-
import com.omega_r.libs.omegatypes.applyTo
8+
import com.omega_r.libs.omegatypes.*
119
import com.omega_r.libs.omegatypes.picasso.from
1210
import kotlin.concurrent.thread
1311

@@ -19,7 +17,7 @@ class MainActivity : BaseActivity() {
1917
override fun onCreate(savedInstanceState: Bundle?) {
2018
super.onCreate(savedInstanceState)
2119
setContentView(R.layout.activity_main)
22-
val text = Text.from(R.string.hello_world)
20+
val text = Text.from("test ") + Text.from(R.string.hello_world, textStyle = TextStyle.bold())
2321
text.applyTo(exampleTextView) // or exampleTextView.setText(text)
2422
val image = Image.from("https://avatars1.githubusercontent.com/u/28600571")
2523

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

Lines changed: 90 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,82 @@ package com.omega_r.libs.omegatypes
33
import android.app.Activity
44
import android.content.Context
55
import 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
610
import android.widget.EditText
711
import android.widget.TextView
812
import android.widget.Toast
913
import java.io.Serializable
1014
import java.util.*
1115
import 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

211251
operator fun Text.plus(text: Text) : Text {

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.omega_r.libs.omegatypes
22

3+
import android.content.Context
34
import android.content.res.Resources
45

56
/**
@@ -62,7 +63,7 @@ class TextBuilder(capacity: Int) {
6263
}
6364

6465

65-
class BuilderText(private val textBuilder: TextBuilder): Text() {
66+
class BuilderText(private val textBuilder: TextBuilder): Text(null) {
6667

6768
constructor(text: Text) : this(TextBuilder(text))
6869

@@ -74,8 +75,12 @@ class TextBuilder(capacity: Int) {
7475
return true
7576
}
7677

77-
override fun getString(resources: Resources): String? {
78-
return textBuilder.toText().getString(resources)
78+
override fun getString(context: Context): String? {
79+
return textBuilder.toText().getString(context)
80+
}
81+
82+
override fun getCharSequence(context: Context, textStyle: TextStyle?): CharSequence? {
83+
return textBuilder.toText().getCharSequence(context, textStyle)
7984
}
8085

8186
override fun equals(other: Any?): Boolean {
@@ -110,7 +115,6 @@ class TextBuilder(capacity: Int) {
110115
textBuilder.append(string)
111116
return this
112117
}
113-
114-
115118
}
119+
116120
}

0 commit comments

Comments
 (0)