Skip to content

support fast data-binding.

Compare
Choose a tag to compare
@LightSun LightSun released this 08 Nov 02:24
· 149 commits to master since this release

New Features

  • 1, data module support nested class now. that means data module can be decorate by ' static'.
  • 2, add member 'generateJsonAdapter' for annotation '@fields'.
  • 3, full support data-binding: annotations, compiler. call. like 'ButterKnife'.
    • 3.1, mock annotation of data-binding like this:
//mock annotation like this:
/**
 * just test generate
 * Created by heaven7 on 2017/11/6 0006.
 */
@BinderFactoryClass(MockBinderFactory.class)
@BinderClass(AndroidBinder.class)
public class MockActivity extends AppCompatActivity {

    @BindsView({"stu_backgroundRes", "stu_visibility", "stu_enable"})
    TextView mTv_name;

    @BindsView({"stu_backgroundRes2", "stu_visibility2"})
    TextView mTv_name2;

    @BindsTextView(value = {"textSizeRes", "textColorRes", "textRes"}, index = 2)
    TextView mTv_3;

    @BindsTextView(value = {"textSizeRes2", "textColorRes2"}, index = 3)
    TextView mTv_4;


    @BindCheckable("checkable")
    CheckBox mcb;

    @BindBackground("v_bg")
    @BindBackgroundColor("v_bgColor")
    @BindBackgroundRes("v_bgRes")
    @BindVisibility("v_visibility")
    @BindEnable("v_enable")
    View mV;

    @BindText("tv_text")
    @BindTextRes("tv_textRes")
    @BindTextSize("tv_textSize")
    @BindTextSizeRes("tv_textSizeRes")
    @BindTextSizePx("tv_textSizePx")
    @BindTextColor("tv_textColor")
    @BindTextColorRes("tv_textColorRes")
    TextView mTv;

    @BindImageUrl("iv_url")
    @BindImageUri("iv_uri")
    @BindImageBitmap("iv_bitmap")
    @BindImageDrawable("iv_drawable")
    @BindImageRes("iv_res")
    ImageView mIv;
}
-3.2,  call demo , like this:
/**
 * test bind image property
 * Created by heaven7 on 2017/11/7.
 */
public class TestImageViewBindActivity extends BaseActivity implements DataBinding.ParameterSupplier {

    @BindView(R.id.iv_url) @BindImageUrl("imageUrl")
    ImageView ivUrl;

    @BindView(R.id.iv_bitmap) @BindImageBitmap("imageBitmap")
    ImageView ivBitmap;

    @BindView(R.id.iv_drawable) @BindImageDrawable("imageDrawable")
    ImageView ivDrawable;

    @BindView(R.id.iv_res) @BindImageRes("imageRes")
    ImageView ivRes;

