Skip to content

Commit b964230

Browse files
committed
Text.format
Color.withAlpha
1 parent eb67123 commit b964230

2 files changed

Lines changed: 60 additions & 10 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ abstract class Color : Serializable {
2424

2525
open fun getColorStateList(context: Context): ColorStateList = ColorStateList.valueOf(getColorInt(context))
2626

27+
fun withAlpha(alpha: Int) = AlphaColor(alpha, this)
28+
2729
abstract override fun equals(other: Any?): Boolean
2830

2931
abstract override fun hashCode(): Int
@@ -75,13 +77,24 @@ abstract class Color : Serializable {
7577
@JvmStatic
7678
fun fromArgb(alpha: Int, red: Int, green: Int, blue: Int): Color = IntColor(GraphicColor.argb(alpha, red, green, blue))
7779

80+
@JvmStatic
81+
fun fromRgb(red: Int, green: Int, blue: Int): Color = fromArgb(255, red, green, blue)
82+
7883
@JvmStatic
7984
fun fromString(colorString: String): Color = HexStringColor(colorString)
8085

8186
@JvmStatic
8287
fun fromColorList(colorStateList: ColorStateList): Color = ColorStateListColor(colorStateList)
8388
}
8489

90+
data class AlphaColor(private val alpha: Int, private val color: Color) : Color() {
91+
92+
override fun getColorInt(context: Context): Int {
93+
val colorInt = color.getColorInt(context)
94+
return GraphicColor.argb(alpha, GraphicColor.red(colorInt), GraphicColor.green(colorInt), GraphicColor.blue(colorInt))
95+
}
96+
}
97+
8598
class IntColor(private val colorInt: Int) : Color() {
8699

87100
override fun getColorInt(context: Context) = colorInt

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

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
6969

7070
@JvmStatic
7171
fun from(image: Image): Text = ImageText(image)
72+
73+
fun format(stringRes: Int, vararg formatArgs: Serializable, textStyle: TextStyle? = null) =
74+
from(stringRes, formatArgs = formatArgs, textStyle)
75+
76+
fun format(format: Text, vararg formatArgs: Serializable, textStyle: TextStyle? = null): Text =
77+
TextFormatText(format, formatArgs = formatArgs, textStyle)
7278
}
7379

7480
open fun isEmpty(): Boolean = true
@@ -124,7 +130,7 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
124130

125131
private class StringText constructor(
126132
private val string: String?,
127-
textStyle: TextStyle?
133+
textStyle: TextStyle?,
128134
) : Text(textStyle) {
129135

130136
override fun isEmpty(): Boolean = string.isNullOrEmpty()
@@ -155,7 +161,7 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
155161

156162
private class CharSequenceText constructor(
157163
private var charSequence: CharSequence,
158-
textStyle: TextStyle?
164+
textStyle: TextStyle?,
159165
) : Text(textStyle) {
160166

161167
companion object {
@@ -224,7 +230,7 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
224230

225231
private class ResourceText constructor(
226232
private val stringRes: Int,
227-
textStyle: TextStyle?
233+
textStyle: TextStyle?,
228234
) : Text(textStyle) {
229235

230236
override fun isEmpty(): Boolean = stringRes <= 0
@@ -261,7 +267,7 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
261267

262268
private abstract class FormatText(
263269
protected vararg val formatArgs: Serializable,
264-
defaultTextStyle: TextStyle?
270+
defaultTextStyle: TextStyle?,
265271
) : Text(defaultTextStyle) {
266272

267273
protected abstract fun getText(context: Context): CharSequence
@@ -283,7 +289,6 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
283289
return String.format(context.getLocale(), string, *formatArgs)
284290
}
285291

286-
287292
override fun getCharSequence(context: Context, textStyle: TextStyle?): CharSequence {
288293
val text = getText(context)
289294
return if (text is Spanned || formatArgs.firstOrNull { it is Text || it is Image } != null) {
@@ -317,12 +322,11 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
317322
private class ResourceFormatText constructor(
318323
private val stringRes: Int,
319324
vararg formatArgs: Serializable,
320-
textStyle: TextStyle?
325+
textStyle: TextStyle?,
321326
) : FormatText(formatArgs = *formatArgs, defaultTextStyle = textStyle) {
322327

323328
override fun isEmpty(): Boolean = stringRes <= 0
324329

325-
326330
override fun getText(context: Context): CharSequence = context.getText(stringRes)
327331

328332
override fun toString(): String {
@@ -349,7 +353,7 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
349353
private val res: Int,
350354
private val quantity: Int,
351355
vararg formatArgs: Serializable,
352-
textStyle: TextStyle? = null
356+
textStyle: TextStyle? = null,
353357
) : FormatText(formatArgs = *formatArgs, defaultTextStyle = textStyle) {
354358

355359
override fun isEmpty(): Boolean = res <= 0
@@ -383,9 +387,42 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
383387
}
384388
}
385389

390+
private class TextFormatText(
391+
private val text: Text,
392+
vararg formatArgs: Serializable,
393+
textStyle: TextStyle? = null,
394+
) : FormatText(formatArgs = formatArgs, defaultTextStyle = textStyle) {
395+
396+
override fun isEmpty(): Boolean {
397+
return text.isEmpty()
398+
}
399+
400+
override fun getText(context: Context): CharSequence {
401+
return text.getCharSequence(context) ?: ""
402+
}
403+
404+
override fun equals(other: Any?): Boolean {
405+
if (this === other) return true
406+
if (other !is TextFormatText) return false
407+
if (!super.equals(other)) return false
408+
409+
if (text != other.text) return false
410+
if (!formatArgs.contentEquals(other.formatArgs)) return false
411+
412+
return true
413+
}
414+
415+
override fun hashCode(): Int {
416+
var result = super.hashCode()
417+
result = 31 * result + text.hashCode()
418+
result = 31 * result + formatArgs.contentHashCode()
419+
return result
420+
}
421+
}
422+
386423
private class ArrayText constructor(
387424
private vararg val texts: Text,
388-
textStyle: TextStyle?
425+
textStyle: TextStyle?,
389426
) : Text(textStyle) {
390427

391428
override fun isEmpty(): Boolean {
@@ -512,7 +549,7 @@ fun List<Textable>.join(
512549
prefix: String = "",
513550
postfix: String = "",
514551
limit: Int = -1,
515-
truncated: String = "..."
552+
truncated: String = "...",
516553
): Text {
517554
var buffer = if (prefix.isNotEmpty()) Text.from(prefix) else Text.from()
518555

0 commit comments

Comments
 (0)