Skip to content

Instantly share code, notes, and snippets.

@PrashamTrivedi
Created January 10, 2018 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PrashamTrivedi/ec6e95a1c8ca947801cfb3bf89965d3e to your computer and use it in GitHub Desktop.
Save PrashamTrivedi/ec6e95a1c8ca947801cfb3bf89965d3e to your computer and use it in GitHub Desktop.
A rough Idea for using Glide as DSL....
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.support.annotation.DrawableRes
import android.support.v4.content.ContextCompat
import android.widget.ImageView
import com.bumptech.glide.DrawableTypeRequest
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.Transformation
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.GlideDrawable
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.signature.StringSignature
import com.celites.kutils.isEmptyString
import java.lang.Exception
class GlideDsl(val requestManager: RequestManager, val context: Context) {
var url: String = ""
var thumbnailUrl: String = ""
var cacheUrl: Boolean = true
var cacheStrategyUrl: DiskCacheStrategy = DiskCacheStrategy.RESULT
var signature: StringSignature = StringSignature(url)
var cacheThumbnail: Boolean = true
var cacheStrategyThumbnail: DiskCacheStrategy = DiskCacheStrategy.RESULT
var thumbnailSignature: StringSignature = StringSignature(thumbnailUrl)
var asBitmap: Boolean = false
var error: Drawable? = null
fun error(@DrawableRes resource: Int) {
error = ContextCompat.getDrawable(context, resource)
}
var placeHolder: Drawable? = null
fun placeHolder(@DrawableRes resource: Int) {
placeHolder = ContextCompat.getDrawable(context, resource)
}
var transform: Boolean = false
var transformations: Array<Transformation<Bitmap>> = arrayOf()
var otherTransformations: (request: DrawableTypeRequest<String>) -> Unit = {}
var onImageReady: (resource: GlideDrawable?,
model: String?,
target: Target<GlideDrawable>?,
isFromMemoryCache: Boolean,
isFirstResource: Boolean) -> Boolean = { _, _, _, _, _ -> false }
var onImageError: (e: Exception?,
model: String?,
target: com.bumptech.glide.request.target.Target<GlideDrawable>?,
isFirstResource: Boolean) -> Boolean = { _, _, _, _ -> false }
fun into(view: ImageView) {
requestManager.load(url).apply {
if (!thumbnailUrl.isEmptyString()) {
val thumbnailRequest = requestManager.load(thumbnailUrl).apply {
if (cacheThumbnail) {
this.diskCacheStrategy(cacheStrategyThumbnail).signature(thumbnailSignature)
}
}
this.thumbnail(thumbnailRequest)
}
if (cacheUrl) {
this.diskCacheStrategy(cacheStrategyUrl).signature(signature)
}
if (asBitmap) {
this.asBitmap()
}
this.error(error)
this.placeholder(placeHolder)
if (transform) {
this.bitmapTransform(*transformations)
}
otherTransformations(this)
this.listener(object : RequestListener<String, GlideDrawable> {
override fun onResourceReady(resource: GlideDrawable?, model: String?, target: Target<GlideDrawable>?, isFromMemoryCache: Boolean, isFirstResource: Boolean): Boolean {
return onImageReady(resource, model, target, isFromMemoryCache, isFirstResource)
}
override fun onException(e: Exception?, model: String?, target: Target<GlideDrawable>?, isFirstResource: Boolean): Boolean {
return onImageError(e, model, target, isFirstResource)
}
})
}.into(view)
}
}
//Example
binding?.root?.context?.let {
GlideDsl(groupItemViewModel.requestManager, it).apply {
url = viewModel.url //Original Url
thumbnailUrl =viewModel.thumb //Thumbnail Url
error(R.drawable.ic_placeholder_image)
//Other Way
//error = ContextCompat.getDrawable(it,R.drawable.ic_placeholder_image)
placeHolder(R.drawable.avd)
transform = false
}.into(binding.imageView)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment