package com.ysnows.base.binding

import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.util.Log
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import coil.load
import coil.size.ViewSizeResolver
import com.ysnows.base.R

object ImageBindingAdapter {

    @BindingAdapter(value = ["url", "drawable"], requireAll = false)
    @JvmStatic
    fun setUrlWithDrawable(view: ImageView, url: String?, drawable: Drawable?) {

        if (!url.isNullOrEmpty()) {
            view.load(url){
                placeholder(drawable)
                error(drawable)
            }
            return
        }

        if (drawable != null) {

            view.load(drawable)
        }

    }

    @BindingAdapter("android:src")
    @JvmStatic
    fun setImageDrawable(view: ImageView, drawable: Drawable?) {
        view.setImageDrawable(drawable)
    }

    @BindingAdapter("android:src")
    @JvmStatic
    fun setImageResource(imageView: ImageView, resource: Int) {
        imageView.setImageResource(resource)
    }
}
