• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.airbnb.lottie.samples
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.net.Uri
8 import android.os.Build
9 import android.os.VibrationEffect
10 import android.os.Vibrator
11 import androidx.annotation.DrawableRes
12 import androidx.annotation.LayoutRes
13 import androidx.annotation.StringRes
14 import com.google.android.material.snackbar.Snackbar
15 import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
16 import androidx.fragment.app.Fragment
17 import androidx.core.content.ContextCompat
18 import android.view.LayoutInflater
19 import android.view.View
20 import android.view.ViewGroup
21 import android.widget.ImageView
22 import android.widget.TextView
23 import com.bumptech.glide.Glide
24 
Fragmentnull25 fun Fragment.startActivity(cls: Class<*>) {
26     startActivity(Intent(context, cls))
27 }
28 
urlIntentnull29 fun String.urlIntent(): Intent =
30         Intent(Intent.ACTION_VIEW).setData(Uri.parse(this))
31 
32 fun ViewGroup.inflate(@LayoutRes layout: Int, attachToRoot: Boolean = true): View =
33         LayoutInflater.from(context).inflate(layout, this, attachToRoot)
34 
35 fun String.hasPermission(context: Context): Boolean =
36         ContextCompat.checkSelfPermission(context, this) == PackageManager.PERMISSION_GRANTED
37 
38 fun TextView.setDrawableLeft(@DrawableRes drawableRes: Int, activity: Activity) {
39     val drawable = VectorDrawableCompat.create(resources, drawableRes, activity.theme)
40     setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
41 }
42 
showSnackbarLongnull43 fun View.showSnackbarLong(@StringRes message: Int) =
44         showSnackbarLong(resources.getString(message))
45 
46 fun View.showSnackbarLong(message: String) =
47     Snackbar.make(this, message, Snackbar.LENGTH_LONG).show()
48 
49 fun View.setVisibleIf(condition: Boolean) {
50     visibility = if (condition) View.VISIBLE else View.GONE
51 }
52 
setImageUrlnull53 fun ImageView.setImageUrl(url: String?) = url?.let { Glide.with(this).load(it).into(this) }
54 
flattennull55 inline fun <reified T> flatten(vararg lists: List<T>?) = lists.flatMap { it ?: emptyList() }
56 
Floatnull57 fun Float.lerp(other: Float, amount: Float): Float = this + amount * (other - this)
58 
59 fun Float.sqrt() = Math.sqrt(this.toDouble()).toFloat()
60 
61 fun View.getText(@StringRes res: Int) = this.resources.getText(res)
62 operator fun Boolean.inc() = !this
63 
64 fun Context.hasPermission(permission: String): Boolean {
65     return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
66 }
67 
vibrateCompatnull68 fun Vibrator.vibrateCompat(millis: Long) {
69     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
70         vibrate(VibrationEffect.createOneShot(millis, VibrationEffect.DEFAULT_AMPLITUDE))
71     } else {
72         @Suppress("DEPRECATION")
73         vibrate(millis)
74     }
75 }