    private ResHelper mResHelper = new ResHelper();
    private Binder<ImageViewBind> mBinder;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_test_image_view_bind;
    }

    @Override
    protected void onInit(Context context, Bundle savedInstanceState) {
        mResHelper.init(context);

        final ImageViewBind data = DataMediatorFactory.createData(ImageViewBind.class);
        mBinder = DataMediatorFactory.createDataBinding(this)
                .bind(data, this, null);
    }
    @OnClick(R.id.bt_url)
    public void onClickUrl(View v){
        mBinder.getDataProxy().setImageUrl(mResHelper.toggleUrl());
    }
    @OnClick(R.id.bt_bitmap)
    public void onClickBitmap(View v){
        mBinder.getDataProxy().setImageBitmap(mResHelper.toggleBitmap());
    }
    @OnClick(R.id.bt_drawable)
    public void onClickDrawable(View v){
        mBinder.getDataProxy().setImageDrawable(mResHelper.toggleDrawable());
    }
    @OnClick(R.id.bt_res)
    public void onClickRes(View v){
        mBinder.getDataProxy().setImageRes(mResHelper.toggleRes());
    }
    @Override //bind imageUrl need pass extra parameter: 'ViewHelper.IImageLoader'
    public Object[] getParameters(Object data, String property) {
        // why here use 'ViewHelper.IImageLoader' ?
        // because the default AndroidBinder use it to load image.
        // if you want change .please override method 'bindImageUrl' and use @BinderClass/@BinderFactoryClass
        return !property.equals("imageUrl") ? null : new Object[]{ mLoader };
    }

    private final ViewHelper.IImageLoader mLoader = new ViewHelper.IImageLoader() {
        @Override
        public void load(String url, final ImageView iv) {
            Glide.with(iv.getContext())
                    .load(url)
                    .into(new SimpleTarget<Drawable>(200, 200) {
                        @Override
                        public void onResourceReady(Drawable resource,
                                                    Transition<? super Drawable> transition) {
                            iv.setImageDrawable(resource);
                        }
                    });
        }
    };

    public static class ResHelper{

       static final String URL_1 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510078438082&di=a69c86d4cc6ab8c3f83e3d624e41e869&imgtype=0&src=http%3A%2F%2Fimg1.3lian.com%2F2015%2Fa1%2F40%2Fd%2F191.jpg";
       static final String URL_2 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510078568038&di=d80672939c82b81fa52ad0075e793b22&imgtype=0&src=http%3A%2F%2Fimg17.3lian.com%2Fd%2Ffile%2F201701%2F20%2F70ac16a3c3336a3bc2fb28c147bf2049.jpg";

       int res1;
       int res2;

       Bitmap bitmap1;
       Bitmap bitmap2;

       Drawable drawable1;
       Drawable drawable2;

       boolean useUrl1;
       boolean useRes1;
       boolean useBitmap1;
       boolean useDrawable1;

       void init(Context context){
           final Resources res = context.getResources();
           res1 = R.mipmap.ic_launcher;
           res2 = R.mipmap.ic_launcher_round;
           drawable1 = res.getDrawable(res1);
           drawable2 = res.getDrawable(res2);
           bitmap1 = BitmapFactory.decodeResource(res, res1);
           bitmap2 = BitmapFactory.decodeResource(res, res2);
        }
        String toggleUrl(){
            String result = useUrl1 ? URL_2 : URL_1;
            useUrl1 = !useUrl1;
            return result;
        }
        int toggleRes(){
            int result = useRes1 ? res2 : res1;
            useRes1 = !useRes1;
            return result;
        }
        Bitmap toggleBitmap(){
            Bitmap result = useBitmap1 ? bitmap2 : bitmap1;
            useBitmap1 = !useBitmap1;
            return result;
        }
        Drawable toggleDrawable(){
            Drawable result = useDrawable1 ? drawable2 : drawable1;
            useDrawable1 = !useDrawable1;
            return result;
        }
    }
}
  • 4, support self Binder(annotation is @BinderClass) and BinderFactory(annotation is @BinderFactoryClass).

Optimization.

  • 1, open class 'AndroidBinder'.

    for Binder: change bindTextSize with default unit of 'dip'. add method 'bindTextSizePx' with 'pix' unit.
  • 2, make data-module support static .
  • 3, optimise other libs.

Release libs

    //all changed .
   // if you use gradle version < 4.1 please replace 'annotationProcessor ' to 'apt'.
    compile 'com.heaven7.java.data.mediator.annotation:data-mediator-annotations:1.2.0'
    compile 'com.heaven7.java.data.mediator:data-mediator:1.4.0'
    annotationProcessor 'com.heaven7.java.data.mediator.compiler:data-mediator-compiler:1.4.0'
    compile 'com.heaven7.java.data.mediator.support.gson:data-mediator-support-gson:1.0.5'
    compile 'com.heaven7.android.data.mediator:data-mediator-android:1.1.1'

Next Plans

  • Full support data-binding for adapter.
  • property dependency