Skip to content

Commit 277916c

Browse files
committed
Add new extensions: recyclerview, clipdata, getCompat<*>
Add DensityValue
1 parent 8f104e0 commit 277916c

11 files changed

Lines changed: 213 additions & 21 deletions

File tree

extensionslib/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ android {
2525
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
2626
}
2727
}
28-
28+
kotlinOptions {
29+
freeCompilerArgs = ["-Xinline-classes"]
30+
}
2931
}
3032

3133
dependencies {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.omega_r.libs.extensions.activity
2+
3+
import androidx.annotation.IdRes
4+
import androidx.appcompat.app.ActionBarDrawerToggle
5+
import androidx.appcompat.app.AppCompatActivity
6+
import androidx.appcompat.widget.Toolbar
7+
import androidx.drawerlayout.widget.DrawerLayout
8+
9+
/**
10+
* Created by Anton Knyazev on 16.11.2020.
11+
*/
12+
fun AppCompatActivity.setDrawerToggle(@IdRes drawerLayoutId: Int, @IdRes toolbarId: Int) {
13+
val drawerLayout = findViewById<DrawerLayout>(drawerLayoutId)!!
14+
val toolbar = findViewById<Toolbar>(toolbarId)!!
15+
val drawerToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, 0, 0)
16+
drawerLayout.addDrawerListener(drawerToggle)
17+
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
18+
supportActionBar!!.setHomeButtonEnabled(true)
19+
drawerToggle.syncState()
20+
}

extensionslib/src/main/java/com/omega_r/libs/extensions/context/ResourceExtensions.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,30 @@ import androidx.annotation.DrawableRes
1212
import androidx.annotation.RequiresApi
1313
import androidx.core.content.ContextCompat
1414

