Skip to content

Instantly share code, notes, and snippets.

@colinrtwhite
Last active April 9, 2020 05:14

Revisions

  1. colinrtwhite revised this gist Sep 23, 2018. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions Extensions.kt
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,15 @@
    fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer)

    @JvmOverloads @Dimension(unit = Dimension.DP) fun Number.toDp(
    @JvmOverloads @Dimension(unit = Dimension.PX) fun Number.dpToPx(
    metrics: DisplayMetrics = Resources.getSystem().displayMetrics
    ): Float {
    return toFloat() / metrics.density
    return toFloat() * metrics.density
    }

    @JvmOverloads @Dimension(unit = Dimension.PX) fun Number.toPx(
    @JvmOverloads @Dimension(unit = Dimension.DP) fun Number.pxToDp(
    metrics: DisplayMetrics = Resources.getSystem().displayMetrics
    ): Float {
    return toFloat() * metrics.density
    return toFloat() / metrics.density
    }

    @ColorInt fun Context.getColorCompat(@ColorRes colorRes: Int): Int {
  2. colinrtwhite created this gist Sep 14, 2018.
    73 changes: 73 additions & 0 deletions Extensions.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer)

    @JvmOverloads @Dimension(unit = Dimension.DP) fun Number.toDp(
    metrics: DisplayMetrics = Resources.getSystem().displayMetrics
    ): Float {
    return toFloat() / metrics.density
    }

    @JvmOverloads @Dimension(unit = Dimension.PX) fun Number.toPx(
    metrics: DisplayMetrics = Resources.getSystem().displayMetrics
    ): Float {
    return toFloat() * metrics.density
    }

    @ColorInt fun Context.getColorCompat(@ColorRes colorRes: Int): Int {
    return ContextCompat.getColor(this, colorRes)
    }

    fun Context.getDrawableCompat(@DrawableRes drawableRes: Int): Drawable {
    return AppCompatResources.getDrawable(this, drawableRes)!!
    }

    @CheckResult fun Drawable.tint(@ColorInt color: Int): Drawable {
    val tintedDrawable = DrawableCompat.wrap(this).mutate()
    DrawableCompat.setTint(tintedDrawable, color)
    return tintedDrawable
    }

    @CheckResult fun Drawable.tint(context: Context, @ColorRes color: Int): Drawable {
    return tint(context.getColorCompat(color))
    }

    fun Context.toActivity(): Activity? {
    var context = this
    while (context is ContextWrapper) {
    if (context is Activity) {
    return context
    }
    context = context.baseContext
    }
    return null
    }

    fun Context.openWebPage(url: String): Boolean {
    // Format the URI properly.
    val uri = url.toWebUri()

    // Try using Chrome Custom Tabs.
    try {
    val intent = CustomTabsIntent.Builder()
    .setToolbarColor(getColorCompat(R.color.primary))
    .setShowTitle(true)
    .build()
    intent.launchUrl(this, uri)
    return true
    } catch (ignored: Exception) {}

    // Fall back to launching a default web browser intent.
    try {
    val intent = Intent(Intent.ACTION_VIEW, uri)
    if (intent.resolveActivity(packageManager) != null) {
    startActivity(intent)
    return true
    }
    } catch (ignored: Exception) {}

    // We were unable to show the web page.
    return false
    }

    fun String.toWebUri(): Uri {
    return (if (startsWith("http://") || startsWith("https://")) this else "https://$this").toUri()
    }