Skip to content

Commit 9525b0a

Browse files
Merge pull request #16 from Omega-R/develop
New release
2 parents 6c92411 + 5ece050 commit 9525b0a

File tree

7 files changed

+319
-33
lines changed

7 files changed

+319
-33
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class MainActivity : BaseActivity() {
1616
override fun onCreate(savedInstanceState: Bundle?) {
1717
super.onCreate(savedInstanceState)
1818
setContentView(R.layout.activity_main)
19-
val text = Text.from("test ") + Text.from(R.string.hello_world, textStyle = TextStyle.bold())
19+
val text = Text.from("test ") + Text.from(R.string.hello_world, Text.from(R.string.app_name), textStyle = TextStyle.bold())
2020
text.applyTo(exampleTextView) // or exampleTextView.setText(text)
2121
val image = Image.from("https://avatars1.githubusercontent.com/u/28600571")
2222

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.omega_r.libs.omegatypes
2+
3+
import android.content.Context
4+
import android.content.res.ColorStateList
5+
import android.os.Build
6+
import java.io.Serializable
7+
8+
/**
9+
* Created by Anton Knyazev on 18.05.2019.
10+
*/
11+
12+
abstract class Color : Serializable {
13+
14+
abstract fun getColorInt(context: Context): Int
15+
16+
open fun getColorStateList(context: Context): ColorStateList {
17+
return ColorStateList.valueOf(getColorInt(context))
18+
}
19+
20+
companion object {
21+
22+
@JvmStatic
23+
fun fromInt(color: Int): Color = IntColor(color)
24+
25+
@JvmStatic
26+
fun fromResource(colorRes: Int): Color = ResourceColor(colorRes)
27+
}
28+
29+
class IntColor(private val colorInt: Int) : Color() {
30+
override fun getColorInt(context: Context) = colorInt
31+
}
32+
33+
class ResourceColor(private val colorRes: Int) : Color() {
34+
35+
override fun getColorInt(context: Context): Int {
36+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
37+
context.getColor(colorRes)
38+
} else {
39+
context.resources.getColor(colorRes)
40+
}
41+
}
42+
43+
override fun getColorStateList(context: Context): ColorStateList {
44+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
45+
context.getColorStateList(colorRes)
46+
} else {
47+
context.resources.getColorStateList(colorRes)
48+
}
49+
}
50+
51+
}
52+
53+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.omega_r.libs.omegatypes
2+
3+
import android.content.Context
4+
import android.util.DisplayMetrics
5+
import android.util.TypedValue.*
6+
import java.io.Serializable
7+
8+
/**
9+
* Created by Anton Knyazev on 18.05.2019.
10+
*/
11+
abstract class Size : Serializable {
12+
13+
companion object {
14+
15+
@JvmStatic
16+
fun from(size: Float, unit: Unit): Size = SimpleSize(size, unit)
17+
18+
@JvmStatic
19+
fun from(dimensionRes: Int): Size = DimensionResourceSize(dimensionRes)
20+
21+
}
22+
23+
abstract fun getSize(context: Context, unit: Unit): Float
24+
25+
protected abstract fun getRawSize(context: Context): Float
26+
27+
fun getPixelOffset(context: Context): Int {
28+
return getSize(context, Unit.PX).toInt()
29+
}
30+
31+
fun getPixelSize(context: Context): Int {
32+
val f = getSize(context, Unit.PX)
33+
34+
// A size conversion involves rounding the base value, and ensuring that a
35+
// non-zero base value is at least one pixel in size.
36+
val res = (if (f >= 0) f + 0.5f else f - 0.5f).toInt()
37+
if (res != 0) return res
38+
val value = getRawSize(context)
39+
if (value == 0f) return 0
40+
if (value > 0) return 1
41+
return -1
42+
}
43+
44+
protected fun getFactor(displayMetrics: DisplayMetrics, unit: Unit): Float {
45+
return displayMetrics.run {
46+
when (unit) {
47+
Unit.PX -> 1.0f
48+
Unit.DP -> density
49+
Unit.SP -> scaledDensity
50+
Unit.PT -> xdpi * (1.0f / 72)
51+
Unit.IN -> xdpi
52+
Unit.MM -> xdpi * (1.0f / 25.4f)
53+
}
54+
}
55+
}
56+
57+
protected fun getFactor(displayMetrics: DisplayMetrics, oldUnit: Unit, newUnit: Unit): Float {
58+
if (oldUnit == newUnit) {
59+
return 1f
60+
}
61+
return getFactor(displayMetrics, newUnit) / getFactor(displayMetrics, oldUnit)
62+
}
63+
64+
protected fun getFactor(context: Context, oldUnit: Unit, newUnit: Unit): Float {
65+
return context.resources.displayMetrics.run {
66+
getFactor(this, newUnit) / getFactor(this, oldUnit)
67+
}
68+
}
69+
70+
protected fun Float.applyFactor(context: Context, oldUnit: Unit, newUnit: Unit): Float {
71+
return this * getFactor(context, oldUnit, newUnit)
72+
}
73+
74+
enum class Unit(val typedValueUnit: Int) {
75+
PX(COMPLEX_UNIT_PX),
76+
DP(COMPLEX_UNIT_DIP),
77+
SP(COMPLEX_UNIT_SP),
78+
PT(COMPLEX_UNIT_PT),
79+
IN(COMPLEX_UNIT_IN),
80+
MM(COMPLEX_UNIT_MM);
81+
82+
companion object {
83+
84+
@JvmStatic
85+
fun from(typedValueUnit: Int): Unit? {
86+
return values().firstOrNull { typedValueUnit == it.typedValueUnit }
87+
}
88+
89+
}
90+
91+
}
92+
93+
private class SimpleSize(private val size: Float, private val unit: Unit) : Size() {
94+
95+
override fun getRawSize(context: Context): Float = size
96+
97+
override fun getSize(context: Context, unit: Unit): Float {
98+
return size.applyFactor(context, this.unit, unit)
99+
}
100+
}
101+
102+
private class DimensionResourceSize(private val sizeRes: Int) : Size() {
103+
104+
override fun getSize(context: Context, unit: Unit) = context.resources.run {
105+
getDimension(sizeRes).applyFactor(context, Unit.PX, unit)
106+
}
107+
108+
override fun getRawSize(context: Context) = context.resources.getDimension(sizeRes)
109+
110+
}
111+
}
112+

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

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import java.io.Serializable
1414
import java.util.*
1515
import kotlin.text.StringBuilder
1616

17-
open class Text(protected val defaultTextStyle: TextStyle? ) : Serializable {
17+
open class Text(protected val defaultTextStyle: TextStyle?) : Serializable {
1818

1919
companion object {
2020
@JvmStatic
@@ -24,6 +24,11 @@ open class Text(protected val defaultTextStyle: TextStyle? ) : Serializable {
2424
@JvmOverloads
2525
fun from(string: String, textStyle: TextStyle? = null): Text = StringText(string, textStyle)
2626

27+
28+
@JvmStatic
29+
@JvmOverloads
30+
fun from(charSequence: CharSequence, textStyle: TextStyle? = null): Text = CharSequenceText(charSequence, textStyle)
31+
2732
@JvmStatic
2833
@JvmOverloads
2934
fun from(stringRes: Int, textStyle: TextStyle? = null): Text = ResourceText(stringRes, textStyle)
@@ -34,7 +39,8 @@ open class Text(protected val defaultTextStyle: TextStyle? ) : Serializable {
3439

3540
@JvmStatic
3641
@JvmOverloads
37-
fun from(stringHolder: StringHolder, textStyle: TextStyle? = null): Text = stringHolder.getStringText()?.let { from(it, textStyle) } ?: empty()
42+
fun from(stringHolder: StringHolder, textStyle: TextStyle? = null): Text = stringHolder.getStringText()?.let { from(it, textStyle) }
43+
?: empty()
3844

3945
@JvmStatic
4046
@JvmOverloads
@@ -54,16 +60,30 @@ open class Text(protected val defaultTextStyle: TextStyle? ) : Serializable {
5460

5561
open fun getString(context: Context): String? = null
5662

63+
@JvmOverloads
5764
open fun getCharSequence(context: Context, textStyle: TextStyle? = null): CharSequence? {
5865
return getString(context)?.let {
5966
(defaultTextStyle + textStyle)?.applyStyle(context, it) ?: it
6067
}
6168
}
6269

70+
@JvmOverloads
6371
open fun applyTo(textView: TextView, textStyle: TextStyle? = null) {
6472
textView.text = getCharSequence(textView.context, textStyle)
6573
}
6674

75+
open operator fun plus(text: Text): Text {
76+
return TextBuilder.BuilderText(this) + text
77+
}
78+
79+
open operator fun plus(string: String): Text {
80+
return TextBuilder.BuilderText(this) + string
81+
}
82+
83+
operator fun plus(textStyle: TextStyle?): Text {
84+
return textStyle?.let { from(this, textStyle = textStyle) } ?: this
85+
}
86+
6787
override fun equals(other: Any?): Boolean {
6888
if (this === other) return true
6989
if (javaClass != other?.javaClass) return false
@@ -106,6 +126,38 @@ open class Text(protected val defaultTextStyle: TextStyle? ) : Serializable {
106126

107127
}
108128

129+
130+
private class CharSequenceText internal constructor(
131+
private val charSequence: CharSequence,
132+
textStyle: TextStyle?) : Text(textStyle) {
133+
134+
override fun isEmpty(): Boolean = charSequence.isEmpty()
135+
136+
override fun getString(context: Context): String? {
137+
return charSequence.toString()
138+
}
139+
140+
override fun getCharSequence(context: Context, textStyle: TextStyle?): CharSequence? {
141+
return (defaultTextStyle + textStyle)?.applyStyle(context, charSequence) ?: charSequence
142+
}
143+
144+
override fun equals(other: Any?): Boolean {
145+
if (this === other) return true
146+
if (javaClass != other?.javaClass) return false
147+
148+
other as CharSequenceText
149+
150+
return charSequence == other.charSequence
151+
}
152+
153+
override fun hashCode(): Int {
154+
var result = super.hashCode()
155+
result = 31 * result + charSequence.hashCode()
156+
return result
157+
}
158+
159+
}
160+
109161
private class ResourceText internal constructor(
110162
private val stringRes: Int,
111163
textStyle: TextStyle?
@@ -142,6 +194,16 @@ open class Text(protected val defaultTextStyle: TextStyle? ) : Serializable {
142194
override fun isEmpty(): Boolean = stringRes <= 0
143195

144196
override fun getString(context: Context): String {
197+
if (formatArgs.firstOrNull { it is Text } != null) {
198+
val list = formatArgs.map {
199+
when (it) {
200+
is Text -> it.getString(context)
201+
else -> it
202+
}
203+
}
204+
return context.getString(stringRes, *list.toTypedArray())
205+
}
206+
145207
return context.getString(stringRes, *formatArgs)
146208
}
147209

@@ -165,7 +227,7 @@ open class Text(protected val defaultTextStyle: TextStyle? ) : Serializable {
165227
private class ArrayText internal constructor(
166228
private vararg val texts: Text,
167229
textStyle: TextStyle?
168-
): Text(textStyle) {
230+
) : Text(textStyle) {
169231

170232
override fun isEmpty(): Boolean {
171233
if (texts.isEmpty()) return false
@@ -255,20 +317,18 @@ fun Context.toast(text: Text, duration: Int = Toast.LENGTH_SHORT, textStyle: Tex
255317
return Toast.makeText(this, text.getCharSequence(this, textStyle), duration).apply { show() }
256318
}
257319

258-
operator fun Text.plus(text: Text) : Text {
259-
if (this is TextBuilder.BuilderText) {
260-
return this + text
261-
}
262-
return TextBuilder.BuilderText(this) + text
263-
}
320+
fun String.toText(textStyle: TextStyle? = null) = Text.from(this, textStyle)
321+
322+
fun CharSequence.toText(textStyle: TextStyle? = null) = Text.from(this, textStyle)
264323

265-
operator fun Text.plus(string: String) : Text {
266-
if (this is TextBuilder.BuilderText) {
267-
return this + string
324+
operator fun Text?.plus(text: Text?): Text? {
325+
return when {
326+
this == null -> text
327+
text == null -> this
328+
else -> this + text
268329
}
269-
return TextBuilder.BuilderText(this) + string
270330
}
271331

272-
fun String.toText(textStyle: TextStyle? = null) = Text.from(this, textStyle)
273-
274-
operator fun Text.plus(textStyle: TextStyle) = Text.from(this, textStyle = textStyle)
332+
operator fun Text?.plus(textStyle: TextStyle?): Text? {
333+
return this?.let { this + textStyle }
334+
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package com.omega_r.libs.omegatypes
22

33
import android.content.Context
44
import android.content.res.Resources
5+
import java.io.Serializable
56

67
/**
78
* Created by Anton Knyazev on 29.03.2019.
89
*/
910
private const val DEFAULT_CAPACITY = 10
10-
class TextBuilder(capacity: Int) {
11+
class TextBuilder(capacity: Int) : Serializable {
1112

1213
private val list: MutableList<Text> = ArrayList(capacity)
1314

@@ -106,12 +107,12 @@ class TextBuilder(capacity: Int) {
106107
return this
107108
}
108109

109-
operator fun plus(text: Text): Text {
110+
override operator fun plus(text: Text): Text {
110111
textBuilder.append(text)
111112
return this
112113
}
113114

114-
operator fun plus(string: String): Text{
115+
override operator fun plus(string: String): Text {
115116
textBuilder.append(string)
116117
return this
117118
}

0 commit comments

Comments
 (0)