Skip to content

Commit b94ecc6

Browse files
committed
Add color sources and constants
1 parent 50e0db0 commit b94ecc6

File tree

9 files changed

+102
-15
lines changed

9 files changed

+102
-15
lines changed

glide/src/main/java/com/omega_r/libs/omegatypes/glide/GlideImage.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class GlideImage(override val url: String) : Image(), UrlImage {
3131
Glide.with(imageView)
3232
.load(url)
3333
.apply {
34-
if (placeholderResId != 0) placeholder(placeholderResId)
34+
val newPlaceholderResId = getDefaultPlaceholderResId(imageView.context, placeholderResId)
35+
36+
if (newPlaceholderResId != 0) placeholder(newPlaceholderResId)
3537
into(imageView)
3638
}
3739
}
@@ -42,7 +44,9 @@ class GlideImage(override val url: String) : Image(), UrlImage {
4244
Glide.with(view)
4345
.load(url)
4446
.apply {
45-
if (placeholderResId != 0) placeholder(placeholderResId)
47+
val newPlaceholderResId = getDefaultPlaceholderResId(view.context, placeholderResId)
48+
49+
if (newPlaceholderResId != 0) placeholder(newPlaceholderResId)
4650

4751
into(object : CustomViewTarget<View, Drawable>(view) {
4852
override fun onLoadFailed(errorDrawable: Drawable?) {

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ package com.omega_r.libs.omegatypes
33
import android.content.Context
44
import android.content.res.ColorStateList
55
import android.os.Build
6+
import android.util.TypedValue
67
import java.io.Serializable
78

9+
810
/**
911
* Created by Anton Knyazev on 18.05.2019.
1012
*/
1113

12-
abstract class Color : Serializable {
14+
private typealias GraphicColor = android.graphics.Color
15+
16+
abstract class Color : Serializable {
1317

1418
abstract fun getColorInt(context: Context): Int
1519

@@ -19,11 +23,51 @@ abstract class Color : Serializable {
1923

2024
companion object {
2125

26+
val BLACK
27+
get() = fromInt(GraphicColor.BLACK)
28+
29+
val WHITE
30+
get() = fromInt(GraphicColor.WHITE)
31+
32+
val DKGRAY
33+
get() = fromInt(GraphicColor.DKGRAY)
34+
35+
val GRAY
36+
get() = fromInt(GraphicColor.GRAY)
37+
38+
val GREEN
39+
get() = fromInt(GraphicColor.GREEN)
40+
41+
val BLUE
42+
get() = fromInt(GraphicColor.BLUE)
43+
44+
val YELLOW
45+
get() = fromInt(GraphicColor.YELLOW)
46+
47+
val CYAN
48+
get() = fromInt(GraphicColor.CYAN)
49+
50+
val MAGENTA
51+
get() = fromInt(GraphicColor.MAGENTA)
52+
53+
val TRANSPARENT
54+
get() = fromInt(GraphicColor.TRANSPARENT)
55+
2256
@JvmStatic
2357
fun fromInt(color: Int): Color = IntColor(color)
2458

2559
@JvmStatic
2660
fun fromResource(colorRes: Int): Color = ResourceColor(colorRes)
61+
62+
@JvmStatic
63+
fun fromAttribute(colorAttr: Int): Color = AttrThemeColor(colorAttr)
64+
65+
@JvmStatic
66+
fun fromArgb(alpha: Int, red: Int, green: Int, blue: Int): Color = IntColor(GraphicColor.argb(alpha, red, green, blue))
67+
68+
@JvmStatic
69+
fun fromString(colorString: String): Color = IntColor(GraphicColor.parseColor(colorString))
70+
2771
}
2872

2973
class IntColor(private val colorInt: Int) : Color() {
@@ -47,6 +91,15 @@ abstract class Color : Serializable {
4791
context.resources.getColorStateList(colorRes)
4892
}
4993
}
94+
}
95+
96+
class AttrThemeColor(private val attrInt: Int) : Color() {
97+
98+
override fun getColorInt(context: Context): Int {
99+
return TypedValue().run {
100+
if (context.theme.resolveAttribute(attrInt, this, true)) data else 0
101+
}
102+
}
50103

51104
}
52105

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.graphics.Rect
88
import android.graphics.drawable.BitmapDrawable
99
import android.graphics.drawable.Drawable
1010
import android.util.Base64
11+
import android.util.TypedValue
1112
import android.view.View
1213
import android.widget.ImageView
1314
import com.omega_r.libs.omegatypes.Image.Companion.applyBackground
@@ -17,17 +18,21 @@ open class Image : Serializable {
1718

1819
@JvmOverloads
1920
open fun applyImage(imageView: ImageView, placeholderResId: Int = 0) {
20-
if (placeholderResId == 0) {
21+
val newPlaceholderResId = getDefaultPlaceholderResId(imageView.context, placeholderResId)
22+
23+
if (newPlaceholderResId == 0) {
2124
imageView.setImageDrawable(null)
2225
} else {
23-
imageView.setImageResource(placeholderResId)
26+
imageView.setImageResource(newPlaceholderResId)
2427
}
2528
}
2629

2730
@JvmOverloads
2831
open fun applyBackground(view: View, placeholderResId: Int = 0) {
29-
if (placeholderResId == 0) {
30-
view.setBackgroundResource(placeholderResId)
32+
val newPlaceholderResId = getDefaultPlaceholderResId(view.context, placeholderResId)
33+
34+
if (newPlaceholderResId != 0) {
35+
view.setBackgroundResource(newPlaceholderResId)
3136
} else {
3237
applyBackground(view, null)
3338
}
@@ -51,6 +56,16 @@ open class Image : Serializable {
5156
Image.applyBackground(view, background)
5257
}
5358

59+
protected fun getDefaultPlaceholderResId(context: Context, placeholderResId: Int): Int {
60+
return if (placeholderResId != 0) {
61+
placeholderResId
62+
} else {
63+
TypedValue().run {
64+
if (context.theme.resolveAttribute(R.attr.omegaTypePlaceholderDefault, this, true)) data else 0
65+
}
66+
}
67+
}
68+
5469
companion object {
5570

5671
@JvmStatic

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ abstract class TextStyle : Serializable {
2727
fun color(color: Color): TextStyle = ColorTextStyle(color)
2828

2929
@JvmStatic
30-
fun color(colorInt: Int): TextStyle = ColorTextStyle(Color.fromInt(colorInt))
30+
fun colorFromInt(colorInt: Int): TextStyle = ColorTextStyle(Color.fromInt(colorInt))
31+
32+
@JvmStatic
33+
fun colorFromResource(colorRes: Int): TextStyle = ColorTextStyle(Color.fromResource(colorRes))
3134

3235
@JvmStatic
3336
fun bold(): TextStyle = boldTextStyle

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.text.ParcelableSpan
77
import android.text.TextPaint
88
import android.text.style.MetricAffectingSpan
99
import android.text.style.TypefaceSpan
10+
import com.omega_r.libs.omegatypes.tools.LeakyTypefaceStorageCompat
1011

1112
class TypefaceSpanCompat(
1213
var family: String? = null,

omegatypes/src/main/java/com/omega_r/libs/omegatypes/LeakyTypefaceStorageCompat.kt renamed to omegatypes/src/main/java/com/omega_r/libs/omegatypes/tools/LeakyTypefaceStorageCompat.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.omega_r.libs.omegatypes
1+
package com.omega_r.libs.omegatypes.tools
22

33
import android.graphics.Typeface
44
import android.os.Parcel
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<declare-styleable name="OmegaTypesAttrs">
5+
6+
<attr name="omegaTypePlaceholderDefault" format="reference"/>
7+
8+
</declare-styleable>
9+
10+
11+
</resources>
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
<resources>
2-
<string name="app_name">lib</string>
3-
<string name="hello_world">Hello world %s super</string>
42
</resources>

picasso/src/main/java/com/omega_r/libs/omegatypes/picasso/PicassoImage.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ class PicassoImage(override val url: String) : Image(), UrlImage {
2929
}
3030

3131
override fun applyImage(imageView: ImageView, placeholderResId: Int) {
32+
val newPlaceholderResId = getDefaultPlaceholderResId(imageView.context, placeholderResId)
3233
Picasso.get()
3334
.load(url)
3435
.apply {
35-
if (placeholderResId != 0) placeholder(placeholderResId)
36+
if (newPlaceholderResId != 0) placeholder(newPlaceholderResId)
3637
fit()
3738
if (imageView.scaleType == ImageView.ScaleType.CENTER_INSIDE) {
3839
centerInside()
@@ -44,12 +45,13 @@ class PicassoImage(override val url: String) : Image(), UrlImage {
4445
}
4546

4647
override fun applyBackground(view: View, placeholderResId: Int) {
47-
super.applyBackground(view, placeholderResId)
48+
val newPlaceholderResId = getDefaultPlaceholderResId(view.context, placeholderResId)
49+
super.applyBackground(view, newPlaceholderResId)
4850

4951
val requestCreator = Picasso.get().load(url)
5052

51-
if (placeholderResId != 0) {
52-
requestCreator.placeholder(placeholderResId)
53+
if (newPlaceholderResId != 0) {
54+
requestCreator.placeholder(newPlaceholderResId)
5355
}
5456

5557
if (view.width > 0 && view.height > 0) {

0 commit comments

Comments
 (0)