Saber

Project Url: simplezhli/Saber
Introduction: 🏄 帮助你快速使用 Android 的 LiveData 与 ViewModel
More: Author   ReportBugs   
Tags:

jitpack LICENSE 作者

本项目帮助你快速使用 LiveData 与 ViewModel

  • 已适配 AndroidX。

  • 支持 Kotlin。

  • 支持 ViewModelAndroidViewModel。(默认为 ViewModel

  • 支持 observeobserveForever 两种观察模式。(默认为 observe

  • 支持 SingleLiveEventMediatorLiveDataMutableLiveData。(默认为 MutableLiveData

  • 支持 SavedState(仅 AndroidX)

  • 支持自定义LiveData类型。

  • 支持事件总线的操作。

  • Forever 模式自动取消订阅。

  • 支持注解处理器增量编译。

详细介绍

使用方式

添加依赖

    implementation 'com.github.simplezhli.saber:saber-api:0.3.1'
    //AndroidX 使用
    implementation 'com.github.simplezhli.saber:saberx-api:0.3.1'

    annotationProcessor 'com.github.simplezhli.saber:saber-compiler:0.3.1'

首先创建一个类,使用@LiveData注解标记你要保存的数据。注意这里的参数名称 value,下面会用到。

public class SeekBar {

    @LiveData
    Integer value;
}

当然也可以直接标记你的 JavaBean,来直接保存此类。那么参数名为类名的首字母小写:seekBar

@LiveData
public class SeekBar {

    Integer value;
}

使用@LiveData(classType = LiveDataClassType.LIST)可以指定对应的数据集合类型

Build -- > Make Project 会生成代码如下:

public class SeekBarViewModel extends ViewModel {
  private MutableLiveData<Integer> mValue;

  public MutableLiveData<Integer> getValue() {
    if (mValue == null) {
      mValue = new MutableLiveData<>();
    }
    return mValue;
  }

  public Integer getValueValue() {
    return getValue().getValue();
  }

  public void setValue(Integer mValue) {
    if (this.mValue == null) {
      return;
    }
    this.mValue.setValue(mValue);
  }

  public void postValue(Integer mValue) {
    if (this.mValue == null) {
      return;
    }
    this.mValue.postValue(mValue);
  }
}

如果想使用AndroidViewModel的话,可以添加@AndroidViewModel注解

@AndroidViewModel
public class SeekBar {

    @LiveData
    Integer value;
}

自定义LiveData类型

public class Single {

    @LiveData(type = LiveDataType.OTHER, liveDataType = XXXLiveData.class)
    Integer value;
}

生成代码提供了 LiveData 的常用操作。

  • setXXX()要在主线程中调用。

  • postXXX()既可在主线程也可在子线程中调用。

  • getXXX()用于获取观察者。

  • getXXXValue()可以获取保存的数据。

  • addSource()用于监听 LiveData。(MediatorLiveData 专用)

  • removeSource()移除监听的 LiveData。(MediatorLiveData 专用)

1. 普通使用方法

一般情况下可以直接使用它。比如:

public class TestFragment extends Fragment {

    private SeekBar mSeekBar;

    @BindViewModel(isShare = true) //<--标记需要绑定的 ViewModel
    SeekBarViewModel mSeekBarViewModel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_test, container, false);
        mSeekBar = root.findViewById(R.id.seekBar);
        Saber.bind(this); // <--这里绑定 ViewModel
        subscribeSeekBar();
        return root;
    }

    private void subscribeSeekBar() {

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    mSeekBarViewModel.setValue(progress);
                }
            }
            ......
        });
    }

    @OnChange(model = "mSeekBarViewModel") //<--接收变化的 ViewModel 变量名
    void setData(Integer value){ //注意这里使用 @LiveData 标记的参数名
        if (value != null) {
            mSeekBar.setProgress(value);
        }
    }
}

@BindViewModel用于绑定 ViewModel。

@OnChange(model = "xxx")用于接收指定 ViewModel 的数据变化,可以不设置,默认 model 名称为 mViewModel。

如果需要Fragment之间数据共享,需要@BindViewModel(isShare = true),当然也要保证传入相同的 key 值。默认 key 值是类的规范名称,也就是包名加类名。

这里写图片描述

所以一旦需要互通的 Fragment 类名或包名不一致,就无法数据共享。这时可以指定 key 值:@BindViewModel(key = "value")

2. 事件总线使用方法,详细用法参看 LiveEventBus

    @LiveEventBus(model = "key_name")
    void liveDataBus(String value){

    }

发送:

    LiveEventBus.get().with("key_name").postValue("value");

更多的使用方法可以参看本项目 demo。

3.Kotlin 环境使用注意事项

1.将以下代码添加到 build.gradle 文件中,保证生成代码的正确性。


    kapt {
        correctErrorTypes = true
    }

2.Kotlin 默认会生成 set/get 方法,并把属性设置为private 所以只要保证 Kotlin 中字段可见性不是private即可,简单解决可以在字段上添加 @JvmField,也可以使用lateinit.


    @BindViewModel
    lateinit var mViewModel: TestViewModel

    //或

    @JvmField
    @BindViewModel
    var mViewModel: TestViewModel? = null

TODO

1.因为现有的@OnChange注解承载的功能过多,不易使用。后面会将EventBus功能从中提出,添加一个新的注解(或许叫做@LiveEventBus)。

2.有什么好的建议或者功能欢迎提 Issues。

版本变化

Thanks For

License

Copyright 2018 simplezhli

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools