Data-mediator入门系列2

455 阅读2分钟

学习路径

简单demo

  • 1, 假设我要定义关于学生的数据模型, 需要实现Serializable, Parcelable. 假如学生有。年龄,名称, id属性。 那么简单的数据定义为:
@Fields({
        @Field(propName = "age" , type = int.class),
        @Field(propName = "name" , type = String.class),
        @Field(propName = "id" , type = long.class),
})
public interface Student extends Serializable, Parcelable{
}
  • 2, 使用idea插件生成代码, 快捷键比如 alt + insert. (安装见这里Data-mediator入门系列1) 模型生成后类似这样
@Fields({
      @Field(propName = "age" , type = int.class),
      @Field(propName = "name" , type = String.class),
      @Field(propName = "id" , type = long.class),
})
public interface Student extends Serializable, Parcelable, DataPools.Poolable {
    Property PROP_age = SharedProperties.get(int.class.getName(), "age", 0);
    Property PROP_name = SharedProperties.get(String.class.getName(), "name", 0);
    Property PROP_id = SharedProperties.get(long.class.getName(), "id", 0);

    Student setAge(int age1);

    int getAge();

    Student setName(String name1);

    String getName();

    Student setId(long id1);

    long getId();
}
  • 3, 调用示例。
     //属性改变demo. 下面用butterknife快速写demo
      public class TestPropertyChangeActivity extends BaseActivity {
    
        @BindView(R.id.tv_desc)
        TextView mTv_desc;
    
        @BindView(R.id.bt_set_text_on_TextView)
        Button mBt_changeProperty;
        @BindView(R.id.bt_set_text_on_mediator)
        Button mBt_temp;
    
        DataMediator<Student> mMediator;
        @Override
        protected int getLayoutId() {
            return R.layout.ac_test_double_bind;
        }
    
        @Override
        protected void onInit(Context context, Bundle savedInstanceState) {
            mBt_changeProperty.setText("click this to change property");
            mBt_temp.setVisibility(View.GONE);
    
            //为数据模型创建  中介者。
            mMediator = DataMediatorFactory.createDataMediator(Student.class);
            //添加属性callback
            mMediator.addDataMediatorCallback(new DataMediatorCallback<Student>() {
                @Override
                public void onPropertyValueChanged(Student data, Property prop, Object oldValue, Object newValue) {
                    Logger.w("TestPropertyChangeActivity","onPropertyValueChanged","prop = "
                            + prop.getName() + " ,oldValue = " + oldValue + " ,newValue = " + newValue);
                    mTv_desc.setText(String.valueOf(newValue));
                }
            });
            mMediator.getDataProxy().setName("heaven7");
        }
    
        @OnClick(R.id.bt_set_text_on_TextView)
        public void onClickSetTextOnTextView(View v){
            mMediator.getDataProxy().setName("time: " + System.currentTimeMillis());
        }
     }

该demo 做了3件事情。创建数据中介者,添加属性回调。然后在点击事件中改变了数据的属性。 结果使得日志被打印出来。而且textView也被更新。

对模型实现的代码感兴趣?

模型实现代码大致是这样的。

public class Student_$Impl implements Student, Serializable, Parcelable, DataPools.Poolable {
  private static final long serialVersionUID =  1L;

  public static final Parcelable.Creator<Student_$Impl> CREATOR = new Parcelable.Creator<Student_$Impl>() {
    @Override
    public Student_$Impl createFromParcel(Parcel in) {
      return new Student_$Impl(in);
    }

    @Override
    public Student_$Impl[] newArray(int size) {
      return new Student_$Impl[size];
    }
  };

  private int age;

  private String name;

  private long id;

  protected Student_$Impl(Parcel in) {
    this.age = in.readInt();
    this.name = in.readString();
    this.id = in.readLong();
  }

  public Student_$Impl() {
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(this.age);
    dest.writeString(this.name);
    dest.writeLong(this.id);
  }

  @Override
  public void recycle() {
    DataPools.recycle(this);
  }

  @Override
  public void clearProperties() {
    this.id = 0;
    this.age = 0;
    this.name = null;
  }

  @Override
  public String toString() {
    Objects.ToStringHelper helper = Objects.toStringHelper(this)
        .add("age", String.valueOf(this.age))
        .add("name", String.valueOf(this.name))
        .add("id", String.valueOf(this.id));
    return helper.toString();
  }

  public int getAge() {
    return age;
  }

  public Student setAge(int age1) {
    this.age = age1;
    return this;
  }

  public String getName() {
    return name;
  }

  public Student setName(String name1) {
    this.name = name1;
    return this;
  }

  public long getId() {
    return id;
  }

  public Student setId(long id1) {
    this.id = id1;
    return this;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof Student_$Impl)) {
      return false;
    }
     Student_$Impl that = (Student_$Impl) o;
    if (getAge() != that.getAge()) {
      return false;
    }
    if (getName() != null ? !getName().equals(that.getName()) : that.getName() != null) {
      return false;
    }
    if (getId() != that.getId()) {
      return false;
    }
    return true;
  }
}

下一章

Data-mediator入门系列2-2

想要体验最新的特性 ?

请到github/data-mediator体验。 如果觉得不错,请star支持下项目哈。

欢迎大家star, fork,contribute ,提issue. 它会越来越棒。

Thanks for reading !

技术源于分享!