Skip to content

Commit e2a345a

Browse files
committed
Refactoring Color, Size and Text
1 parent 4cc9e2e commit e2a345a

File tree

4 files changed

+79
-14
lines changed

4 files changed

+79
-14
lines changed

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

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,6 @@ abstract class Color : Serializable {
111111

112112
}
113113

114-
private fun extractColor(theme: Resources.Theme): Int {
115-
return TypedValue().run {
116-
if (theme.resolveAttribute(attrInt, this, true)) data else 0
117-
}
118-
}
119-
120114
override fun getColorInt(context: Context): Int {
121115

122116
val theme = context.theme
@@ -127,15 +121,23 @@ abstract class Color : Serializable {
127121
if (this >= 0) {
128122
sparseIntArray.valueAt(this)
129123
} else {
130-
extractColor(theme).apply {
131-
sparseIntArray.put(attrInt, this)
132-
}
124+
sparseIntArray.extractAndPutCache(theme)
133125
}
134126
} ?: SparseIntArray().run {
135127
cache[theme] = this
136-
extractColor(theme).apply {
137-
put(attrInt, this)
138-
}
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
139141
}
140142
}
141143

@@ -165,4 +167,10 @@ var TextView.hintTextColor: Color
165167
get() = Color.fromColorList(hintTextColors)
166168
set(value) {
167169
setHintTextColor(value.getColorStateList(context))
170+
}
171+
172+
var TextView.linkTextColor: Color
173+
get() = Color.fromColorList(linkTextColors)
174+
set(value) {
175+
setLinkTextColor(value.getColorStateList(context))
168176
}

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+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
3535

3636
@JvmStatic
3737
@JvmOverloads
38-
fun from(stringRes: Int, quantity: Int, vararg formatArgs: Any, textStyle: TextStyle? = null): Text = PluralsText(stringRes, quantity, *formatArgs, textStyle = textStyle)
38+
fun fromPlurals(stringRes: Int, quantity: Int, vararg formatArgs: Any, textStyle: TextStyle? = null): Text = PluralsText(stringRes, quantity, *formatArgs, textStyle = textStyle)
3939

4040
@JvmStatic
4141
@JvmOverloads

0 commit comments

Comments
 (0)