package com.sjkj.base_lib.label;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.databinding.BindingAdapter;
import androidx.databinding.adapters.TextViewBindingAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;


public class CustomBindingMethod {
    @BindingAdapter(value = {"android:imgUrl", "android:placeholder", "android:error"}, requireAll = false)
    public static void loadImg(ImageView imageView, String imgUrl, Drawable placeholder, Drawable error) {
        Glide.with(imageView)
                .load(imgUrl)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .placeholder(placeholder)
                .error(error)
                .into(imageView);
    }
    @SuppressLint("RestrictedApi")
    @BindingAdapter(value={"android:text"})
    public static void setText(EditText view, String oldText, String text) {
        TextViewBindingAdapter.setText(view, text);
        if (text == null) return;
        if (text.equals(oldText) || oldText == null) {
            view.setSelection(text.length());
        }
    }
}
