-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionFunction.kt
More file actions
316 lines (258 loc) · 9.46 KB
/
ExtensionFunction.kt
File metadata and controls
316 lines (258 loc) · 9.46 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.Intent.*
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Matrix
import android.net.Uri
import android.os.Build
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.ImageView
import android.widget.Toast
import androidx.annotation.*
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearSmoothScroller
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import java.io.File
import java.io.FileOutputStream
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.log2
import kotlin.math.pow
object ExtensionFunction {
fun View.show(): View {
if (visibility != View.VISIBLE) {
visibility = View.VISIBLE
}
return this
}
fun View.remove(): View {
if (visibility != View.GONE) {
visibility = View.GONE
}
return this
}
fun View.hide(): View {
if (visibility != View.GONE) {
visibility = View.GONE
}
return this
}
fun View.toggleVisibility(): View {
if (visibility == View.VISIBLE) {
visibility = View.INVISIBLE
} else {
visibility = View.INVISIBLE
}
return this
}
/**
* Extension method to get a view as bitmap.
*/
fun View.getBitmap(): Bitmap {
val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bmp)
draw(canvas)
canvas.save()
return bmp
}
/**
* Extension method to provide simpler access to {@link View#getResources()#getString(int)}.
*/
fun View.getString(stringResId: Int): String = resources.getString(stringResId)
fun View.showKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
this.requestFocus()
imm.showSoftInput(this, 0)
}
fun View.hideKeyboard(): Boolean {
try {
val inputMethodManager =
context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
return inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
} catch (ignored: RuntimeException) {
}
return false
}
// ----------------------------------Context ---------------------------------------------------
fun Context.getColorCompat(color: Int) = ContextCompat.getColor(this, color)
fun Context?.toast(text: CharSequence, duration: Int = Toast.LENGTH_LONG) =
this?.let { Toast.makeText(it, text, duration).show() }
fun Context?.toast(@StringRes textId: Int, duration: Int = Toast.LENGTH_LONG) =
this?.let { Toast.makeText(it, textId, duration).show() }
fun Context.getInteger(@IntegerRes id: Int) = resources.getInteger(id)
fun Context.getBoolean(@BoolRes id: Int) = resources.getBoolean(id)
fun Context.getColor(@ColorRes id: Int) = ContextCompat.getColor(this, id)
fun Context.getDrawable(@DrawableRes id: Int) = ContextCompat.getDrawable(this, id)
fun Context.share(text: String, subject: String = ""): Boolean {
val intent = Intent()
intent.type = "text/plain"
intent.putExtra(EXTRA_SUBJECT, subject)
intent.putExtra(EXTRA_TEXT, text)
try {
startActivity(createChooser(intent, null))
return true
} catch (e: ActivityNotFoundException) {
return false
}
}
fun Context.makeCall(number: String): Boolean {
try {
val intent = Intent(ACTION_CALL, Uri.parse("tel:$number"))
startActivity(intent)
return true
} catch (e: Exception) {
return false
}
}
//---------------------------------- Fragment -------------------------------------------------
fun Fragment?.toast(text: CharSequence, duration: Int = Toast.LENGTH_LONG) =
this?.let { activity.toast(text, duration) }
fun Fragment?.toast(@StringRes textId: Int, duration: Int = Toast.LENGTH_LONG) =
this?.let { activity.toast(textId, duration) }
//------------------------------- Activity ----------------------------------------------------
fun Activity.hideSoftKeyboard() {
if (currentFocus != null) {
val inputMethodManager = getSystemService(
Context
.INPUT_METHOD_SERVICE
) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
}
}
fun String.isPhone(): Boolean {
val p = "^1([34578])\\d{9}\$".toRegex()
return matches(p)
}
fun String.isNumeric(): Boolean {
val p = "^[0-9]+$".toRegex()
return matches(p)
}
fun String.equalsIgnoreCase(other: String) =
this.toLowerCase().contentEquals(other.toLowerCase())
fun Char.decimalValue(): Int {
if (!isDigit())
throw IllegalArgumentException("Out of range")
return this.toInt() - '0'.toInt()
}
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
}
fun Int.twoDigitTime() = if (this < 10) "0" + toString() else toString()
fun String.dateInFormat(format: String): Date? {
val dateFormat = SimpleDateFormat(format, Locale.getDefault())
var parsedDate: Date? = null
try {
parsedDate = dateFormat.parse(this)
} catch (ignored: ParseException) {
ignored.printStackTrace()
}
return parsedDate
}
fun getClickableSpan(color: Int, action: (view: View) -> Unit): ClickableSpan {
return object : ClickableSpan() {
override fun onClick(view: View) {
action(view)
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.color = color
}
}
}
//------------------------------------- Image Related --------------------------------------
fun Bitmap.resize(w: Number, h: Number): Bitmap {
val width = width
val height = height
val scaleWidth = w.toFloat() / width
val scaleHeight = h.toFloat() / height
val matrix = Matrix()
matrix.postScale(scaleWidth, scaleHeight)
if (width > 0 && height > 0) {
return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
}
return this
}
fun Bitmap.saveFile(path: String) {
val f = File(path)
if (!f.exists()) {
f.createNewFile()
}
val stream = FileOutputStream(f)
compress(Bitmap.CompressFormat.PNG, 100, stream)
stream.flush()
stream.close()
}
fun ImageView.loadFromUrl(imageUrl: String) {
Glide.with(this).load(imageUrl).into(this)
}
// ------------------------------------ OS --------------------------------------------
inline fun aboveApi(api: Int, included: Boolean = false, block: () -> Unit) {
if (Build.VERSION.SDK_INT > if (included) api - 1 else api) {
block()
}
}
inline fun belowApi(api: Int, included: Boolean = false, block: () -> Unit) {
if (Build.VERSION.SDK_INT < if (included) api + 1 else api) {
block()
}
}
val Long.formatAsFileSize: String
get() = log2(if (this != 0L) toDouble() else 1.0).toInt().div(10).let {
val precision = when (it) {
0 -> 0; 1 -> 1; else -> 2
}
val prefix = arrayOf("", "K", "M", "G", "T", "P", "E", "Z", "Y")
String.format("%.${precision}f ${prefix[it]}B", toDouble() / 2.0.pow(it * 10.0))
}
fun RecyclerView.smoothSnapToPosition(position: Int, snapMode: Int = LinearSmoothScroller.SNAP_TO_START) {
val smoothScroller = object : LinearSmoothScroller(this.context) {
override fun getVerticalSnapPreference(): Int = snapMode
override fun getHorizontalSnapPreference(): Int = snapMode
}
smoothScroller.targetPosition = position
layoutManager?.startSmoothScroll(smoothScroller)
}
fun View.errorScreen(message: String, parent: View) {
if (message.contentEquals(Constants.NO_INTERNET_CONNECTION)) {
layoutNoInternet.multipleViewShowHideOperation(
parent,
layoutProgressBar
)
} else {
layoutDataIsNotAvailable.showHideForDataIsNotAvailable(
parent,
layoutProgressBar,
layoutNoInternet
)
}
}
fun View.loadingScreen(parent: View) {
multipleViewShowHideOperation(
parent,
layoutNoInternet
)
}
fun View.dataNotAvailable(parent: View) {
showHideForDataIsNotAvailable(parent, layoutProgressBar, layoutNoInternet)
}
}