Skip to content

Instantly share code, notes, and snippets.

View colinrtwhite's full-sized avatar
🙌
When Github stories?

Colin White colinrtwhite

🙌
When Github stories?
View GitHub Profile
@colinrtwhite
colinrtwhite / complex_forwarding_painter.kt
Last active November 13, 2023 13:41
A painter that wraps another painter to overwrite its color filter, alpha, and/or onDraw.
/**
* Create and return a new [Painter] that wraps [painter] with its [alpha], [colorFilter], or [onDraw] overwritten.
*/
fun forwardingPainter(
painter: Painter,
alpha: Float = DefaultAlpha,
colorFilter: ColorFilter? = null,
onDraw: DrawScope.(ForwardingDrawInfo) -> Unit = DefaultOnDraw,
): Painter = ForwardingPainter(painter, alpha, colorFilter, onDraw)
@colinrtwhite
colinrtwhite / forwarding_painter.kt
Created March 8, 2022 01:11
A painter that wraps another painter to overwrite its color filter and/or alpha.
open class ForwardingPainter(
private val delegate: Painter,
private var alpha: Float = DefaultAlpha,
private var colorFilter: ColorFilter? = null,
) : Painter() {
override val intrinsicSize get() = delegate.intrinsicSize
override fun applyAlpha(alpha: Float): Boolean {
if (alpha == DefaultAlpha) {
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")
// 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") {
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
@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
}
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()
@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
}
@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)!!
}