Created
July 3, 2019 05:03
-
-
Save IhwanID/8065f9d7060b597b155a82f55fe5d489 to your computer and use it in GitHub Desktop.
Binding Adapter Collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.graphics.Color | |
| import android.graphics.drawable.ColorDrawable | |
| import android.graphics.drawable.GradientDrawable | |
| import android.graphics.drawable.ShapeDrawable | |
| import android.os.Build | |
| import android.text.Html | |
| import android.view.View | |
| import android.widget.ImageView | |
| import android.widget.TextView | |
| import androidx.cardview.widget.CardView | |
| import androidx.databinding.BindingAdapter | |
| import com.squareup.picasso.Callback | |
| import com.squareup.picasso.Picasso | |
| import com.<*>.R | |
| object BindingAdapters { | |
| @JvmStatic | |
| @BindingAdapter("isInvis") | |
| fun bindIsInvisible(view: View, isInvis: Boolean) { | |
| view.visibility = if (isInvis) { | |
| View.INVISIBLE | |
| } else { | |
| View.VISIBLE | |
| } | |
| } | |
| @JvmStatic | |
| @BindingAdapter("isGone") | |
| fun bindIsGone(view: View, isGone: Boolean) { | |
| view.visibility = if (isGone) { | |
| View.GONE | |
| } else { | |
| View.VISIBLE | |
| } | |
| } | |
| @JvmStatic | |
| @BindingAdapter("cardBgColor") | |
| fun setBackgroundColor(view: CardView, bg: String) { | |
| val color: Int = try { | |
| Color.parseColor(bg) | |
| } catch (e: Exception) { | |
| try { | |
| Color.parseColor("#$bg") | |
| } catch (e: Exception) { | |
| Color.parseColor("#FFFFFF") | |
| } | |
| } | |
| view.setCardBackgroundColor(color) | |
| } | |
| @JvmStatic | |
| @BindingAdapter("textHexColor") | |
| fun setFontColor(textView: TextView, bg: String) { | |
| val color: Int = try { | |
| Color.parseColor(bg) | |
| } catch (e: Exception) { | |
| Color.parseColor("#$bg") | |
| } | |
| textView.setTextColor(color) | |
| } | |
| @JvmStatic | |
| @BindingAdapter(value = ["imageUrlBg", "imageBgColor"]) | |
| fun setDrawableBgColor(imgView: ImageView, url: String?, bg: String) { | |
| val color: Int = try { | |
| Color.parseColor(bg) | |
| } catch (e: Exception) { | |
| try { | |
| Color.parseColor("#$bg") | |
| } catch (e: Exception) { | |
| Color.parseColor("#FFFFFF") | |
| } | |
| } | |
| if (!url.isNullOrEmpty()) { | |
| Picasso.get() | |
| .load(url) | |
| .placeholder(R.color.colorAthensGray) | |
| .fit() | |
| .centerCrop() | |
| .into(imgView, object : Callback { | |
| override fun onSuccess() { | |
| } | |
| override fun onError(e: java.lang.Exception?) { | |
| imgView.setBackgroundColor(color) | |
| } | |
| }) | |
| } else { | |
| when (val background = imgView.background) { | |
| is ShapeDrawable -> // cast to 'ShapeDrawable' | |
| background.paint.color = color | |
| is GradientDrawable -> // cast to 'GradientDrawable' | |
| background.setColor(color) | |
| is ColorDrawable -> // alpha value may need to be set again after this call | |
| background.color = color | |
| } | |
| } | |
| } | |
| @JvmStatic | |
| @Suppress("DEPRECATION") | |
| @BindingAdapter("renderHtml") | |
| fun renderHtml(textView: TextView, html: String) { | |
| val convertedHtml = html.replace( | |
| "(<(/)img>)|(<img.+?>)|(<(/)video>)|(<video.+?>)|(<(/)embed>)|(<embed.+?>|(<(/)iframe>)|(<iframe.+?>))".toRegex(), | |
| "" | |
| ) | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | |
| textView.text = Html.fromHtml(convertedHtml, Html.FROM_HTML_MODE_COMPACT) | |
| } else { | |
| textView.text = Html.fromHtml(convertedHtml) | |
| } | |
| } | |
| @JvmStatic | |
| @BindingAdapter("imageUrl") | |
| fun imageUrl(imgView: ImageView, url: String?) { | |
| url?.let { | |
| Picasso.get() | |
| .load(url) | |
| .placeholder(R.color.colorAthensGray) | |
| .fit() | |
| .centerCrop() | |
| .into(imgView) | |
| } | |
| } | |
| @JvmStatic | |
| @BindingAdapter(value = ["imageUrlWP", "bgColorWP"]) | |
| fun imageUrlWithPlaceHolder(imgView: ImageView, url: String?, placeholderColor: String?) { | |
| Constants.log("HERE LOAD SOME IMAGE FROM $url with $placeholderColor") | |
| val color = try { | |
| Color.parseColor(placeholderColor) | |
| } catch (e: Exception) { | |
| Color.parseColor("#FFFFFF") | |
| } | |
| if (!url.isNullOrEmpty()) { | |
| Picasso.get() | |
| .load(url) | |
| .placeholder(R.color.colorAthensGray) | |
| .fit() | |
| .centerCrop() | |
| .into(imgView, object : Callback { | |
| override fun onSuccess() { | |
| } | |
| override fun onError(e: java.lang.Exception?) { | |
| imgView.setBackgroundColor(color) | |
| } | |
| }) | |
| } else { | |
| imgView.apply { | |
| setImageDrawable(null) | |
| setBackgroundColor(color) | |
| } | |
| } | |
| } | |
| @JvmStatic | |
| @BindingAdapter("imageUrlCenterInside") | |
| fun imageUrlCenterInside(imgView: ImageView, url: String?) { | |
| url?.let { | |
| Picasso.get() | |
| .load(url) | |
| .placeholder(R.color.colorAthensGray) | |
| .fit() | |
| .centerInside() | |
| .into(imgView) | |
| } | |
| } | |
| @JvmStatic | |
| @BindingAdapter("android:loadImage") | |
| fun loadImage(imgView: ImageView, image: Int) { | |
| Picasso.get() | |
| .load(R.color.white) | |
| .placeholder(image) | |
| .fit() | |
| .centerInside() | |
| .into(imgView) | |
| } | |
| @JvmStatic | |
| @BindingAdapter("android:src") | |
| fun setImageViewResource(imageView: ImageView, resource: Int) { | |
| imageView.setImageResource(resource) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment