Skip to content

Commit 4ec2639

Browse files
Merge pull request #25 from Omega-R/develop
Add new TextStyles, and several small fix
2 parents 0ba7ff1 + 4dedd8e commit 4ec2639

File tree

13 files changed

+350
-23
lines changed

13 files changed

+350
-23
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class MainActivity : BaseActivity() {
2828
textStyle = TextStyle.font(ResourcesCompat.getFont(this, R.font.noto_sans_semi_bold)!!)
2929
)
3030
text.applyTo(exampleTextView) // or exampleTextView.setText(text)
31+
32+
val list = listOf(Text.from("1", TextStyle.color(Color.fromAttribute(R.attr.colorAccent))), Text.from("2", TextStyle.color(Color.fromAttribute(R.attr.colorAccent))), Text.from("3"))
33+
34+
title = list.join(",", postfix = ".").getCharSequence(this)
35+
3136
val image = Image.from("https://avatars1.githubusercontent.com/u/28600571")
3237

3338
thread {

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4+
ext.kotlin_version = '1.3.41'
45
ext.kotlin_version = '1.3.30'
56
ext.supportVersion = '28.0.0'
67
ext.compileSdkVersion = 28

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@ import java.io.InputStream
2121

2222
class GlideImage(override val url: String) : Image(), UrlImage {
2323

24+
override fun preload(context: Context) {
25+
Glide.with(context)
26+
.load(url)
27+
.preload()
28+
}
29+
2430
override fun applyImage(imageView: ImageView, placeholderResId: Int) {
2531
Glide.with(imageView)
2632
.load(url)
2733
.apply {
28-
if (placeholderResId != 0) placeholder(placeholderResId)
34+
val newPlaceholderResId = getDefaultPlaceholderResId(imageView.context, placeholderResId)
35+
36+
if (newPlaceholderResId != 0) placeholder(newPlaceholderResId)
2937
into(imageView)
3038
}
3139
}
@@ -36,7 +44,9 @@ class GlideImage(override val url: String) : Image(), UrlImage {
3644
Glide.with(view)
3745
.load(url)
3846
.apply {
39-
if (placeholderResId != 0) placeholder(placeholderResId)
47+
val newPlaceholderResId = getDefaultPlaceholderResId(view.context, placeholderResId)
48+
49+
if (newPlaceholderResId != 0) placeholder(newPlaceholderResId)
4050

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

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

Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@ package com.omega_r.libs.omegatypes
22

33
import android.content.Context
44
import android.content.res.ColorStateList
5+
import android.content.res.Resources
56
import android.os.Build
7+
import android.util.SparseIntArray
8+
import android.util.TypedValue
9+
import android.widget.TextView
610
import java.io.Serializable
11+
import java.util.*
12+
713

814
/**
915
* Created by Anton Knyazev on 18.05.2019.
1016
*/
1117

12-
abstract class Color : Serializable {
18+
private typealias GraphicColor = android.graphics.Color
19+
20+
abstract class Color : Serializable {
1321

1422
abstract fun getColorInt(context: Context): Int
1523

@@ -19,11 +27,57 @@ abstract class Color : Serializable {
1927

2028
companion object {
2129

30+
val BLACK
31+
get() = fromInt(GraphicColor.BLACK)
32+
33+
val WHITE
34+
get() = fromInt(GraphicColor.WHITE)
35+
36+
val RED
37+
get() = fromInt(GraphicColor.RED)
38+
39+
val DKGRAY
40+
get() = fromInt(GraphicColor.DKGRAY)
41+
42+
val GRAY
43+
get() = fromInt(GraphicColor.GRAY)
44+
45+
val GREEN
46+
get() = fromInt(GraphicColor.GREEN)
47+
48+
val BLUE
49+
get() = fromInt(GraphicColor.BLUE)
50+
51+
val YELLOW
52+
get() = fromInt(GraphicColor.YELLOW)
53+
54+
val CYAN
55+
get() = fromInt(GraphicColor.CYAN)
56+
57+
val MAGENTA
58+
get() = fromInt(GraphicColor.MAGENTA)
59+
60+
val TRANSPARENT
61+
get() = fromInt(GraphicColor.TRANSPARENT)
62+
2263
@JvmStatic
2364
fun fromInt(color: Int): Color = IntColor(color)
2465

2566
@JvmStatic
2667
fun fromResource(colorRes: Int): Color = ResourceColor(colorRes)
68+
69+
@JvmStatic
70+
fun fromAttribute(colorAttr: Int): Color = AttrThemeColor(colorAttr)
71+
72+
@JvmStatic
73+
fun fromArgb(alpha: Int, red: Int, green: Int, blue: Int): Color = IntColor(GraphicColor.argb(alpha, red, green, blue))
74+
75+
@JvmStatic
76+
fun fromString(colorString: String): Color = IntColor(GraphicColor.parseColor(colorString))
77+
78+
@JvmStatic
79+
fun fromColorList(colorStateList: ColorStateList): Color = ColorStateListColor(colorStateList)
80+
2781
}
2882

2983
class IntColor(private val colorInt: Int) : Color() {
@@ -47,7 +101,76 @@ abstract class Color : Serializable {
47101
context.resources.getColorStateList(colorRes)
48102
}
49103
}
104+
}
105+
106+
class AttrThemeColor(private val attrInt: Int) : Color() {
107+
108+
companion object {
109+
110+
private val cache = WeakHashMap<Resources.Theme, SparseIntArray>()
111+
112+
}
113+
114+
override fun getColorInt(context: Context): Int {
115+
116+
val theme = context.theme
117+
118+
val sparseIntArray = cache[theme]
119+
120+
return sparseIntArray?.indexOfKey(attrInt)?.run {
121+
if (this >= 0) {
122+
sparseIntArray.valueAt(this)
123+
} else {
124+
sparseIntArray.extractAndPutCache(theme)
125+
}
126+
} ?: SparseIntArray().run {
127+
cache[theme] = this
128+
extractAndPutCache(theme)
129+
}
130+
}
131+
132+
private fun SparseIntArray.extractAndPutCache(theme: Resources.Theme): Int {
133+
return extractColor(theme).apply {
134+
put(attrInt, this)
135+
}
136+
}
137+
138+
private fun extractColor(theme: Resources.Theme): Int {
139+
return TypedValue().run {
140+
if (theme.resolveAttribute(attrInt, this, true)) data else 0
141+
}
142+
}
143+
144+
}
145+
146+
class ColorStateListColor(private val colorStateList: ColorStateList) : Color() {
147+
148+
override fun getColorInt(context: Context): Int {
149+
return colorStateList.defaultColor
150+
}
151+
152+
override fun getColorStateList(context: Context): ColorStateList {
153+
return colorStateList
154+
}
155+
156+
}
157+
158+
}
159+
160+
var TextView.textColor: Color
161+
get() = Color.fromColorList(textColors)
162+
set(value) {
163+
setTextColor(value.getColorStateList(context))
164+
}
50165

166+
var TextView.hintTextColor: Color
167+
get() = Color.fromColorList(hintTextColors)
168+
set(value) {
169+
setHintTextColor(value.getColorStateList(context))
51170
}
52171

53-
}
172+
var TextView.linkTextColor: Color
173+
get() = Color.fromColorList(linkTextColors)
174+
set(value) {
175+
setLinkTextColor(value.getColorStateList(context))
176+
}

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,38 @@ 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
1415
import java.io.*
1516

1617
open class Image : Serializable {
1718

19+
@JvmOverloads
1820
open fun applyImage(imageView: ImageView, placeholderResId: Int = 0) {
19-
if (placeholderResId == 0) {
21+
val newPlaceholderResId = getDefaultPlaceholderResId(imageView.context, placeholderResId)
22+
23+
if (newPlaceholderResId == 0) {
2024
imageView.setImageDrawable(null)
2125
} else {
22-
imageView.setImageResource(placeholderResId)
26+
imageView.setImageResource(newPlaceholderResId)
2327
}
2428
}
2529

30+
@JvmOverloads
2631
open fun applyBackground(view: View, placeholderResId: Int = 0) {
27-
if (placeholderResId == 0) {
28-
view.setBackgroundResource(placeholderResId)
32+
val newPlaceholderResId = getDefaultPlaceholderResId(view.context, placeholderResId)
33+
34+
if (newPlaceholderResId != 0) {
35+
view.setBackgroundResource(newPlaceholderResId)
2936
} else {
3037
applyBackground(view, null)
3138
}
3239
}
3340

3441

42+
@JvmOverloads
3543
@Throws(IOException::class)
3644
open fun getStream(context: Context,
3745
compressFormat: Bitmap.CompressFormat = Bitmap.CompressFormat.JPEG, quality: Int = 100): InputStream {
@@ -40,10 +48,24 @@ open class Image : Serializable {
4048
}
4149
}
4250

51+
open fun preload(context: Context) {
52+
// nothing
53+
}
54+
4355
protected fun applyBackground(view: View, background: Drawable?) {
4456
Image.applyBackground(view, background)
4557
}
4658

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+
4769
companion object {
4870

4971
@JvmStatic

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

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

33
import android.content.Context
4+
import android.content.res.Resources
45
import android.util.DisplayMetrics
6+
import android.util.SparseArray
7+
import android.util.SparseIntArray
8+
import android.util.TypedValue
59
import android.util.TypedValue.*
10+
import android.widget.TextView
611
import java.io.Serializable
12+
import java.util.*
713

814
/**
915
* Created by Anton Knyazev on 18.05.2019.
@@ -18,6 +24,9 @@ abstract class Size : Serializable {
1824
@JvmStatic
1925
fun from(dimensionRes: Int): Size = DimensionResourceSize(dimensionRes)
2026

27+
@JvmStatic
28+
fun fromAttribute(dimensionAttr: Int): Size = AttrThemeSize(dimensionAttr)
29+
2130
}
2231

2332
abstract fun getSize(context: Context, unit: Unit): Float
@@ -102,11 +111,58 @@ abstract class Size : Serializable {
102111
private class DimensionResourceSize(private val sizeRes: Int) : Size() {
103112

104113
override fun getSize(context: Context, unit: Unit) = context.resources.run {
105-
getDimension(sizeRes).applyFactor(context, Unit.PX, unit)
114+
getRawSize(context).applyFactor(context, Unit.PX, unit)
106115
}
107116

108117
override fun getRawSize(context: Context) = context.resources.getDimension(sizeRes)
109118

110119
}
120+
121+
class AttrThemeSize(private val attrInt: Int) : Size() {
122+
123+
companion object {
124+
125+
private val cache = WeakHashMap<Resources.Theme, SparseArray<Float>>()
126+
127+
}
128+
129+
override fun getSize(context: Context, unit: Unit): Float = context.resources.run {
130+
getRawSize(context).applyFactor(context, Unit.PX, unit)
131+
}
132+
133+
override fun getRawSize(context: Context): Float {
134+
val theme = context.theme
135+
val metrics = context.resources.displayMetrics
136+
val sparseIntArray = cache[theme]
137+
138+
return (sparseIntArray?.indexOfKey(attrInt)?.run {
139+
if (this >= 0) {
140+
sparseIntArray.valueAt(this)
141+
} else {
142+
sparseIntArray.extractAndPutCache(metrics, theme)
143+
}
144+
} ?: SparseArray<Float>().run {
145+
cache[theme] = this
146+
extractAndPutCache(metrics, theme)
147+
})
148+
}
149+
150+
private fun SparseArray<Float>.extractAndPutCache(metrics: DisplayMetrics, theme: Resources.Theme): Float {
151+
return extractSize(metrics, theme).apply {
152+
put(attrInt, this)
153+
}
154+
}
155+
156+
private fun extractSize(metrics: DisplayMetrics, theme: Resources.Theme): Float {
157+
return TypedValue().run {
158+
if (theme.resolveAttribute(attrInt, this, true)) getDimension(metrics) else 0f
159+
}
160+
}
161+
162+
}
163+
111164
}
112165

166+
fun TextView.setTextSize(size: Size) {
167+
setTextSize(COMPLEX_UNIT_PX, size.getSize(context, Size.Unit.PX))
168+
}

0 commit comments

Comments
 (0)