The Wayback Machine - https://web.archive.org/web/20201002233031/https://github.com/simplezhli/Saber
Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Saber

jitpack LICENSE 作者

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

  • 已适配AndroidX。

  • 支�?Kotlin。

  • 支�? ViewModel�?AndroidViewModel。�?�?认为 ViewModel

  • 支�? observe�?observeForever 两种观察模式。�?�?认为 observe

  • 支�? SingleLiveEvent�?MediatorLiveData�?MutableLiveData。�?�?认为 MutableLiveData

  • 支�?自定义LiveData类型。

  • 支�?事件总线的操作。

  • Forever模式自动取�?订�?�。

详细介绍

使用方式

添加依赖

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

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

首�?�?�建一个类,使用@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.

About

🏄 帮助你快速使用Android的LiveData与ViewModel

Topics

Resources

License

Packages

No packages published
You can’t perform that action at this time.