-
Notifications
You must be signed in to change notification settings - Fork 97
/
Extensions.kt
192 lines (169 loc) · 5.27 KB
/
Extensions.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* Extension method to remove the required boilerplate for running code after a view has been
* inflated and measured.
*
* @author Antonio Leiva
* @see <a href="https://antonioleiva.com/kotlin-ongloballayoutlistener/>Kotlin recipes: OnGlobalLayoutListener</a>
*/
inline fun <T : View> T.afterMeasured(crossinline f: T.() -> Unit) {
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
if (measuredWidth > 0 && measuredHeight > 0) {
viewTreeObserver.removeOnGlobalLayoutListener(this)
f()
}
}
})
}
/**
* Extension method to simplify the code needed to apply spans on a specific sub string.
*/
inline fun SpannableStringBuilder.withSpan(vararg spans: Any, action: SpannableStringBuilder.() -> Unit):
SpannableStringBuilder {
val from = length
action()
for (span in spans) {
setSpan(span, from, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
return this
}
/**
* Extension method to provide simpler access to {@link ContextCompat#getColor(int)}.
*/
fun Context.getColorCompat(color: Int) = ContextCompat.getColor(this, color)
/**
* Extension method to provide simpler access to {@link View#getResources()#getString(int)}.
*/
fun View.getString(stringResId: Int): String = resources.getString(stringResId)
/**
* Extension method to provide show keyboard for View.
*/
fun View.showKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
this.requestFocus()
imm.showSoftInput(this, 0)
}
/**
* Extension method to provide hide keyboard for [Activity].
*/
fun Activity.hideSoftKeyboard() {
if (currentFocus != null) {
val inputMethodManager = getSystemService(Context
.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
}
}
/**
* Extension method to provide hide keyboard for [Fragment].
*/
fun Fragment.hideSoftKeyboard() {
activity?.hideSoftKeyboard()
}
/**
* Extension method to provide hide keyboard for [View].
*/
fun View.hideKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, 0)
}
/**
* Extension method to int time to 2 digit String
*/
fun Int.twoDigitTime() = if (this < 10) "0" + toString() else toString()
/**
* Extension method to provide quicker access to the [LayoutInflater] from [Context].
*/
fun Context.getLayoutInflater() = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
/**
* Extension method to provide quicker access to the [LayoutInflater] from a [View].
*/
fun View.getLayoutInflater() = context.getLayoutInflater()
/**
* Extension method to replace all text inside an [Editable] with the specified [newValue].
*/
fun Editable.replaceAll(newValue: String) {
replace(0, length, newValue)
}
/**
* Extension method to replace all text inside an [Editable] with the specified [newValue] while
* ignoring any [android.text.InputFilter] set on the [Editable].
*/
fun Editable.replaceAllIgnoreFilters(newValue: String) {
val currentFilters = filters
filters = emptyArray()
replaceAll(newValue)
filters = currentFilters
}
/**
* Extension method to cast a char with a decimal value to an [Int].
*/
fun Char.decimalValue(): Int {
if (!isDigit())
throw IllegalArgumentException("Out of range")
return this.toInt() - '0'.toInt()
}
/**
* Extension method to simplify view binding.
*/
fun <T : ViewDataBinding> View.bind() = DataBindingUtil.bind<T>(this) as T
/**
* Extension method to simplify view inflating and binding inside a [ViewGroup].
*
* e.g.
* This:
*<code>
* binding = bind(R.layout.widget_card)
*</code>
*
* Will replace this:
*<code>
* binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.widget_card, this, true)
*</code>
*/
fun <T : ViewDataBinding> ViewGroup.bind(layoutId: Int): T {
return DataBindingUtil.inflate(getLayoutInflater(), layoutId, this, true)
}
/**
* Extension method to get Date for String with specified format.
*/
fun String.dateInFormat(format: String): Date? {
val dateFormat = SimpleDateFormat(format, Locale.US)
var parsedDate: Date? = null
try {
parsedDate = dateFormat.parse(this)
} catch (ignored: ParseException) {
ignored.printStackTrace()
}
return parsedDate
}
/**
* Convert a given date to milliseconds
*/
fun Date.toMillis() : Long {
val calendar = Calendar.getInstance()
calendar.time = this
return calendar.timeInMillis
}
/**
* Checks if dates are same
*/
fun Date.isSame(to : Date) : Boolean {
val sdf = SimpleDateFormat("yyyMMdd", Locale.getDefault())
return sdf.format(this) == sdf.format(to)
}
/**
* Extension method to get ClickableSpan.
* e.g.
* val loginLink = getClickableSpan(context.getColorCompat(R.color.colorAccent), { })
*/
fun getClickableSpan(color: Int, action: (view: View) -> Unit): ClickableSpan {
return object : ClickableSpan() {
override fun onClick(view: View) {
action
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.color = color
}
}
}