Skip to content

Instantly share code, notes, and snippets.

Avatar
🙌

Colin White colinrtwhite

🙌
View GitHub Profile
View coil_listener.kt
imageView.load("https://www.example.com/image.jpg") {
listener(
onSuccess = { /** Handle success. */ },
onError = { /** Handle error. */ }
)
}
// Call this inside of your coroutine scope.
// This will throw an error inside of the scope if it fails.
val drawable = Coil.get("https://www.example.com/image.jpg")
View coil.kt
// To load an image into an ImageView, use the load extension function.
imageView.load("https://www.example.com/image.jpg")
// Coil supports urls, uris, resources, drawables, bitmaps, files, and more.
imageView.load(R.drawable.image)
imageView.load(File("/path/to/image.jpg"))
imageView.load("content://com.android.externalstorage/image.jpg")
// Requests can be configured with an optional trailing lambda.
imageView.load("https://www.example.com/image.jpg") {
View Extensions.kt
fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer)
@JvmOverloads @Dimension(unit = Dimension.PX) fun Number.dpToPx(
metrics: DisplayMetrics = Resources.getSystem().displayMetrics
): Float {
return toFloat() * metrics.density
}
@JvmOverloads @Dimension(unit = Dimension.DP) fun Number.pxToDp(
metrics: DisplayMetrics = Resources.getSystem().displayMetrics
View inflate.kt
@Suppress("UNCHECKED_CAST")
@JvmOverloads fun <V : View> ViewGroup.inflate(@LayoutRes layoutRes: Int, attachToRoot: Boolean = false): V {
return LayoutInflater.from(context).inflate(layoutRes, this, attachToRoot) as V
}
View open_web_page.kt
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()
View drawable_tint.kt
@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))
}
View to_activity.kt
fun Context.toActivity(): Activity? {
var context = this
while (context is ContextWrapper) {
if (context is Activity) {
return context
}
context = context.baseContext
}
return null
}
View color_and_drawable.kt
@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)!!
}
View dp_and_px.kt
@JvmOverloads @Dimension(unit = Dimension.PX) fun Number.dpToPx(
metrics: DisplayMetrics = Resources.getSystem().displayMetrics
): Float {
return toFloat() * metrics.density
}
@JvmOverloads @Dimension(unit = Dimension.DP) fun Number.pxToDp(
metrics: DisplayMetrics = Resources.getSystem().displayMetrics
): Float {
return toFloat() / metrics.density
View unsafe_lazy.kt
fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer)