All Projects → simplezhli → Saber

simplezhli / Saber

Licence: apache-2.0
🏄 帮助你快速使用Android的LiveData与ViewModel,已支持SavedState

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Saber

Aacomponents
基于google Android Architecture Components 封装实现组件式MVP快速开发框架
Stars: ✭ 66 (-53.85%)
Mutual labels:  lifecycle, viewmodel, livedata
Jetpackmvvm
🐔🏀一个Jetpack结合MVVM的快速开发框架,基于MVVM模式集成谷歌官方推荐的JetPack组件库:LiveData、ViewModel、Lifecycle、Navigation组件 使用Kotlin语言,添加大量拓展函数,简化代码 加入Retrofit网络请求,协程,帮你简化各种操作,让你快速开发项目
Stars: ✭ 1,100 (+669.23%)
Mutual labels:  lifecycle, viewmodel, livedata
TeamManagerApp
A sample app structure using the MVVM architecture LiveData, RxJava, ViewModel, Room and the Navigation Arch Components.
Stars: ✭ 36 (-74.83%)
Mutual labels:  viewmodel, lifecycle, livedata
Jetpack Mvvm Scaffold
人生苦短,让脚手架为你节省时间。(目前作为《最佳实践》项目的 Dev 版来优先更新)
Stars: ✭ 239 (+67.13%)
Mutual labels:  lifecycle, viewmodel, livedata
Aachulk
️🔥️🔥️🔥AACHulk是以Google的ViewModel+DataBinding+LiveData+Lifecycles框架为基础, 结合Okhttp+Retrofit+BaseRecyclerViewAdapterHelper+SmartRefreshLayout+ARouter打造的一款快速MVVM开发框架
Stars: ✭ 109 (-23.78%)
Mutual labels:  lifecycle, viewmodel, livedata
LifecycleMvp
No description or website provided.
Stars: ✭ 20 (-86.01%)
Mutual labels:  viewmodel, lifecycle, livedata
MockAppMVVM
A sample app structure using the MVVM architecture using Retrofit, Dagger2, LiveData, RxJava, ViewModel and Room.
Stars: ✭ 14 (-90.21%)
Mutual labels:  viewmodel, lifecycle, livedata
AndriodDevelopLibrary
Lifecycle->ViewModel+LiveData+Repository
Stars: ✭ 16 (-88.81%)
Mutual labels:  viewmodel, lifecycle, livedata
Mvvmarms
Android MVVM Architecture Components based on MVPArms and Android Architecture Components.
Stars: ✭ 425 (+197.2%)
Mutual labels:  lifecycle, viewmodel, livedata
Android Jetpack Demo
🔥 快速入门Android Jetpack以及相关Kotlin、RxJava、MVVM等主流技术,独立构架App的基础技能
Stars: ✭ 335 (+134.27%)
Mutual labels:  lifecycle, viewmodel, livedata
Mvvmframe
🏰 MVVMFrame for Android 是一个基于Google官方推出的Architecture Components dependencies(现在叫JetPack){ Lifecycle,LiveData,ViewModel,Room } 构建的快速开发框架。有了MVVMFrame的加持,从此构建一个MVVM模式的项目变得快捷简单。
Stars: ✭ 218 (+52.45%)
Mutual labels:  lifecycle, viewmodel, livedata
Readhubclient
Readhub客户端
Stars: ✭ 44 (-69.23%)
Mutual labels:  lifecycle, viewmodel, livedata
Androidbaseframemvvm
Android 组件化 MVVM 框架 基于 Jetpack + Kotlin
Stars: ✭ 169 (+18.18%)
Mutual labels:  lifecycle, viewmodel, livedata
Easychatandroidclient
EasyChat是一个开源的社交类的App。主要包含消息、好友、群组等相关的IM核心功能。部分界面参照了QQ、微信等相关社交APP。EasyChat APP整体采用MVVM模式,基于JetPack(Lifecycle,LiveData,ViewModel,Room)构建
Stars: ✭ 64 (-55.24%)
Mutual labels:  lifecycle, viewmodel, livedata
CoMvvmHelper
android mvvm 基础框架,适合日常快速开发。有需要添加的内容或者发现问题可以提 issue。
Stars: ✭ 26 (-81.82%)
Mutual labels:  viewmodel, lifecycle, livedata
Wanandroid
Jetpack MVVM For Wanandroid 最佳实践 !
Stars: ✭ 1,004 (+602.1%)
Mutual labels:  lifecycle, viewmodel, livedata
Tdcapp
Sample app which access the TDC (The Developer's Conference) REST API.
Stars: ✭ 55 (-61.54%)
Mutual labels:  lifecycle, viewmodel, livedata
Android Mvvm Architecture
A basic sample android application to understand MVVM in a very simple way.
Stars: ✭ 129 (-9.79%)
Mutual labels:  viewmodel, livedata
Githubprojectbrowser
This is a sample Android Project that is based on Clean Architecture
Stars: ✭ 64 (-55.24%)
Mutual labels:  viewmodel, livedata
Harrypotter
🧙🏻 Sample HarryPotter application based on MVVM architecture (ViewModel, LiveData, Repository, Coroutines, Koin or Dagger-Hilt)
Stars: ✭ 116 (-18.88%)
Mutual labels:  lifecycle, livedata

Saber

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].