• 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.VibrationEffect
11 import android.os.Vibrator
12 import android.util.Log
13 import android.view.LayoutInflater
14 import android.view.View
15 import android.view.ViewGroup
16 import android.view.inputmethod.InputMethodManager
17 import android.widget.ImageView
18 import android.widget.TextView
19 import androidx.annotation.ColorInt
20 import androidx.annotation.DrawableRes
21 import androidx.annotation.LayoutRes
22 import androidx.annotation.StringRes
23 import androidx.core.content.ContextCompat
24 import androidx.core.content.getSystemService
25 import androidx.core.graphics.toColorInt
26 import androidx.fragment.app.Fragment
27 import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
28 import com.airbnb.lottie.L
29 import com.bumptech.glide.Glide
30 import com.google.android.material.snackbar.Snackbar
31 
Fragmentnull32 fun Fragment.startActivity(cls: Class<*>) {
33     startActivity(Intent(context, cls))
34 }
35 
urlIntentnull36 fun String.urlIntent(): Intent =
37     Intent(Intent.ACTION_VIEW).setData(Uri.parse(this))
38 
39 fun ViewGroup.inflate(@LayoutRes layout: Int, attachToRoot: Boolean = true): View =
40     LayoutInflater.from(context).inflate(layout, this, attachToRoot)
41 
42 fun String.hasPermission(context: Context): Boolean =
43     ContextCompat.checkSelfPermission(context, this) == PackageManager.PERMISSION_GRANTED
44 
45 fun TextView.setDrawableLeft(@DrawableRes drawableRes: Int, activity: Activity) {
46     val drawable = VectorDrawableCompat.create(resources, drawableRes, activity.theme)
47     setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
48 }
49 
showSnackbarLongnull50 fun View.showSnackbarLong(@StringRes message: Int) =
51     showSnackbarLong(resources.getString(message))
52 
53 fun View.showSnackbarLong(message: String) =
54     Snackbar.make(this, message, Snackbar.LENGTH_LONG).show()
55 
56 fun View.setVisibleIf(condition: Boolean) {
57     visibility = if (condition) View.VISIBLE else View.GONE
58 }
59 
setImageUrlnull60 fun ImageView.setImageUrl(url: String?) = url?.let { Glide.with(this).load(it).into(this) }
61 
flattennull62 inline fun <reified T> flatten(vararg lists: List<T>?) = lists.flatMap { it ?: emptyList() }
63 
Floatnull64 fun Float.lerp(other: Float, amount: Float): Float = this + amount * (other - this)
65 
66 fun Float.sqrt() = kotlin.math.sqrt(this.toDouble()).toFloat()
67 
68 fun View.getText(@StringRes res: Int) = this.resources.getText(res)
69 operator fun Boolean.inc() = !this
70 
71 fun Context.hasPermission(permission: String): Boolean {
72     return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
73 }
74 
vibrateCompatnull75 fun Vibrator.vibrateCompat(millis: Long) {
76     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
77         vibrate(VibrationEffect.createOneShot(millis, VibrationEffect.DEFAULT_AMPLITUDE))
78     } else {
79         @Suppress("DEPRECATION")
80         vibrate(millis)
81     }
82 }
83 
84 @ColorInt
Stringnull85 fun String?.toColorIntSafe(): Int {
86     var bgColor = this ?: "#ffffff"
87     bgColor = if (bgColor.startsWith("#")) bgColor else "#$bgColor"
88 
89     return try {
90         when (bgColor.length) {
91             0 -> "#ffffff"
92             4 -> "#%c%c%c%c%c%c".format(
93                 bgColor[1], bgColor[1],
94                 bgColor[2], bgColor[2],
95                 bgColor[3], bgColor[3]
96             )
97             5 -> "#%c%c%c%c%c%c%c%c".format(
98                 bgColor[1], bgColor[1],
99                 bgColor[2], bgColor[2],
100                 bgColor[3], bgColor[3],
101                 bgColor[4], bgColor[4]
102             )
103             else -> bgColor
104         }.toColorInt()
105     } catch (e: IllegalArgumentException) {
106         Log.w(L.TAG, "Unable to parse $bgColor.")
107         Color.WHITE
108     }
109 }
110 
Contextnull111 fun Context.hideKeyboard() {
112     val inputMethodManager = getSystemService<InputMethodManager>()!!
113     inputMethodManager.hideSoftInputFromWindow((this as Activity).currentFocus?.windowToken, 0)
114 }