15-
fun Context.getCompatDrawable(@DrawableRes id: Int): Drawable? {
15+
@Suppress("NOTHING_TO_INLINE")
16+
inline fun Context.getCompatDrawable(@DrawableRes id: Int): Drawable? {
1617
return ContextCompat.getDrawable(this, id)
1718
}
1819

20+
@Suppress("NOTHING_TO_INLINE")
1921
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
20-
fun Context.getAnimatedVectorDrawable(@DrawableRes res: Int): AnimatedVectorDrawable? {
22+
inline fun Context.getAnimatedVectorDrawable(@DrawableRes res: Int): AnimatedVectorDrawable? {
2123
return getCompatDrawable(res) as? AnimatedVectorDrawable
2224
}
2325

24-
fun Context.getCompatColor(@ColorRes id: Int): Int {
26+
@Suppress("NOTHING_TO_INLINE")
27+
inline fun Context.getCompatColor(@ColorRes id: Int): Int {
2528
return ContextCompat.getColor(this, id)
2629
}
2730

28-
fun Context.getColorByAttribute(@AttrRes attrInt: Int): Int {
31+
@Suppress("NOTHING_TO_INLINE")
32+
inline fun Context.getColorByAttribute(@AttrRes attrInt: Int): Int {
2933
return TypedValue().run {
3034
if (theme.resolveAttribute(attrInt, this, true)) data else 0
3135
}
3236
}
3337

34-
fun Context.getColorDrawableByAttribute(@AttrRes attrInt: Int): ColorDrawable {
38+
@Suppress("NOTHING_TO_INLINE")
39+
inline fun Context.getColorDrawableByAttribute(@AttrRes attrInt: Int): ColorDrawable {
3540
return ColorDrawable(getColorByAttribute(attrInt))
3641
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.omega_r.libs.extensions.intents
2+
3+
import android.content.ClipData
4+
5+
/**
6+
* Created by Anton Knyazev on 16.11.2020.
7+
*/
8+
operator fun ClipData.iterator(): Iterator<ClipData.Item> {
9+
return (0 until itemCount).asSequence().map(::getItemAt).iterator()
10+
}
11+
12+
val ClipData.items: List<ClipData.Item>
13+
get() = (0 until itemCount).map(::getItemAt)

extensionslib/src/main/java/com/omega_r/libs/extensions/metrics/DensityExtensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package com.omega_r.libs.extensions.metrics
22

33
import android.content.res.Resources
44

5-
fun Int.toDp(): Int = (this / Resources.getSystem().displayMetrics.density).toInt()
5+
fun Int.toDp(): Int = toFloat().toDp().toInt()
66

7-
fun Int.toPx(): Int = (this * Resources.getSystem().displayMetrics.density).toInt()
7+
fun Int.toPx(): Int = toFloat().toPx().toInt()
88

99
fun Float.toDp(): Float = (this / Resources.getSystem().displayMetrics.density)
1010

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.omega_r.libs.extensions.metrics
2+
3+
import android.content.res.Resources
4+
5+
/**
6+
* Created by Anton Knyazev on 16.11.2020.
7+
*/
8+
enum class DensityUnit (private val coefToPx: Float){
9+
PX(1f),
10+
DP(Resources.getSystem().displayMetrics.density),
11+
SP(Resources.getSystem().displayMetrics.scaledDensity);
12+
13+
companion object {
14+
15+
fun convertMetricsUnit(value: Float, sourceUnit: DensityUnit, targetUnit: DensityUnit): Float {
16+
return when {
17+
sourceUnit == targetUnit -> value
18+
targetUnit == PX -> value * targetUnit.coefToPx
19+
else -> value * (targetUnit.coefToPx / sourceUnit.coefToPx)
20+
}
21+
}
22+
23+
}
24+
25+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.omega_r.libs.extensions.metrics
2+
3+
import android.util.TypedValue.COMPLEX_UNIT_PX
4+
import android.widget.TextView
5+
import com.omega_r.libs.extensions.metrics.DensityUnit.Companion.convertMetricsUnit
6+
import com.omega_r.libs.extensions.metrics.DensityUnit.DP
7+
import com.omega_r.libs.extensions.metrics.DensityUnit.PX
8+
9+
/**
10+
* Created by Anton Knyazev on 16.11.2020.
11+
*/
12+
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
13+
inline class DensityValue internal constructor(private val value: Float): Comparable<DensityValue> {
14+
15+
companion object {
16+
17+
private val storageUnit
18+
get() = PX
19+
20+
}
21+
22+
val inPx: Float
23+
get() = toFloat(PX)
24+
25+
val inDp: Float
26+
get() = toFloat(DP)
27+
28+
constructor(value: Float, unit: DensityUnit): this(convertMetricsUnit(value, unit, storageUnit))
29+
30+
fun toFloat(unit: DensityUnit) = convertMetricsUnit(value, storageUnit, unit)
31+
32+
fun toInt(unit: DensityUnit) = toFloat(unit).toInt()
33+
34+
operator fun plus(metrics: DensityValue): DensityValue = DensityValue(value + metrics.value)
35+
36+
operator fun minus(metrics: DensityValue): DensityValue = DensityValue(value - metrics.value)
37+
38+
operator fun unaryMinus(): DensityValue = DensityValue(-value)
39+
40+
operator fun times(scale: Int): DensityValue = DensityValue(value * scale)
41+
42+
operator fun times(scale: Double): DensityValue = DensityValue((value * scale).toFloat())
43+
44+
operator fun div(scale: Int): DensityValue = DensityValue(value / scale)
45+
46+
operator fun div(scale: Double): DensityValue = DensityValue((value / scale).toFloat())
47+
48+
operator fun div(other: DensityValue): Float = this.value / other.value
49+
50+
override fun compareTo(other: DensityValue): Int {
51+
return value.compareTo(other.value)
52+
}
53+
54+
}
55+
56+
val Int.px
57+
get() = DensityValue(toFloat(), PX)
58+
59+
val Float.px
60+
get() = DensityValue(this, PX)
61+
62+
val Int.dp
63+
get() = DensityValue(toFloat(), DP)
64+
65+
val Float.dp
66+
get() = DensityValue(this, DP)
67+
68+
fun TextView.setTextSize(densityValue: DensityValue) = setTextSize(COMPLEX_UNIT_PX, densityValue.toFloat(PX))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.omega_r.libs.extensions.view
2+
3+
import android.text.InputFilter
4+
import android.widget.EditText
5+
6+
/**
7+
* Created by Anton Knyazev on 16.11.2020.
8+
*/
9+
var EditText.maxLength: Int
10+
get() = filters.filterIsInstance(InputFilter.LengthFilter::class.java).firstOrNull()?.max ?: 0
11+
set(value) {
12+
val nonLengthFilters = filters.filter { it !is InputFilter.LengthFilter }
13+
filters = (nonLengthFilters + InputFilter.LengthFilter(value)).toTypedArray()
14+
}

extensionslib/src/main/java/com/omega_r/libs/extensions/view/RecyclerViewExtensions.kt

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package com.omega_r.libs.extensions.view
22

3+
import android.graphics.drawable.AnimatedVectorDrawable
4+
import android.graphics.drawable.ColorDrawable
5+
import android.graphics.drawable.Drawable
6+
import android.os.Build
7+
import android.view.View
8+
import androidx.annotation.AttrRes
9+
import androidx.annotation.ColorRes
10+
import androidx.annotation.DrawableRes
11+
import androidx.annotation.RequiresApi
312
import androidx.recyclerview.widget.GridLayoutManager
413
import androidx.recyclerview.widget.LinearLayoutManager
514
import androidx.recyclerview.widget.RecyclerView
@@ -69,4 +78,35 @@ fun RecyclerView.addOnScrollPositionListener(onChanged: ((firstVisiblePos: Int,
6978
}
7079
})
7180

72-
}
81+
}
82+
83+
@Suppress("NOTHING_TO_INLINE")
84+
inline fun RecyclerView.Adapter<*>.getCompatDrawable(@DrawableRes id: Int): Drawable? {
85+
return context.getCompatDrawable(id)
86+
}
87+
88+
@Suppress("NOTHING_TO_INLINE")
89+
inline fun RecyclerView.ViewHolder.getCompatDrawable(@DrawableRes id: Int): Drawable? {
90+
return itemView.getCompatDrawable(id)
91+
}
92+
93+
@Suppress("NOTHING_TO_INLINE")
94+
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
95+
inline fun RecyclerView.ViewHolder.getAnimatedVectorDrawable(@DrawableRes res: Int): AnimatedVectorDrawable? {
96+
return itemView.getAnimatedVectorDrawable(res)
97+
}
98+
99+
@Suppress("NOTHING_TO_INLINE")
100+
inline fun RecyclerView.ViewHolder.getCompatColor(@ColorRes id: Int): Int {
101+
return itemView.getCompatColor(id)
102+
}
103+
104+
@Suppress("NOTHING_TO_INLINE")
105+
inline fun RecyclerView.ViewHolder.getColorByAttribute(@AttrRes attrInt: Int): Int {
106+
return itemView.getColorByAttribute(attrInt)
107+
}
108+
109+
@Suppress("NOTHING_TO_INLINE")
110+
inline fun RecyclerView.ViewHolder.getColorDrawableByAttribute(@AttrRes attrInt: Int): ColorDrawable {
111+
return itemView.getColorDrawableByAttribute(attrInt)
112+
}

extensionslib/src/main/java/com/omega_r/libs/extensions/view/TextViewExtensions.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fun TextView.addLinkCallback(needUnderline: Boolean = true, callback: LinkCallba
146146
}
147147

148148
fun TextView.setTextColorResource(@ColorRes colorRes: Int) {
149-
setTextColor(context.getCompatColor(colorRes))
149+
setTextColor(getCompatColor(colorRes))
150150
}
151151

152152
private fun TextView.getDrawableRelative(index: Int): Drawable? {
@@ -186,47 +186,47 @@ var TextView.drawableLeft: Drawable?
186186
set(value) = setDrawable(0, value)
187187

188188
fun TextView.setDrawableLeft(drawableRes: Int) {
189-
drawableLeft = ContextCompat.getDrawable(context, drawableRes)
189+
drawableLeft = getCompatDrawable(drawableRes)
190190
}
191191

192192
var TextView.drawableStart: Drawable?
193193
get() = getDrawableRelative(0)
194194
set(value) = setDrawableRelative(0, value)
195195

196196
fun TextView.setDrawableStart(drawableRes: Int) {
197-
drawableStart = ContextCompat.getDrawable(context, drawableRes)
197+
drawableStart = getCompatDrawable(drawableRes)
198198
}
199199

200200
var TextView.drawableRight: Drawable?
201201
get() = getDrawable(2)
202202
set(value) = setDrawableRelative(2, value)
203203

204204
fun TextView.setDrawableRight(drawableRes: Int) {
205-
drawableRight = ContextCompat.getDrawable(context, drawableRes)
205+
drawableRight = getCompatDrawable(drawableRes)
206206
}
207207

208208
var TextView.drawableEnd: Drawable?
209209
get() = getDrawableRelative(2)
210210
set(value) = setDrawableRelative(2, value)
211211

212212
fun TextView.setDrawableEnd(drawableRes: Int) {
213-
drawableEnd = ContextCompat.getDrawable(context, drawableRes)
213+
drawableEnd = getCompatDrawable(drawableRes)
214214
}
215215

216216
var TextView.drawableTop: Drawable?
217217
get() = getDrawableRelative(1)
218218
set(value) = setDrawableRelative(1, value)
219219

220220
fun TextView.setDrawableTop(drawableRes: Int) {
221-
drawableTop = ContextCompat.getDrawable(context, drawableRes)
221+
drawableTop = getCompatDrawable(drawableRes)
222222
}
223223

224224
var TextView.drawableBottom: Drawable?
225225
get() = getDrawableRelative(3)
226226
set(value) = setDrawableRelative(3, value)
227227

228228
fun TextView.setDrawableBottom(drawableRes: Int) {
229-
drawableBottom = ContextCompat.getDrawable(context, drawableRes)
229+
drawableBottom = getCompatDrawable(drawableRes)
230230
}
231231

232232
fun TextView.removeCompoundDrawables() {

0 commit comments

Comments
 (0)