Skip to content

Instantly share code, notes, and snippets.

@sam43
Created August 17, 2021 18:40
Show Gist options
  • Save sam43/8c3d766f26aa054fca425d3d5d728937 to your computer and use it in GitHub Desktop.
Save sam43/8c3d766f26aa054fca425d3d5d728937 to your computer and use it in GitHub Desktop.
package com.fourshop.customer.utils.extentions
import android.os.Handler
import androidx.annotation.DimenRes
import androidx.annotation.DrawableRes
import androidx.appcompat.widget.AppCompatImageView
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.fourshop.customer.R
import com.fourshop.customer.di.module.GlideApp
import com.fourshop.customer.ui.module.home.pojo.OfferImage
import com.fourshop.customer.utils.Utils
import java.io.File
import java.text.DecimalFormat
import java.text.NumberFormat
import java.util.*
fun AppCompatImageView.loadFile(url: String) {
GlideApp.with(this).load(File(url)).into(this)
}
fun AppCompatImageView.loadFile(url: String, @DrawableRes placeHolder: Int = R.drawable.sign_up_avatar, width: Int, height: Int) {
if (placeHolder == 0)
GlideApp.with(this)
.load(File(url))
.override(Utils.dpToPx(width), Utils.dpToPx(height))
.centerCrop()
.into(this)
else
GlideApp.with(this)
.load(File(url))
.placeholder(placeHolder)
.override(Utils.dpToPx(width), Utils.dpToPx(height))
.centerCrop()
.into(this)
}
fun AppCompatImageView.loadFileRoundedCorner(url: String, radius: Int) {
var requestOptions = RequestOptions()
requestOptions = requestOptions.transforms(CenterCrop(), RoundedCorners(radius))
GlideApp.with(this)
.load(File(url))
.centerCrop()
.apply(requestOptions)
.into(this)
}
/*fun AppCompatImageView.loadUrl(url: String, @DrawableRes placeHolder: Int, width: Int, height: Int) {
GlideApp.with(this)
.load(url)
.override(width, height)
.centerCrop()
.placeholder(drawable)
.into(this)
}*/
fun AppCompatImageView.loadUrl(url: String, @DrawableRes placeHolder: Int = R.drawable.sign_up_avatar, width: Int, height: Int) {
if (placeHolder == 0)
GlideApp.with(this)
.load(url)
.override(Utils.dpToPx(width), Utils.dpToPx(height))
.centerCrop()
.into(this)
else
GlideApp.with(this)
.load(url)
.override(Utils.dpToPx(width), Utils.dpToPx(height))
.centerCrop()
.placeholder(placeHolder)
.into(this)
}
fun AppCompatImageView.loadUrl(url: String, @DrawableRes placeHolder: Int = R.drawable.sign_up_avatar) {
if (placeHolder == 0) {
GlideApp.with(this)
.load(url)
.centerCrop()
.into(this)
} else {
GlideApp.with(this)
.load(url)
.placeholder(placeHolder)
.centerCrop()
.into(this)
}
}
fun AppCompatImageView.loadUrlStore(url: String, @DrawableRes placeHolder: Int = R.drawable.ic_default_image_store) {
if (placeHolder == 0) {
GlideApp.with(this)
.load(url)
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.transition(DrawableTransitionOptions.withCrossFade())
.into(this)
} else {
GlideApp.with(this)
.load(url)
.placeholder(placeHolder)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.transition(DrawableTransitionOptions.withCrossFade())
.centerCrop()
.into(this)
}
}
fun AppCompatImageView.loadUrlWithActualSize(url: String, @DrawableRes placeHolder: Int) {
if (placeHolder == 0) {
GlideApp.with(this)
.load(url)
.centerCrop()
.into(this)
} else {
GlideApp.with(this)
.load(url)
.placeholder(placeHolder)
.centerCrop()
.override(Target.SIZE_ORIGINAL)
.into(this)
}
}
fun AppCompatImageView.loadDrawable(@DrawableRes drawableRes: Int) {
GlideApp.with(this).load(drawableRes).into(this)
}
fun AppCompatImageView.loadDrawable(@DrawableRes drawableRes: Int, width: Int, height: Int) {
GlideApp.with(this)
.load(drawableRes)
.override(Utils.dpToPx(width), Utils.dpToPx(height))
.centerCrop()
.into(this)
}
fun AppCompatImageView.loadUrlCircle(url: String, @DrawableRes placeHolder: Int = R.drawable.sign_up_avatar) {
if (placeHolder == 0)
GlideApp.with(this)
.load(url)
.centerCrop()
.apply(RequestOptions().circleCrop())
.into(this)
else
GlideApp.with(this)
.load(url)
.centerCrop()
.apply(RequestOptions().circleCrop())
.placeholder(placeHolder)
.into(this)
// .override(Utils.dpToPx(width), Utils.dpToPx(height))
}
fun AppCompatImageView.loadFileCircle(url: String, @DrawableRes placeHolder: Int = R.drawable.sign_up_avatar, @DimenRes width: Int = 0, @DimenRes height: Int = 0) {
GlideApp.with(this)
.load(File(url))
.apply(RequestOptions().circleCrop())
.into(this)
// .override(Utils.dpToPx(width), Utils.dpToPx(height))
// .centerCrop()
}
fun AppCompatImageView.loadDrawableCircle(@DrawableRes drawableRes: Int, @DimenRes width: Int = 0, @DimenRes height: Int = 0) {
GlideApp.with(this)
.load(drawableRes)
.apply(RequestOptions.circleCropTransform())
.into(this)
// .override(Utils.dpToPx(width), Utils.dpToPx(height))
}
fun AppCompatImageView.loadDrawableRoundedCorner(@DrawableRes drawableRes: Int) {
var requestOptions = RequestOptions()
requestOptions = requestOptions.transforms(CenterCrop(), RoundedCorners(20))
GlideApp.with(this)
.load(drawableRes)
.apply(requestOptions)
.into(this)
}
fun AppCompatImageView.loadDrawableRoundedCorner(@DrawableRes drawableRes: Int, radius: Int) {
var requestOptions = RequestOptions()
requestOptions = requestOptions.transforms(CenterCrop(), RoundedCorners(radius))
GlideApp.with(this)
.load(drawableRes)
.apply(requestOptions)
.into(this)
}
fun AppCompatImageView.loadUrlRoundedCorner(url: String, @DrawableRes placeHolder: Int = R.drawable.sign_up_avatar, radius: Int) {
var requestOptions = RequestOptions()
requestOptions = requestOptions.transforms(CenterCrop(), RoundedCorners(radius))
GlideApp.with(this)
.load(url)
.placeholder(placeHolder)
.apply(requestOptions)
.into(this)
}
fun String.fourDecimal(): String {
val nf = NumberFormat.getNumberInstance(Locale.US)
val df = nf as DecimalFormat
df.applyPattern("0.0000")
return df.format(this.toDouble()).toString()
}
fun String.twoDecimal(): String {
val nf = NumberFormat.getNumberInstance(Locale.US)
val df = nf as DecimalFormat
df.applyPattern("0.00")
return df.format(this.toDouble()).toString()
}
fun String.oneDecimal(): String {
val nf = NumberFormat.getNumberInstance(Locale.US)
val df = nf as DecimalFormat
df.applyPattern("0.0")
return df.format(this.toDouble()).toString()
}
fun AppCompatImageView.nextImage(currIndex: Int, endIndex: Int, urls: List<OfferImage>, duration: Long) {
try {
this.loadUrlStore(urls[currIndex].imageurl)
var currentIndex = currIndex
currentIndex++
Handler().postDelayed({
if (currentIndex > endIndex) {
currentIndex = 0
nextImage(currentIndex, endIndex, urls, duration)
} else {
nextImage(currentIndex, endIndex, urls, duration)
}
}, duration)
} catch (e: Exception) {
e.printStackTrace()
}
}
/*
fun ViewGroup.inflate(layoutRes: Int): View {
return LayoutInflater.from(context).inflate(layoutRes, this, false)
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment