Skip to content

Commit 2f0f691

Browse files
Merge pull request #6 from Omega-R/feature/base_extensions
Orientation and Drawable extensions added
2 parents 28c2524 + 554e1a5 commit 2f0f691

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.omega_r.libs.extensions.context
2+
3+
import android.content.Context
4+
import android.provider.Settings
5+
6+
val Context.orientation: Int
7+
get() {
8+
return resources.configuration.orientation
9+
}
10+
11+
val Context.isAutoRotateEnabled: Boolean
12+
get() {
13+
return try {
14+
Settings.System.getInt(contentResolver, Settings.System.ACCELEROMETER_ROTATION, 0) == 1
15+
} catch (exc: SecurityException) {
16+
exc.printStackTrace()
17+
true
18+
}
19+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.omega_r.libs.extensions.drawable
2+
3+
import android.content.Context
4+
import android.content.res.Resources
5+
import android.graphics.Bitmap
6+
import android.graphics.drawable.BitmapDrawable
7+
import android.graphics.drawable.Drawable
8+
import android.graphics.drawable.ScaleDrawable
9+
import android.view.Gravity.NO_GRAVITY
10+
import androidx.annotation.ColorInt
11+
import androidx.annotation.ColorRes
12+
import androidx.annotation.DrawableRes
13+
import androidx.core.content.ContextCompat
14+
import androidx.core.graphics.drawable.DrawableCompat
15+
import com.omega_r.libs.extensions.context.getCompatDrawable
16+
17+
fun Context.getCompatDrawable(@DrawableRes id: Int, @ColorRes color: Int): Drawable? =
18+
getCompatDrawable(id)?.tint(this, color)
19+
20+
fun Drawable?.tint(context: Context, @ColorRes color: Int): Drawable? =
21+
this?.tint(ContextCompat.getColor(context, color))
22+
23+
fun Drawable?.tint(@ColorInt color: Int): Drawable? {
24+
return this?.let {
25+
val mutate = DrawableCompat.wrap(it).mutate()
26+
DrawableCompat.setTint(mutate, color)
27+
return@let mutate
28+
}
29+
}
30+
31+
fun Drawable?.scale(resources: Resources, size: Int) : Drawable? = this?.scale(resources, size, size)
32+
33+
fun Drawable?.scale(resources: Resources, height: Int, width: Int) : Drawable? {
34+
if (this == null) return null
35+
if (this is BitmapDrawable) {
36+
bitmap?.let {
37+
val bitmap: Bitmap? = Bitmap.createScaledBitmap(it, height, width, true)
38+
if (bitmap != null) return BitmapDrawable(resources, bitmap)
39+
}
40+
}
41+
42+
val scaleDrawable = ScaleDrawable(this, NO_GRAVITY, width.toFloat(), height.toFloat()).drawable
43+
scaleDrawable?.let {
44+
it.setBounds(0, 0, width, height)
45+
return it
46+
}
47+
48+
return this
49+
}

extensionslib/src/main/java/com/omega_r/libs/extensions/list/ListExtensions.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ fun <T> List<T>.contains(vararg values: T): Boolean {
88
if (contains(it)) counter++
99
}
1010
return counter == values.size
11-
}
11+
}
12+
13+
fun <T> List<T>.toArrayList() = ArrayList(this)

0 commit comments

Comments
 (0)