• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.airbnb.lottie.samples.utils
2 
3 import android.app.Activity
4 import android.content.Context
5 import android.content.Intent
6 import android.content.pm.PackageManager
7 import android.graphics.Color
8 import android.net.Uri
9 import android.os.Build
10 import android.os.Bundle
11 import android.os.Parcelable
12 import android.os.VibrationEffect
13 import android.os.Vibrator
14 import android.util.Log
15 import android.view.LayoutInflater
16 import android.view.View
17 import android.view.ViewGroup
18 import android.view.inputmethod.InputMethodManager
19 import android.widget.ImageView
20 import android.widget.TextView
21 import androidx.annotation.ColorInt
22 import androidx.annotation.DrawableRes
23 import androidx.annotation.LayoutRes
24 import androidx.annotation.StringRes
25 import androidx.core.content.ContextCompat
26 import androidx.core.content.getSystemService
27 import androidx.core.graphics.toColorInt
28 import androidx.fragment.app.Fragment
29 import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
30 import com.airbnb.lottie.L
31 import com.bumptech.glide.Glide
32 import com.google.android.material.snackbar.Snackbar
33 
Fragmentnull34 fun Fragment.startActivity(cls: Class<*>) {
35     startActivity(Intent(context, cls))
36 }
37 
urlIntentnull38 fun String.urlIntent(): Intent =
39     Intent(Intent.ACTION_VIEW).setData(Uri.parse(this))
40 
41 fun ViewGroup.inflate(@LayoutRes layout: Int, attachToRoot: Boolean = true): View =
42     LayoutInflater.from(context).inflate(layout, this, attachToRoot)
43 
44 fun String.hasPermission(context: Context): Boolean =
45     ContextCompat.checkSelfPermission(context, this) == PackageManager.PERMISSION_GRANTED
46 
47 fun TextView.setDrawableLeft(@DrawableRes drawableRes: Int, activity: Activity) {
48     val drawable = VectorDrawableCompat.create(resources, drawableRes, activity.theme)
49     setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
50 }
51 
showSnackbarLongnull52 fun View.showSnackbarLong(@StringRes message: Int) =
53     showSnackbarLong(resources.getString(message))
54 
55 fun View.showSnackbarLong(message: String) =
56     Snackbar.make(this, message, Snackbar.LENGTH_LONG).show()
57 
58 fun View.setVisibleIf(condition: Boolean) {
59     visibility = if (condition) View.VISIBLE else View.GONE
60 }
61 
setImageUrlnull62 fun ImageView.setImageUrl(url: String?) = url?.let { Glide.with(this).load(it).into(this) }
63 
flattennull64 inline fun <reified T> flatten(vararg lists: List<T>?) = lists.flatMap { it ?: emptyList() }
65 
Floatnull66 fun Float.lerp(other: Float, amount: Float): Float = this + amount * (other - this)
67 
68 fun Float.sqrt() = kotlin.math.sqrt(this.toDouble()).toFloat()
69 
70 fun View.getText(@StringRes res: Int) = this.resources.getText(res)
71 operator fun Boolean.inc() = !this
72 
73 fun Context.hasPermission(permission: String): Boolean {
74     return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
75 }
76 
vibrateCompatnull77 fun Vibrator.vibrateCompat(millis: Long) {
78     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
79         vibrate(VibrationEffect.createOneShot(millis, VibrationEffect.DEFAULT_AMPLITUDE))
80     } else {
81         @Suppress("DEPRECATION")
82         vibrate(millis)
83     }
84 }
85 
86 @ColorInt
Stringnull87 fun String?.toColorIntSafe(): Int {
88     var bgColor = this ?: "#ffffff"
89     bgColor = if (bgColor.startsWith("#")) bgColor else "#$bgColor"
90 
91     return try {
92         when (bgColor.length) {
93             0 -> "#ffffff"
94             4 -> "#%c%c%c%c%c%c".format(
95                 bgColor[1], bgColor[1],
96                 bgColor[2], bgColor[2],
97                 bgColor[3], bgColor[3]
98             )
99 
100             5 -> "#%c%c%c%c%c%c%c%c".format(
101                 bgColor[1], bgColor[1],
102                 bgColor[2], bgColor[2],
103                 bgColor[3], bgColor[3],
104                 bgColor[4], bgColor[4]
105             )
106 
107             else -> bgColor
108         }.toColorInt()
109     } catch (e: IllegalArgumentException) {
110         Log.w(L.TAG, "Unable to parse $bgColor.")
111         Color.WHITE
112     }
113 }
114 
Contextnull115 fun Context.hideKeyboard() {
116     val inputMethodManager = getSystemService<InputMethodManager>()!!
117     inputMethodManager.hideSoftInputFromWindow((this as Activity).currentFocus?.windowToken, 0)
118 }
119 
getParcelableExtraCompatnull120 fun <T : Parcelable> Intent.getParcelableExtraCompat(key: String, klass: Class<T>): T? {
121     return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
122         getParcelableExtra(key, klass)
123     } else {
124         @Suppress("DEPRECATION")
125         getParcelableExtra(key)
126     }
127 }
128 
getParcelableCompatnull129 fun <T : Parcelable> Bundle.getParcelableCompat(key: String, klass: Class<T>): T? {
130     return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
131         getParcelable(key, klass)
132     } else {
133         @Suppress("DEPRECATION")
134         getParcelable(key)
135     }
136 }