-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewExt.kt
126 lines (106 loc) · 4.01 KB
/
ViewExt.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.cube.lib.util
import android.app.Activity
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Build
import android.support.annotation.ColorInt
import android.support.annotation.ColorRes
import android.support.annotation.IdRes
import android.support.annotation.LayoutRes
import android.support.v4.app.Fragment
import android.support.v4.content.ContextCompat
import android.support.v4.widget.CompoundButtonCompat
import android.support.v7.widget.AppCompatCheckBox
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
/**
* Extension functions for View class
*/
fun <T : View> Activity.bind(@IdRes idRes: Int): Lazy<T> {
@Suppress("UNCHECKED_CAST")
return unsafeLazy { findViewById<T>(idRes) as T }
}
fun <T : View> Fragment.bind(@IdRes idRes: Int): Lazy<T> {
@Suppress("UNCHECKED_CAST")
return unsafeLazy { view?.findViewById<T>(idRes) as T }
}
fun <T : View> View.bind(@IdRes idRes: Int): Lazy<T> {
@Suppress("UNCHECKED_CAST")
return unsafeLazy { findViewById<T>(idRes) as T}
}
fun <T : View> RecyclerView.ViewHolder.bind(@IdRes idRes: Int): Lazy<T> {
@Suppress("UNCHECKED_CAST")
return unsafeLazy { itemView.findViewById<T>(idRes) as T }
}
fun <T : View> bind(@IdRes idRes: Int, parent: View): Lazy<T> {
@Suppress("UNCHECKED_CAST")
return unsafeLazy { parent.findViewById<T>(idRes) as T }
}
/**
* Convenience method for inflating a view into another. Will return the inflated view, or the parent view if attach = true
*/
fun <T : View> View.inflate(@LayoutRes layoutRes: Int, attach: Boolean = false): T {
@Suppress("UNCHECKED_CAST")
return LayoutInflater.from(context).inflate(layoutRes, this as ViewGroup, attach) as T
}
private fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer)
/**
* Tints views based on view type
*/
fun <T : View> T.tint(@ColorRes colourRes: Int, alpha: Float = 1.0f): T {
val tintColour = context.getColorCompat(colourRes).alpha(alpha)
when (this) {
is AppCompatCheckBox -> {
val colorStateList = ColorStateList(
arrayOf(intArrayOf(android.R.attr.state_enabled)),
intArrayOf(tintColour)
)
CompoundButtonCompat.setButtonTintList(this, colorStateList)
}
is ProgressBar -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
indeterminateTintList = ColorStateList.valueOf(tintColour)
progressTintList = ColorStateList.valueOf(tintColour)
secondaryProgressTintList = ColorStateList.valueOf(tintColour)
progressBackgroundTintList = ColorStateList.valueOf(tintColour.alpha(alpha * 0.5f))
}
}
is ImageView -> drawable?.tint(tintColour)
is TextView -> compoundDrawables?.tint(tintColour)
else -> {
if (background == null) background = ColorDrawable(tintColour) else background.tint(tintColour)
}
}
return this
}
/**
* Tints ann array of drawables
*/
fun Array<Drawable?>.tint(@ColorInt tintColour: Int) = forEach { drawable -> drawable?.tint(tintColour) }
/**
* Tints a drawable
*/
fun Drawable.tint(@ColorInt tintColour: Int) {
mutate()
colorFilter = PorterDuffColorFilter(tintColour, PorterDuff.Mode.SRC_IN)
}
/**
* Applies an alpha channel to a given colour int
*/
fun Int.alpha(alpha: Float): Int {
val red = Color.red(this)
val green = Color.green(this)
val blue = Color.blue(this)
return Color.argb((alpha * 255.0f).toInt(), red, green, blue)
}
fun Context.getColorCompat(@ColorRes colourRes: Int) = ContextCompat.getColor(this, colourRes)